mirror of
https://github.com/lightninglabs/aperture.git
synced 2025-12-17 09:04:19 +01:00
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.
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package pricer
|
package pricer
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
// DefaultPricer provides the same price for any service path. It implements
|
// DefaultPricer provides the same price for any service path. It implements
|
||||||
// the Pricer interface.
|
// the Pricer interface.
|
||||||
@@ -16,8 +19,8 @@ func NewDefaultPricer(price int64) *DefaultPricer {
|
|||||||
|
|
||||||
// GetPrice returns the price charged for all resources of a service.
|
// GetPrice returns the price charged for all resources of a service.
|
||||||
// It is part of the Pricer interface.
|
// It is part of the Pricer interface.
|
||||||
func (d *DefaultPricer) GetPrice(_ context.Context, _ string) (int64,
|
func (d *DefaultPricer) GetPrice(_ context.Context,
|
||||||
error) {
|
_ *http.Request) (int64, error) {
|
||||||
|
|
||||||
return d.Price, nil
|
return d.Price, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package pricer
|
package pricer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/lightninglabs/aperture/pricesrpc"
|
"github.com/lightninglabs/aperture/pricesrpc"
|
||||||
"google.golang.org/grpc"
|
"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
|
// GetPrice queries the server for the price of a resource path and returns the
|
||||||
// price. GetPrice is part of the Pricer interface.
|
// 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{
|
resp, err := c.rpcClient.GetPrice(ctx, &pricesrpc.GetPriceRequest{
|
||||||
Path: path,
|
Path: r.URL.Path,
|
||||||
|
HttpRequestText: b.String(),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
package pricer
|
package pricer
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
// Pricer is an interface used to query price data from a price provider.
|
// Pricer is an interface used to query price data from a price provider.
|
||||||
type Pricer interface {
|
type Pricer interface {
|
||||||
// GetPrice should return the price in satoshis for the given
|
// GetPrice should return the price in satoshis for the given
|
||||||
// resource path.
|
// 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 should clean up the Pricer implementation if needed.
|
||||||
Close() error
|
Close() error
|
||||||
|
|||||||
@@ -155,9 +155,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// resources.
|
// resources.
|
||||||
acceptAuth := p.authenticator.Accept(&r.Header, resourceName)
|
acceptAuth := p.authenticator.Accept(&r.Header, resourceName)
|
||||||
if !acceptAuth {
|
if !acceptAuth {
|
||||||
price, err := target.pricer.GetPrice(
|
price, err := target.pricer.GetPrice(r.Context(), r)
|
||||||
r.Context(), r.URL.Path,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
prefixLog.Errorf("error getting "+
|
prefixLog.Errorf("error getting "+
|
||||||
"resource price: %v", err)
|
"resource price: %v", err)
|
||||||
@@ -197,7 +195,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
price, err := target.pricer.GetPrice(
|
price, err := target.pricer.GetPrice(
|
||||||
r.Context(), r.URL.Path,
|
r.Context(), r,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
prefixLog.Errorf("error getting "+
|
prefixLog.Errorf("error getting "+
|
||||||
|
|||||||
Reference in New Issue
Block a user