diff --git a/pricer/defaultPricer.go b/pricer/defaultPricer.go index 103b89d..fe99006 100644 --- a/pricer/defaultPricer.go +++ b/pricer/defaultPricer.go @@ -1,6 +1,9 @@ package pricer -import "context" +import ( + "context" + "net/http" +) // DefaultPricer provides the same price for any service path. It implements // the Pricer interface. @@ -16,8 +19,8 @@ func NewDefaultPricer(price int64) *DefaultPricer { // GetPrice returns the price charged for all resources of a service. // It is part of the Pricer interface. -func (d *DefaultPricer) GetPrice(_ context.Context, _ string) (int64, - error) { +func (d *DefaultPricer) GetPrice(_ context.Context, + _ *http.Request) (int64, error) { return d.Price, nil } diff --git a/pricer/grpcPricer.go b/pricer/grpcPricer.go index 59b9034..f9f84ca 100644 --- a/pricer/grpcPricer.go +++ b/pricer/grpcPricer.go @@ -1,8 +1,10 @@ package pricer import ( + "bytes" "context" "fmt" + "net/http" "github.com/lightninglabs/aperture/pricesrpc" "google.golang.org/grpc" @@ -68,9 +70,17 @@ func NewGRPCPricer(cfg *Config) (*GRPCPricer, error) { // GetPrice queries the server for the price of a resource path and returns the // price. GetPrice is part of the Pricer interface. -func (c GRPCPricer) GetPrice(ctx context.Context, path string) (int64, error) { +func (c GRPCPricer) GetPrice(ctx context.Context, + r *http.Request) (int64, error) { + + var b bytes.Buffer + if err := r.Write(&b); err != nil { + return 0, nil + } + resp, err := c.rpcClient.GetPrice(ctx, &pricesrpc.GetPriceRequest{ - Path: path, + Path: r.URL.Path, + HttpRequestText: b.String(), }) if err != nil { return 0, err diff --git a/pricer/pricer.go b/pricer/pricer.go index 3309380..a2ec6d9 100644 --- a/pricer/pricer.go +++ b/pricer/pricer.go @@ -1,12 +1,15 @@ package pricer -import "context" +import ( + "context" + "net/http" +) // Pricer is an interface used to query price data from a price provider. type Pricer interface { // GetPrice should return the price in satoshis for the given // resource path. - GetPrice(ctx context.Context, path string) (int64, error) + GetPrice(ctx context.Context, req *http.Request) (int64, error) // Close should clean up the Pricer implementation if needed. Close() error diff --git a/proxy/proxy.go b/proxy/proxy.go index c1dc24b..d686fb8 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -155,9 +155,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { // resources. acceptAuth := p.authenticator.Accept(&r.Header, resourceName) if !acceptAuth { - price, err := target.pricer.GetPrice( - r.Context(), r.URL.Path, - ) + price, err := target.pricer.GetPrice(r.Context(), r) if err != nil { prefixLog.Errorf("error getting "+ "resource price: %v", err) @@ -197,7 +195,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if !ok { price, err := target.pricer.GetPrice( - r.Context(), r.URL.Path, + r.Context(), r, ) if err != nil { prefixLog.Errorf("error getting "+