Skip to content

Commit

Permalink
Add grpc-web support, generating TS proto
Browse files Browse the repository at this point in the history
  • Loading branch information
pomo-mondreganto committed Nov 17, 2023
1 parent 6d88657 commit cc27f3b
Show file tree
Hide file tree
Showing 9 changed files with 475 additions and 23 deletions.
39 changes: 21 additions & 18 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os/signal"
"strings"
Expand All @@ -25,6 +25,7 @@ import (
"github.com/c4t-but-s4d/neo/v2/internal/server/fs"
logs "github.com/c4t-but-s4d/neo/v2/internal/server/logs"
"github.com/c4t-but-s4d/neo/v2/pkg/grpcauth"
"github.com/c4t-but-s4d/neo/v2/pkg/mu"
"github.com/c4t-but-s4d/neo/v2/pkg/neosync"
epb "github.com/c4t-but-s4d/neo/v2/proto/go/exploits"
fspb "github.com/c4t-but-s4d/neo/v2/proto/go/fileserver"
Expand Down Expand Up @@ -73,11 +74,6 @@ func main() {
}
logsServer := logs.New(logStore)

lis, err := net.Listen("tcp", cfg.Addr)
if err != nil {
logrus.Fatalf("Failed to listen: %v", err)
}

var opts []grpc.ServerOption
if cfg.GrpcAuthKey != "" {
authInterceptor := grpcauth.NewServerInterceptor(cfg.GrpcAuthKey)
Expand All @@ -91,13 +87,13 @@ func main() {
logspb.RegisterServiceServer(s, logsServer)
reflection.Register(s)

http.Handle("/metrics", promhttp.Handler())
go func() {
logrus.Infof("Starting metrics server on %s", viper.GetString("metrics.address"))
if err := http.ListenAndServe(viper.GetString("metrics.address"), http.DefaultServeMux); err != nil {
logrus.Fatalf("Failed to serve metrics: %v", err)
}
}()
httpMux := http.NewServeMux()
httpMux.Handle("/metrics", promhttp.Handler())
muHandler := mu.NewHandler(s, mu.WithHTTPHandler(httpMux))
httpServer := &http.Server{
Handler: muHandler,
Addr: cfg.Address,
}

runCtx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer cancel()
Expand All @@ -117,11 +113,19 @@ func main() {
defer wg.Done()
<-runCtx.Done()
logrus.Info("Received shutdown signal, stopping server")
s.GracefulStop()

shutdownCtx, shutdownCancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer shutdownCancel()
shutdownCtx, shutdownCancel = context.WithTimeout(shutdownCtx, 10*time.Second)
defer shutdownCancel()

if err := httpServer.Shutdown(shutdownCtx); err != nil {
logrus.Errorf("Failed to shutdown http server: %v", err)
}
}()

logrus.Infof("Starting server on %s", cfg.Addr)
if err := s.Serve(lis); err != nil {
logrus.Infof("Starting multiproto server on %s", cfg.Address)
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logrus.Fatalf("Failed to serve: %v", err)
}

Expand Down Expand Up @@ -153,8 +157,7 @@ func setupConfig() error {
viper.SetDefault("config", "server_config.yml")
viper.SetDefault("ping_every", time.Second*5)
viper.SetDefault("submit_every", time.Second*2)
viper.SetDefault("metrics.address", ":3000")
viper.SetDefault("addr", ":5005")
viper.SetDefault("address", ":5005")

return nil
}
Expand Down
1 change: 1 addition & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ services:

victoria:
image: victoriametrics/victoria-metrics:v1.92.1
user: 0
volumes:
- ./monitoring/cfg/prometheus:/etc/prometheus
- ./volumes/victoria-metrics:/victoria-metrics-data
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,32 @@ require (
)

require (
github.com/improbable-eng/grpc-web v0.15.0
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/common v0.44.0
github.com/samber/lo v1.38.1
golang.org/x/net v0.17.0
golang.org/x/sys v0.13.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand All @@ -51,8 +57,8 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
nhooyr.io/websocket v1.8.6 // indirect
)
Loading

0 comments on commit cc27f3b

Please sign in to comment.