Skip to content

Commit

Permalink
feat: use standard grpc-web instead of improbable-eng
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz committed Nov 15, 2023
1 parent 7ee1a26 commit 6a64c18
Show file tree
Hide file tree
Showing 35 changed files with 47,070 additions and 23,100 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ generate.protobuf: node_modules

.PHONY: generate.weshnet
generate.weshnet: node_modules
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
buf generate buf.build/berty/weshnet -o .weshgen
rm -fr packages/api/weshnet
mkdir -p packages/api/weshnet
buf generate --template ./weshnet.buf.gen.yaml buf.build/gogo/protobuf -o .gogogen
cp -r .gogogen/packages/api/gogoproto packages/api/weshnet/gogoproto
rm -fr .gogogen
buf generate --template ./weshnet.buf.gen.yaml buf.build/berty/weshnet -o .weshgen
cp -r .weshgen/packages/api/ packages/api/weshnet/
rm -fr .weshgen

Expand Down
1 change: 1 addition & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ lint:
- node_modules
deps:
- buf.build/berty/weshnet:a01c754f0f5441d4b4400796c7d170a2
- buf.build/gogo/protobuf:5461a3dfa9d941da82028ab185dc2a0e
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ services:
- prices-data:/var/lib/postgresql/data/ # persist data even if container shuts down
ports:
- "5433:5432"
envoy:
image: envoyproxy/envoy:v1.28.0
volumes:
- ./envoy.yaml:/etc/envoy/envoy.yaml
depends_on:
- weshd
ports:
- "8080:8080"
- "8042:8042"
- "9901:9901"
weshd:
build:
context: .
dockerfile: ./go/cmd/weshd/Dockerfile
platforms:
- linux/amd64
ports:
- "4242:4242"


volumes:
teritori-data: # named volumes can be managed easier using docker-compose
Expand Down
59 changes: 59 additions & 0 deletions envoy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
admin:
address:
socket_address: { address: 0.0.0.0, port_value: 9901 }

static_resources:
listeners:
- name: listener_0
address:
socket_address: { address: 0.0.0.0, port_value: 8080 }
filter_chains:
- filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
codec_type: auto
stat_prefix: ingress_http
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match: { prefix: "/" }
route:
cluster: weshnet_service
max_stream_duration:
grpc_timeout_header_max: 0s
cors:
allow_origin_string_match:
- prefix: "*"
allow_methods: GET, PUT, DELETE, POST, OPTIONS
allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
max_age: "1728000"
expose_headers: custom-header-1,grpc-status,grpc-message
http_filters:
- name: envoy.filters.http.grpc_web
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.grpc_web.v3.GrpcWeb
- name: envoy.filters.http.cors
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.cors.v3.Cors
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
clusters:
- name: weshnet_service
connect_timeout: 0.25s
type: LOGICAL_DNS
http2_protocol_options: {}
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: weshnet_service
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: weshd
port_value: 4242
2 changes: 1 addition & 1 deletion go/cmd/teritori-dapp-backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1

ARG GO_VERSION="1.18"
ARG GO_VERSION="1.19"
ARG RUNNER_IMAGE="gcr.io/distroless/static"

# --------------------------------------------------------
Expand Down
16 changes: 15 additions & 1 deletion go/cmd/teritori-dapp-backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
"net"
"net/http"
"os"
"strings"
Expand All @@ -24,6 +25,7 @@ import (
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

func main() {
Expand Down Expand Up @@ -133,10 +135,22 @@ func main() {
p2epb.RegisterP2EServiceServer(server, p2eSvc)
daopb.RegisterDAOServiceServer(server, daoSvc)
feedpb.RegisterFeedServiceServer(server, feedSvc)
reflection.Register(server)

lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 9042))
if err != nil {
panic(errors.Wrap(err, "failed to listen"))
}
go func() {
if err := server.Serve(lis); err != nil {
panic(err)
}
}()

wrappedServer := grpcweb.WrapServer(server,
grpcweb.WithWebsockets(true),
grpcweb.WithWebsocketOriginFunc(func(*http.Request) bool { return true }))
grpcweb.WithWebsocketOriginFunc(func(*http.Request) bool { return true }),
)

