-
Notifications
You must be signed in to change notification settings - Fork 1
/
disneyland_server.go
119 lines (102 loc) · 3.11 KB
/
disneyland_server.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
package main
import (
"crypto/tls"
"crypto/x509"
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/auth"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/sirupsen/logrus"
"github.com/skygrid/disneyland/disneyland"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/reflection"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net"
"os"
)
type DisneylandServerConfig struct {
ServerCert string `yaml:"server_cert"`
ServerKey string `yaml:"server_key"`
CACert string `yaml:"ca_cert"`
ListenOn string `yaml:"listen_on"`
DatabaseURI string `yaml:"db_uri"`
}
const maxMessageSizeInBytes = 5 * 1024 * 1024 * 1024
var Config *DisneylandServerConfig
func getTransportCredentials() (*credentials.TransportCredentials, error) {
peerCert, err := tls.LoadX509KeyPair(Config.ServerCert, Config.ServerKey)
if err != nil {
return nil, err
}
caCert, err := ioutil.ReadFile(Config.CACert)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tc := credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{peerCert},
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
})
return &tc, nil
}
func main() {
Config = &DisneylandServerConfig{}
config_path := os.Getenv("DISNEYLAND_CONFIG")
content, err := ioutil.ReadFile(config_path)
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
err = yaml.Unmarshal([]byte(content), Config)
if err != nil {
log.Fatalf("Error parsing config: %v", err)
}
storage, err := disneyland.NewDisneylandStorage(Config.DatabaseURI)
if err != nil {
log.Fatal(err)
}
lis, err := net.Listen("tcp", Config.ListenOn)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
server := &disneyland.Server{
Storage: storage,
}
logger := &logrus.Logger{
Out: os.Stderr,
Formatter: new(logrus.TextFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.DebugLevel,
}
logrusEntry := logrus.NewEntry(logger)
transportCredentials, err := getTransportCredentials()
if err != nil {
log.Fatalf("failed to get credentials: %v", err)
}
s := grpc.NewServer(
grpc.MaxRecvMsgSize(maxMessageSizeInBytes),
grpc.MaxSendMsgSize(maxMessageSizeInBytes),
grpc.Creds(*transportCredentials),
grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.UnaryServerInterceptor(logrusEntry),
grpc_auth.UnaryServerInterceptor(nil),
),
grpc_middleware.WithStreamServerChain(
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_logrus.StreamServerInterceptor(logrusEntry),
grpc_auth.StreamServerInterceptor(nil),
),
)
disneyland.RegisterDisneylandServer(s, server)
// Register reflection service on gRPC server.
reflection.Register(s)
log.Print("Server started")
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}