-
Notifications
You must be signed in to change notification settings - Fork 23
/
server.go
151 lines (135 loc) · 4.47 KB
/
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package server // import "pathwar.land/server"
import (
"context"
"net"
"net/http"
"strings"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/gogo/gateway"
"github.com/gogo/protobuf/gogoproto"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"pathwar.land/sql"
)
var _ = gogoproto.IsStdTime
func server(opts *serverOptions) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errs := make(chan error)
go func() { errs <- errors.Wrap(startGRPCServer(ctx, opts), "gRPC server error") }()
go func() { errs <- errors.Wrap(startHTTPServer(ctx, opts), "HTTP server error") }()
return <-errs
}
func startHTTPServer(ctx context.Context, opts *serverOptions) error {
// configure gateway mux
gwmux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, &gateway.JSONPb{
EmitDefaults: false,
Indent: " ",
OrigName: true,
}),
runtime.WithProtoErrorHandler(runtime.DefaultHTTPProtoErrorHandler),
)
grpcOpts := []grpc.DialOption{grpc.WithInsecure()}
if err := RegisterServerHandlerFromEndpoint(ctx, gwmux, opts.GRPCBind, grpcOpts); err != nil {
return err
}
// configure chi router
r := chi.NewRouter()
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, // FIXME: if production, should be the production portal
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
})
r.Use(cors.Handler)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(5 * time.Second))
r.Use(middleware.RealIP)
r.Use(middleware.RequestID)
// gateway mux
r.Mount(opts.APIPrefix, http.StripPrefix(strings.TrimRight(opts.APIPrefix, "/"), gwmux))
// static files
if opts.WebDir != "" {
fs := http.StripPrefix("/", http.FileServer(http.Dir(opts.WebDir)))
r.Get("/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
}))
}
// start HTTP server
zap.L().Info("starting HTTP server", zap.String("bind", opts.HTTPBind))
return http.ListenAndServe(opts.HTTPBind, r)
}
func startGRPCServer(ctx context.Context, opts *serverOptions) error {
listener, err := net.Listen("tcp", opts.GRPCBind)
if err != nil {
return errors.Wrap(err, "failed to listen")
}
defer func() {
if err := listener.Close(); err != nil {
zap.L().Error(
"failed to close listener",
zap.String("address", opts.GRPCBind),
zap.Error(err),
)
}
}()
grpcLogger := zap.L().Named("grpc")
serverStreamOpts := []grpc.StreamServerInterceptor{
grpc_recovery.StreamServerInterceptor(),
grpc_auth.StreamServerInterceptor(authFunc),
grpc_ctxtags.StreamServerInterceptor(),
grpc_zap.StreamServerInterceptor(grpcLogger),
grpc_recovery.StreamServerInterceptor(),
}
serverUnaryOpts := []grpc.UnaryServerInterceptor{
grpc_recovery.UnaryServerInterceptor(),
grpc_auth.UnaryServerInterceptor(authFunc),
grpc_ctxtags.UnaryServerInterceptor(),
grpc_zap.UnaryServerInterceptor(grpcLogger),
grpc_recovery.UnaryServerInterceptor(),
}
grpcServer := grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(serverStreamOpts...)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(serverUnaryOpts...)),
)
svc, err := newSvc(opts)
if err != nil {
return errors.Wrap(err, "failed to initialize service")
}
RegisterServerServer(grpcServer, svc)
if opts.WithReflection {
reflection.Register(grpcServer)
}
go func() {
defer grpcServer.GracefulStop()
<-ctx.Done()
}()
zap.L().Info("starting gRPC server", zap.String("bind", opts.GRPCBind))
return grpcServer.Serve(listener)
}
func newSvc(opts *serverOptions) (*svc, error) {
db, err := sql.FromOpts(&opts.SQL)
if err != nil {
return nil, errors.Wrap(err, "failed to initialize database")
}
return &svc{
db: db,
client: opts.Client,
startedAt: time.Now(),
}, nil
}