/
serv.go
173 lines (145 loc) · 3.37 KB
/
serv.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package serv
import (
"context"
"embed"
"net"
"net/http"
"os"
"os/signal"
"strings"
"time"
"github.com/dosco/graphjin/serv/auth"
"github.com/go-chi/chi"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
serverName = "GraphJin"
defaultHP = "0.0.0.0:8080"
)
var (
// These variables are set using -ldflags
version string
commit string
date string
//go:embed web/build
webBuild embed.FS
)
func initConfigWatcher(s1 *Service) {
s := s1.Load().(*service)
if s.conf.Serv.Production {
return
}
go func() {
err := startConfigWatcher(s1)
if err != nil {
s.log.Fatalf("error in config file watcher: %s", err)
}
}()
}
func initHotDeployWatcher(s1 *Service) {
s := s1.Load().(*service)
go func() {
err := startHotDeployWatcher(s1)
if err != nil {
s.log.Fatalf("error in hot deploy watcher: %s", err)
}
}()
}
func startHTTP(s1 *Service) {
s := s1.Load().(*service)
r := chi.NewRouter()
routes, err := routesHandler(s1, r, s.namespace)
if err != nil {
s.log.Fatalf("error setting up routes: %s", err)
}
s.srv = &http.Server{
Addr: s.conf.hostPort,
Handler: routes,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
ReadHeaderTimeout: 10 * time.Second,
}
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
if err := s.srv.Shutdown(context.Background()); err != nil {
s.log.Warn("shutdown signal received")
}
close(idleConnsClosed)
}()
s.srv.RegisterOnShutdown(func() {
if s.closeFn != nil {
s.closeFn()
}
if s.db != nil {
s.db.Close()
}
s.log.Info("shutdown complete")
})
ver := version
dep := s.conf.name
if version == "" {
ver = "not-set"
}
/*
s.log.Infof("GraphJin started, version: %s, host-port: %s, app-name: %s, deployment: %s, env: %s",
ver, s.conf.hostPort, s.conf.AppName, dep, os.Getenv("GO_ENV"))
*/
fields := []zapcore.Field{
zap.String("version", ver),
zap.String("host-port", s.conf.hostPort),
zap.String("app-name", s.conf.AppName),
zap.String("env", os.Getenv("GO_ENV")),
zap.Bool("hot-deploy", s.conf.HotDeploy),
zap.Bool("production", s.conf.Core.Production),
zap.Bool("secrets-used", (s.conf.Serv.SecretsFile != "")),
}
if s.namespace.set {
fields = append(fields, zap.String("namespace", s.namespace.name))
}
if s.conf.HotDeploy {
fields = append(fields, zap.String("deployment-name", dep))
}
s.zlog.Info("GraphJin started", fields...)
l, err := net.Listen("tcp", s.conf.hostPort)
if err != nil {
s.log.Fatalf("failed to init port: %s", err)
}
// signal we are open for business.
s.state = servListening
if err := s.srv.Serve(l); err != http.ErrServerClosed {
s.log.Fatalf("failed to start: %s", err)
}
<-idleConnsClosed
}
func setServerHeader(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", serverName)
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func findAuth(s *service, name string) (auth.Auth, bool) {
for _, a := range s.conf.Auths {
if strings.EqualFold(a.Name, name) {
return a, true
}
}
return auth.Auth{}, false
}
type BuildInfo struct {
Version string
Commit string
Date string
}
func GetBuildInfo() BuildInfo {
return BuildInfo{
Version: version,
Commit: commit,
Date: date,
}
}