Skip to content

Commit

Permalink
Envoy proxy for gRPC-web
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrucznik committed Dec 3, 2020
1 parent ad35406 commit 540c761
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 13 deletions.
15 changes: 12 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ services:
ports:
- '8085:8085'
- '8086:8086'
expose:
- '8085'
- '8086'

envoy:
image: envoyproxy/envoy:v1.16-latest
volumes:
- ./envoy.yaml:/envoy.yaml
- ./logs:/logs
ports:
- '9901:9901'
- '8087:8087'
command:
- "--config-path /envoy.yaml"
- "--log-level info"
66 changes: 66 additions & 0 deletions envoy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
admin:
access_log_path: /dev/stdout
address:
socket_address:
address: 0.0.0.0
port_value: 9901

static_resources:
listeners:
- name: grpc-web-listener
address:
socket_address:
address: 0.0.0.0
port_value: 8087
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
stat_prefix: grpc-web-listener
access_log:
- name: envoy.access_loggers.file
typed_config:
"@type": type.googleapis.com/envoy.extensions.access_loggers.file.v3.FileAccessLog
path: /dev/stdout
http_filters:
- name: envoy.filters.http.grpc_web
- name: envoy.filters.http.cors
- name: envoy.filters.http.router
route_config:
name: local_route
virtual_hosts:
- name: grpc_web_service
domains:
- "*"
routes:
- name: grpc_web_route
match:
prefix: "/"
route:
cluster: mruv_api_placeholder_cluster
max_grpc_timeout: 5s
cors:
allow_origin_string_match:
- safe_regex:
google_re2: {}
regex: ".*"
allow_methods: GET, PUT, DELETE, POST, PATCH, OPTIONS
allow_headers: authorization,keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
expose_headers: grpc-status,grpc-message,x-envoy-upstream-service-time
max_age: "1728000"
clusters:
- name: mruv_api_placeholder_cluster
connect_timeout: 0.25s
lb_policy: round_robin
type: logical_dns
http2_protocol_options: { }
load_assignment:
cluster_name: mruv_api_placeholder_cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: mruv-api-placeholder
port_value: 8085
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.15

require (
github.com/MruV-RP/mruv-pb-go v0.9.2
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2
github.com/grpc-ecosystem/grpc-gateway/v2 v2.0.1
github.com/sirupsen/logrus v1.7.0
Expand Down
21 changes: 11 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,33 @@ import (

func main() {
viper.AutomaticEnv()
viper.SetDefault("HTTP_PORT", 8085)
viper.SetDefault("GRPC_PORT", 8086)
viper.SetDefault("GRPC_PORT", 8085)
viper.SetDefault("HTTP_PORT", 8086)

RunGRPCServer()
}

func RunGRPCServer() {
// if we crash the go code, we get the file name and line number in log
log.SetFlags(log.LstdFlags | log.Lshortfile)
logger := logrus.NewEntry(logrus.New())

logrus.Infoln("Starting listener for gRPC API")
logger.Infoln("Starting listener for gRPC API")
listener, err := net.Listen("tcp",
fmt.Sprintf("%s:%d", viper.GetString("HOST"), viper.GetInt("GRPC_PORT")))
if err != nil {
logrus.Fatalf("Failed to listen: %v", err)
}
logrus.Infoln("Listener started on", listener.Addr())
logger.Infoln("Listener started on", listener.Addr())

//serve gRPC services
s := grpc.NewServer(grpc.MaxRecvMsgSize(1048576),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpc_logrus.StreamServerInterceptor(logrus.NewEntry(logrus.New())),
grpc_logrus.StreamServerInterceptor(logger.WithField("type", "stream")),
grpc_recovery.StreamServerInterceptor(),
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_logrus.UnaryServerInterceptor(logrus.NewEntry(logrus.New())),
grpc_logrus.UnaryServerInterceptor(logger.WithField("type", "unary")),
grpc_recovery.UnaryServerInterceptor(),
)),
)
Expand Down Expand Up @@ -102,24 +103,24 @@ func RunGRPCServer() {
vehicles.RegisterMruVVehiclesServiceServer(s, services.NewVehiclesServer(gen))

go func() {
logrus.Println("Starting server.")
logger.Println("Starting server.")

if err := s.Serve(listener); err != nil {
logrus.Fatalln("Failed to serve", err)
logger.Fatalln("Failed to serve", err)
}
}()

// Set up the gRPC gateway for REST endpoints
if err = setUpgRPCGateway(); err != nil {
logrus.Fatalln("Failed to set up gRPC gateway: ", err)
logger.Fatalln("Failed to set up gRPC gateway: ", err)
}

// Wait for CTRL+C to exit
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)

<-ch // Block until signal is received
logrus.Infoln("\nStopping the server.")
logger.Infoln("\nStopping the server.")
}

func setUpgRPCGateway() error {
Expand Down

0 comments on commit 540c761

Please sign in to comment.