-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
67 lines (55 loc) · 2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"net"
"os"
"github.com/gdong42/grpc-mate/http"
"github.com/gdong42/grpc-mate/proxy"
"go.uber.org/zap"
"github.com/gdong42/grpc-mate/log"
"github.com/kelseyhightower/envconfig"
"google.golang.org/grpc"
)
// EnvConfig has all Environment variables that grpc-mate reads
type EnvConfig struct {
// Port the HTTP Port grpc-mate listens on, defaults to 6600
Port int `envconfig:"GRPC_MATE_PORT" default:"6600"`
// GrpcServerHost the backend gRPC Host grpc-mate connects to, defaults to 127.0.0.1
GrpcServerHost string `envconfig:"GRPC_MATE_PROXIED_HOST" default:"127.0.0.1"`
// GrpcServerPort the backend gRPC Port grpc-mate connects to, defaults to 9090
GrpcServerPort int `envconfig:"GRPC_MATE_PROXIED_PORT" default:"9090"`
// LogLevel the log level, must be INFO, DEBUG, or ERROR, defaults to INFO
LogLevel string `envconfig:"GRPC_MATE_LOG_LEVEL" default:"INFO"`
}
func main() {
var env EnvConfig
if err := envconfig.Process("", &env); err != nil {
fmt.Fprintf(os.Stderr, "[FATAL] Failed to read environment variables: %s\n", err.Error())
os.Exit(1)
}
logger, err := log.NewLogger(env.LogLevel)
if err != nil {
fmt.Fprintf(os.Stderr, "[ERROR] Failed to create logger: %s\n", err)
os.Exit(1)
}
grpcAddr := fmt.Sprintf("%s:%d", env.GrpcServerHost, env.GrpcServerPort)
logger.Info("Connecting to gRPC service...", zap.String("grpc_addr", grpcAddr))
conn, err := grpc.Dial(grpcAddr, grpc.WithInsecure())
if err != nil {
logger.Fatal("Could not connect to gRPC service", zap.String("grpc_addr", grpcAddr))
}
defer conn.Close()
proxy := proxy.NewProxy(conn)
s := http.New(proxy, logger)
logger.Info("starting grpc-mate",
zap.String("log_level", env.LogLevel),
zap.Int("port", env.Port),
)
logger.Info("gRPC Mate Serving on %d...", zap.Int("port", env.Port))
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", env.Port))
if err != nil {
logger.Fatal("[FATAL] Failed to listen HTTP port \n", zap.Int("port", env.Port), zap.Error(err))
os.Exit(1)
}
s.Serve(ln)
}