Skip to content

Commit 68b8d1b

Browse files
authored
Merge pull request #99 from Roasbeef/pricesrpc-full-req
multi: extend the pricesrpc interface to pass in the full HTTP request context
2 parents 9b85f8b + c396ca5 commit 68b8d1b

18 files changed

+856
-235
lines changed

.github/workflows/main.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ env:
2727
GO_VERSION: 1.19.2
2828

2929
jobs:
30+
rpc-check:
31+
name: RPC check
32+
runs-on: ubuntu-latest
33+
steps:
34+
- name: git checkout
35+
uses: actions/checkout@v3
36+
37+
- name: Generate RPC stubs and check REST annotations
38+
run: make rpc-check
39+
3040
########################
3141
# lint code
3242
########################

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ list:
111111
grep -v Makefile | \
112112
sort
113113

114+
rpc:
115+
@$(call print, "Compiling protos.")
116+
cd ./pricesrpc; ./gen_protos_docker.sh
117+
118+
rpc-format:
119+
@$(call print, "Formatting protos.")
120+
cd ./pricesrpc; find . -name "*.proto" | xargs clang-format --style=file -i
121+
122+
rpc-check: rpc
123+
@$(call print, "Verifying protos.")
124+
cd ./pricesrpc; ../scripts/check-rest-annotations.sh
125+
if test -n "$$(git status --porcelain)"; then echo "Protos not properly formatted or not compiled with correct version"; git status; git diff; exit 1; fi
126+
114127
clean:
115128
@$(call print, "Cleaning source.$(NC)")
116129
$(RM) ./aperture

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: 13 additions & 3 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,15 +70,23 @@ 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
7787
}
7888

79-
return resp.Price, nil
89+
return resp.PriceSats, nil
8090
}
8191

8292
// Close closes the gRPC connection. It is part of the Pricer interface.

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

pricesrpc/Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
FROM golang:1.20.4-buster
2+
3+
RUN apt-get update && apt-get install -y \
4+
git \
5+
protobuf-compiler='3.6.1*' \
6+
clang-format='1:7.0*'
7+
8+
# We don't want any default values for these variables to make sure they're
9+
# explicitly provided by parsing the go.mod file. Otherwise we might forget to
10+
# update them here if we bump the versions.
11+
ARG PROTOBUF_VERSION
12+
ARG GRPC_GATEWAY_VERSION
13+
14+
ENV PROTOC_GEN_GO_GRPC_VERSION="v1.1.0"
15+
ENV FALAFEL_VERSION="v0.9.1"
16+
ENV GOCACHE=/tmp/build/.cache
17+
ENV GOMODCACHE=/tmp/build/.modcache
18+
19+
RUN cd /tmp \
20+
&& mkdir -p /tmp/build/.cache \
21+
&& mkdir -p /tmp/build/.modcache \
22+
&& go install google.golang.org/protobuf/cmd/protoc-gen-go@${PROTOBUF_VERSION} \
23+
&& go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@${PROTOC_GEN_GO_GRPC_VERSION} \
24+
&& go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@${GRPC_GATEWAY_VERSION} \
25+
&& go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@${GRPC_GATEWAY_VERSION} \
26+
&& go install github.com/lightninglabs/falafel@${FALAFEL_VERSION} \
27+
&& go install golang.org/x/tools/cmd/goimports@v0.1.7 \
28+
&& chmod -R 777 /tmp/build/
29+
30+
WORKDIR /build
31+
32+
CMD ["/bin/bash", "/build/pricesrpc/gen_protos.sh"]

pricesrpc/gen_protos.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# generate compiles the *.pb.go stubs from the *.proto files.
6+
function generate() {
7+
echo "Generating root gRPC server protos"
8+
9+
PROTOS="prices.proto"
10+
11+
# For each of the sub-servers, we then generate their protos, but a restricted
12+
# set as they don't yet require REST proxies, or swagger docs.
13+
for file in $PROTOS; do
14+
DIRECTORY=$(dirname "${file}")
15+
echo "Generating protos from ${file}, into ${DIRECTORY}"
16+
17+
# Generate the protos.
18+
protoc -I/usr/local/include -I. \
19+
--go_out . --go_opt paths=source_relative \
20+
--go-grpc_out . --go-grpc_opt paths=source_relative \
21+
"${file}"
22+
23+
# Generate the REST reverse proxy.
24+
annotationsFile=${file//proto/yaml}
25+
protoc -I/usr/local/include -I. \
26+
--grpc-gateway_out . \
27+
--grpc-gateway_opt logtostderr=true \
28+
--grpc-gateway_opt paths=source_relative \
29+
--grpc-gateway_opt grpc_api_configuration=${annotationsFile} \
30+
"${file}"
31+
32+
# Generate the swagger file which describes the REST API in detail.
33+
protoc -I/usr/local/include -I. \
34+
--openapiv2_out . \
35+
--openapiv2_opt logtostderr=true \
36+
--openapiv2_opt grpc_api_configuration=${annotationsFile} \
37+
--openapiv2_opt json_names_for_fields=false \
38+
"${file}"
39+
done
40+
41+
# Generate the JSON/WASM client stubs.
42+
falafel=$(which falafel)
43+
pkg="pricesrpc"
44+
opts="package_name=$pkg,js_stubs=1,build_tags=// +build js"
45+
protoc -I/usr/local/include -I. -I.. \
46+
--plugin=protoc-gen-custom=$falafel\
47+
--custom_out=. \
48+
--custom_opt="$opts" \
49+
prices.proto
50+
51+
PACKAGES=""
52+
for package in $PACKAGES; do
53+
54+
opts="package_name=$package,manual_import=$manual_import,js_stubs=1,build_tags=// +build js"
55+
pushd $package
56+
protoc -I/usr/local/include -I. -I.. \
57+
--plugin=protoc-gen-custom=$falafel\
58+
--custom_out=. \
59+
--custom_opt="$opts" \
60+
"$(find . -name '*.proto')"
61+
popd
62+
done
63+
}
64+
65+
# format formats the *.proto files with the clang-format utility.
66+
function format() {
67+
find . -name "*.proto" -print0 | xargs -0 clang-format --style=file -i
68+
}
69+
70+
# Compile and format the pricesrpc package.
71+
pushd pricesrpc
72+
format
73+
generate
74+
popd
75+
76+
if [[ "$COMPILE_MOBILE" == "1" ]]; then
77+
pushd mobile
78+
./gen_bindings.sh $FALAFEL_VERSION
79+
popd
80+
fi

pricesrpc/gen_protos_docker.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Directory of the script file, independent of where it's called from.
6+
DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
7+
8+
PROTOBUF_VERSION=$(go list -f '{{.Version}}' -m google.golang.org/protobuf)
9+
GRPC_GATEWAY_VERSION=$(go list -f '{{.Version}}' -m github.com/grpc-ecosystem/grpc-gateway/v2)
10+
11+
echo "Building protobuf compiler docker image..."
12+
docker build -t aperture-protobuf-builder \
13+
--build-arg PROTOBUF_VERSION="$PROTOBUF_VERSION" \
14+
--build-arg GRPC_GATEWAY_VERSION="$GRPC_GATEWAY_VERSION" \
15+
.
16+
17+
echo "Compiling and formatting *.proto files..."
18+
docker run \
19+
--rm \
20+
--user "$UID:$(id -g)" \
21+
-e UID=$UID \
22+
-e COMPILE_MOBILE \
23+
-e SUBSERVER_PREFIX \
24+
-v "$DIR/../:/build" \
25+
aperture-protobuf-builder

0 commit comments

Comments
 (0)