Skip to content

Commit 40df8f8

Browse files
committed
proxy+pricer: pass in the entire HTTP requests for GetPrice
In this commit, we modify the `GetPrice` method and interface to accept the full request instead of _just_ the path. For backwards compat, we leave the path in place, but also include the full serialized HTTP request.
1 parent 3a5dd96 commit 40df8f8

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

pricer/defaultPricer.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package pricer
22

3-
import "context"
3+
import (
4+
"context"
5+
"net/http"
6+
)
47

58
// DefaultPricer provides the same price for any service path. It implements
69
// the Pricer interface.
@@ -16,8 +19,8 @@ func NewDefaultPricer(price int64) *DefaultPricer {
1619

1720
// GetPrice returns the price charged for all resources of a service.
1821
// It is part of the Pricer interface.
19-
func (d *DefaultPricer) GetPrice(_ context.Context, _ string) (int64,
20-
error) {
22+
func (d *DefaultPricer) GetPrice(_ context.Context,
23+
_ *http.Request) (int64, error) {
2124

2225
return d.Price, nil
2326
}

pricer/grpcPricer.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package pricer
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
7+
"net/http"
68

79
"github.com/lightninglabs/aperture/pricesrpc"
810
"google.golang.org/grpc"
@@ -68,9 +70,17 @@ func NewGRPCPricer(cfg *Config) (*GRPCPricer, error) {
6870

6971
// GetPrice queries the server for the price of a resource path and returns the
7072
// price. GetPrice is part of the Pricer interface.
71-
func (c GRPCPricer) GetPrice(ctx context.Context, path string) (int64, error) {
73+
func (c GRPCPricer) GetPrice(ctx context.Context,
74+
r *http.Request) (int64, error) {
75+
76+
var b bytes.Buffer
77+
if err := r.Write(&b); err != nil {
78+
return 0, nil
79+
}
80+
7281
resp, err := c.rpcClient.GetPrice(ctx, &pricesrpc.GetPriceRequest{
73-
Path: path,
82+
Path: r.URL.Path,
83+
HttpRequestText: b.String(),
7484
})
7585
if err != nil {
7686
return 0, err

pricer/pricer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package pricer
22

3-
import "context"
3+
import (
4+
"context"
5+
"net/http"
6+
)
47

58
// Pricer is an interface used to query price data from a price provider.
69
type Pricer interface {
710
// GetPrice should return the price in satoshis for the given
811
// resource path.
9-
GetPrice(ctx context.Context, path string) (int64, error)
12+
GetPrice(ctx context.Context, req *http.Request) (int64, error)
1013

1114
// Close should clean up the Pricer implementation if needed.
1215
Close() error

proxy/proxy.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
155155
// resources.
156156
acceptAuth := p.authenticator.Accept(&r.Header, resourceName)
157157
if !acceptAuth {
158-
price, err := target.pricer.GetPrice(
159-
r.Context(), r.URL.Path,
160-
)
158+
price, err := target.pricer.GetPrice(r.Context(), r)
161159
if err != nil {
162160
prefixLog.Errorf("error getting "+
163161
"resource price: %v", err)
@@ -197,7 +195,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
197195
}
198196
if !ok {
199197
price, err := target.pricer.GetPrice(
200-
r.Context(), r.URL.Path,
198+
r.Context(), r,
201199
)
202200
if err != nil {
203201
prefixLog.Errorf("error getting "+

0 commit comments

Comments
 (0)