Api

Description

Backend API build with Nest Framework.

Generate

Generate code with protoc

  1. 1.
    Generate nest server-side code
  2. 2.
    Generate node server-side code (optional)
  3. 3.
    Generate grpc-web client-side code
  4. 4.
    Generate grpc-web client-side TS code (optional)
1
make proto
Copied!
As an alternative to make proto, you can also generate same code manually...
1
protoc -I="./proto" ./proto/yeti/echo/v1/echo.proto \
2
--js_out=import_style=commonjs:/libs/gen/grpcweb/src/lib \
3
--grpc-web_out=import_style=typescript,mode=grpcwebtext:./libs/gen/grpcweb/src/lib
Copied!
Generate node server-side code
Refer NESTJS
1
protoc --plugin=./node_modules/ts-proto/protoc-gen-ts_proto \
2
-I="./proto" -I="third_party/proto" --ts_proto_opt=nestJs=true,addGrpcMetadata=true,addNestjsRestParameter=true --ts_proto_out=libs/gen/nest/src/lib ./proto/yeti/echo/v1/echo.proto
3
4
protoc --plugin=./node_modules/ts-proto/protoc-gen-ts_proto \
5
-I="./proto" -I="third_party/proto" --ts_proto_opt=nestJs=true,addGrpcMetadata=true,addNestjsRestParameter=true --ts_proto_out=libs/gen/nest/src/lib ./proto/yeti/account/v1/account.proto
Copied!

Generate certs

1
# first time, generate local certs
2
./config/certs/certs.sh
Copied!

Run

Run Dev Mode

1
# start in watch mode
2
ng serve api
3
# start with TLS. ***To use with Envoy, start with TLS***
4
SECURE=true ng serve api
5
# to turn on logging for `request`
6
NODE_DEBUG=request ng serve api
7
DEBUG=typeorm:* ng serve api
8
# optionally you can run with prod env(environment.prod.ts) for testing! use this for testing only.
9
ng serve api --prod
Copied!
start envoy
1
docker-compose up envoy
Copied!
envoy admin: http://localhost:9901/

Test Dev Mode

1
# test API directly (nestJS)
2
grpcurl -plaintext \
3
-protoset <(buf image build -o -) \
4
-d '{"message": "sumo"}' 0.0.0.0:5000 yeti.echo.v1.EchoService/Echo
5
# test API directly (nestJS) with TLS
6
grpcurl -insecure \
7
-protoset <(buf image build -o -) \
8
-d '{"message": "sumo"}' 0.0.0.0:5000 yeti.echo.v1.EchoService/Echo
9
10
# test API via envoy
11
grpcurl -plaintext \
12
-protoset <(buf image build -o -) \
13
-d '{"message": "sumo"}' 0.0.0.0:9090 yeti.echo.v1.EchoService/Echo
14
# test API via envoy with TLS
15
grpcurl -cacert=config/certs/ca-cert.pem \
16
-protoset <(buf image build -o -) \
17
-d '{"message": "sumo"}' localhost:9444 yeti.echo.v1.EchoService/Echo
18
19
# make sure `require_client_certificate: true` is enabled in `listeners.yaml` for following tests:
20
21
# test API via envoy with TLS, and client cert
22
grpcurl -cacert=config/certs/ca-cert.pem \
23
-cert=config/certs/client-cert.pem \
24
-key=config/certs/client-key.pem \
25
-protoset <(buf image build -o -) \
26
-d '{"message": "sumo"}' localhost:9444 yeti.echo.v1.EchoService/Echo
27
# test with wrong client cert. This will fail!
28
grpcurl -cacert=config/certs/ca-cert.pem \
29
-cert=config/certs/upstream-cert.pem \
30
-key=config/certs/upstream-key.pem \
31
-protoset <(buf image build -o -) \
32
-d '{"message": "sumo"}' localhost:9444 yeti.echo.v1.EchoService/Echo
33
34
# testing with request data from file.
35
grpcurl -cacert=config/certs/ca-cert.pem \
36
-protoset <(buf image build -o -) \
37
-v -H trans_id=abc123 \
38
-d @ localhost:9444 yeti.echo.v1.EchoService/Echo <test/echo-request.json
Copied!

Test gRPC-Web

1
# without TLS
2
3
curl 'http://localhost:9090/greetersrv/Greeter.Hello' \
4
-H 'Content-Type: application/grpc-web+proto' \
5
-H 'X-Grpc-Web: 1' \
6
-H 'custom-header-1: value1' \
7
-H 'Accept: */*' \
8
-H 'Connection: keep-alive' \
9
--data-binary #x27;\x00\x00\x00\x00\x05\n\x03abc' --compressed
10
11
curl 'http://localhost:9090/yeti.EchoService/Echo' \
12
-H 'Accept: application/grpc-web-text' \
13
-H 'Content-Type: application/grpc-web-text' \
14
-H 'X-Grpc-Web: 1' \
15
-H 'Connection: keep-alive' \
16
-H 'Accept-Encoding: gzip, deflate, br' \
17
--data-binary 'AAAAAAYKBHN1bW8=' --compressed
Copied!
Last modified 10mo ago
Copy link