-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
130 lines (112 loc) · 3.65 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"strconv"
healthpb "github.com/crossphoton/email-microservice/health"
"github.com/crossphoton/email-microservice/src"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
// grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
grpc_validator "github.com/grpc-ecosystem/go-grpc-middleware/validator"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
grpc "google.golang.org/grpc"
)
// Config stores service configuration
type Config struct {
Port int `mapstructure:"PORT"`
PrometheusPort int `mapstructure:"PROMETHEUS_PORT"`
Environment string `mapstructure:"ENVIRONMENT"`
}
var config Config
var logger *zap.Logger
func init() {
// From environment variables
config.Port, _ = strconv.Atoi(os.Getenv("PORT"))
config.PrometheusPort, _ = strconv.Atoi(os.Getenv("PROMETHEUS_PORT"))
// From command line
flag.IntVar(&config.Port, "port", 5555, "port to listen")
flag.IntVar(&config.PrometheusPort, "prometheusPort", 9090, "port to listen for prometheus")
flag.StringVar(&config.Environment, "environment", "development", "environment")
help := flag.Bool("help", false, "show help")
flag.Parse()
if *help {
flag.PrintDefaults()
os.Exit(0)
}
}
func main() {
reg := prometheus.NewRegistry()
httpServer := &http.Server{
Handler: promhttp.HandlerFor(reg, promhttp.HandlerOpts{}),
Addr: fmt.Sprintf(":%d", config.PrometheusPort),
}
// Create logger based on environment
var err error
if config.Environment == "production" {
logger, err = zap.NewProduction()
} else {
logger, err = zap.NewDevelopment(
zap.AddStacktrace(zap.ErrorLevel),
zap.AddStacktrace(zap.InfoLevel),
zap.AddStacktrace(zap.FatalLevel),
zap.AddStacktrace(zap.DebugLevel),
zap.AddStacktrace(zap.DPanicLevel),
)
}
if err != nil {
panic(err)
}
defer logger.Sync()
// Registering service with middlewares
server := grpc.NewServer(
grpc.UnaryInterceptor(
grpc_middleware.ChainUnaryServer(
grpc_prometheus.UnaryServerInterceptor,
grpc_zap.UnaryServerInterceptor(logger),
grpc_validator.UnaryServerInterceptor(),
// grpc_opentracing.UnaryServerInterceptor(),
),
),
grpc.StreamInterceptor(
grpc_middleware.ChainStreamServer(
grpc_prometheus.StreamServerInterceptor,
grpc_zap.StreamServerInterceptor(logger),
grpc_validator.StreamServerInterceptor(),
// grpc_opentracing.StreamServerInterceptor(),
),
),
)
// Initialize mail service
src.Initialize(logger)
// Initialize all metrics.
grpcMetrics := grpc_prometheus.NewServerMetrics()
grpcMetrics.InitializeMetrics(server)
// Start your http server for prometheus.
go func() {
logger.Info("starting server for prometheus", zap.Int("port", config.PrometheusPort))
if err := httpServer.ListenAndServe(); err != nil {
logger.Error("failed to start http server for prometheus", zap.Error(err))
}
}()
emailServer := src.EmailServer{}
src.RegisterEmailServiceServer(server, &emailServer)
healthpb.RegisterHealthServer(server, &emailServer)
// Listening to port
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port))
if err != nil {
logger.Fatal("cannot listen on port", zap.Int("port", config.Port), zap.Error(err))
}
defer listener.Close()
// Starting server
logger.Info("starting service", zap.Int("port", config.Port))
if err := server.Serve(listener); err != nil {
logger.Fatal("failed to start server", zap.Error(err))
}
}