Skip to content

Commit

Permalink
chore: revert to improbable-eng/grpc-web
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <norman@berty.tech>
  • Loading branch information
n0izn0iz committed Nov 15, 2023
1 parent 9f81dda commit 73cecc3
Show file tree
Hide file tree
Showing 33 changed files with 24,376 additions and 47,035 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ packages/api/weshnet: node_modules
buf generate --template ./weshnet.buf.gen.yaml buf.build/berty/weshnet -o .weshgen
cp -r .weshgen/packages/api $@
rm -fr .weshgen
buf generate --template ./weshnet.buf.gen.yaml buf.build/gogo/protobuf -o .gogogen
cp -r .gogogen/packages/api/gogoproto $@/gogoproto
rm -fr .gogogen

.PHONY: generate.graphql
generate.graphql:
Expand Down
1 change: 0 additions & 1 deletion buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ lint:
- node_modules
deps:
- buf.build/berty/weshnet:a01c754f0f5441d4b4400796c7d170a2
- buf.build/gogo/protobuf:5461a3dfa9d941da82028ab185dc2a0e
19 changes: 0 additions & 19 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,6 @@ 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: 0 additions & 59 deletions envoy.yaml

This file was deleted.

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.19"
ARG GO_VERSION="1.18"
ARG RUNNER_IMAGE="gcr.io/distroless/static"

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

func main() {
Expand Down Expand Up @@ -135,22 +133,10 @@ 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: 0 additions & 46 deletions go/cmd/weshd/Dockerfile

This file was deleted.

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

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

"berty.tech/weshnet"
Expand All @@ -25,8 +24,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 @@ -61,21 +60,21 @@ func checkAndProcessDir() {
fmt.Printf("Directory '%s' and its parent directories have been created.\n", weshDir)
}

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

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

logger.Info("server config", zap.Int("port", port))
fmt.Printf("Using port: %d\n", 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 @@ -133,47 +132,51 @@ func startServer() func() error {
if err != nil {
panic(errors.Wrap(err, "failed to create weshnet server"))
}
defer svc.Close()

protocoltypes.RegisterProtocolServiceServer(grpcServer, svc)
reflection.Register(grpcServer)
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)
}

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

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()
if err := httpServer.ListenAndServe(); err != nil {
panic(errors.Wrap(err, "failed to start http server"))
}

}

func main() {

checkAndUpdatePortFromArgs()
checkAndProcessDir()

stopServer := startServer()
stopHTTPServer := make(chan struct{})

go func() {
startHTTPServer()
close(stopHTTPServer)
}()

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

<-interruptChannel

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

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

fmt.Println("Server stopped gracefully.")
fmt.Println("Program has exited.")
}
Loading

0 comments on commit 73cecc3

Please sign in to comment.