handler := func(resp http.ResponseWriter, req *http.Request) {
resp.Header().Set("Access-Control-Allow-Origin", "*")
Expand Down
46 changes: 46 additions & 0 deletions go/cmd/weshd/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# syntax=docker/dockerfile:1

ARG GO_VERSION="1.19"
ARG RUNNER_IMAGE="gcr.io/distroless/static"

# --------------------------------------------------------
# Builder
# --------------------------------------------------------

FROM golang:${GO_VERSION}-alpine as builder

RUN set -eux; apk add --no-cache ca-certificates build-base; apk add git linux-headers

# Download go dependencies
WORKDIR /app
COPY go.* ./
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/root/go/pkg/mod \
go mod download

# Copy the remaining files
COPY go ./go
COPY .git ./.git

# Build teritorid binary
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/root/go/pkg/mod \
go build \
-mod=readonly \
-ldflags "-w -s -linkmode=external -extldflags '-Wl,-z,muldefs -static'" \
-trimpath \
-o /app/build/ \
./go/cmd/weshd

# --------------------------------------------------------
# Runner
# --------------------------------------------------------

FROM ${RUNNER_IMAGE}

COPY --from=builder /app/build/weshd /bin/weshd

ENV HOME /app
WORKDIR $HOME

ENTRYPOINT ["weshd"]
67 changes: 32 additions & 35 deletions go/cmd/weshd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"flag"
"fmt"
"net"
"os/signal"
"strconv"
"sync"
"syscall"

mrand "math/rand"
"net/http"
"os"

"berty.tech/weshnet"
Expand All @@ -24,8 +25,8 @@ import (
"github.com/peterbourgon/ff/v3"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -60,21 +61,21 @@ func checkAndProcessDir() {
fmt.Printf("Directory '%s' and its parent directories have been created.\n", weshDir)
}

func startHTTPServer() {
func startServer() func() error {

fmt.Printf("Using port: %d\n", port)
logger, err := zap.NewDevelopment()
if err != nil {
panic(errors.Wrap(err, "failed to create logger"))
}

logger.Info("server config", zap.Int("port", port))

fs := flag.NewFlagSet("weshd", flag.ContinueOnError)

if err := ff.Parse(fs, os.Args[1:]); err != nil {
panic(errors.Wrap(err, "failed to parse flags"))
}

logger, err := zap.NewDevelopment()
if err != nil {
panic(errors.Wrap(err, "failed to create logger"))
}

logger.Info("weshd", zap.Int("port", port))

grpcServer := grpc.NewServer()
Expand Down Expand Up @@ -132,51 +133,47 @@ func startHTTPServer() {
if err != nil {
panic(errors.Wrap(err, "failed to create weshnet server"))
}
defer svc.Close()

protocoltypes.RegisterProtocolServiceServer(grpcServer, svc)
wrappedServer := grpcweb.WrapServer(grpcServer,
grpcweb.WithOriginFunc(func(string) bool { return true }), // @FIXME: this is very insecure
grpcweb.WithWebsockets(true),
)
handler := func(resp http.ResponseWriter, req *http.Request) {
resp.Header().Set("Access-Control-Allow-Origin", "*")
resp.Header().Set("Access-Control-Allow-Headers", "*")
logger.Debug(fmt.Sprintf("Request: %v", req))
wrappedServer.ServeHTTP(resp, req)
}
reflection.Register(grpcServer)

httpServer := http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: http.HandlerFunc(handler),
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
panic(errors.Wrap(err, "failed to listen"))
}

if err := httpServer.ListenAndServe(); err != nil {
panic(errors.Wrap(err, "failed to start http server"))
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
if err := grpcServer.Serve(lis); err != nil {
panic(err)
}
wg.Done()
}()
return func() error {
grpcServer.GracefulStop()
wg.Wait()
return svc.Close()
}

}

func main() {

checkAndUpdatePortFromArgs()
checkAndProcessDir()

stopHTTPServer := make(chan struct{})

go func() {
startHTTPServer()
close(stopHTTPServer)
}()
stopServer := startServer()

interruptChannel := make(chan os.Signal, 1)
signal.Notify(interruptChannel, os.Interrupt, syscall.SIGTERM)

<-interruptChannel

fmt.Println("Ctrl+C received. Stopping the program.")
fmt.Println("Termination signal received. Stopping the program.")

close(stopHTTPServer)
if err := stopServer(); err != nil {
panic(err)
}

fmt.Println("Program has exited.")
fmt.Println("Server stopped gracefully.")
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
"expo-font": "~11.4.0",
"expo-linear-gradient": "~12.3.0",
"expo-status-bar": "~1.6.0",
"google-protobuf": "^3.21.2",
"grpc-web": "^1.5.0",
"html-to-draftjs": "^1.5.0",
"lodash": "^4.17.21",
"long": "^5.2.1",
Expand Down
Loading

0 comments on commit 6a64c18

Please sign in to comment.