-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (156 loc) · 5.03 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
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
174
175
176
177
178
179
180
package main
import (
"context"
"fmt"
"net"
"net/url"
"os"
"os/signal"
"sync"
"syscall"
arg "github.com/alexflint/go-arg"
goahttp "goa.design/goa/v3/http"
)
const (
loggerPrefix = "[serverapi]"
cookieName = "session_id"
servicePort = ":5678"
)
// Define command line flags, add any other flag required to configure the service.
type args struct {
Local bool `arg:"--local,env:LOCAL" default:"false" help:"local mode"`
LogLevel string `arg:"--log-level,env:LOG_LEVEL" redact:"false" default:"info" help:"logging level" placeholder:"debug|info|warn|error"`
Domain string `arg:"--domain,env:DOMAIN" default:"" help:"Host domain name (overrides host domain specified in service design)"`
Host string `arg:"--host,env:HOST" default:"localhost" help:"Server host (valid values: localhost)"`
HttpPort string `arg:"--http-port,env:HTTP_PORT" default:"" help:"HTTP port (overrides host HTTP port specified in service design)"`
Secure bool `arg:"--secure,env:SECURE" default:"false" help:"Use secure scheme"`
DebugRequest bool `arg:"--debug-request,env:DEBUG_REQUEST" default:"false" help:"Verbose request logging"`
}
func (args) Version() string {
return fmt.Sprintf("Version: %v", build.Version)
}
func (args) Description() string {
return "API service"
}
func (args) Epilogue() string {
return "For more information check the README"
}
func main() {
var args args
arg.MustParse(&args)
// init session manager
sm := serverapi.NewSessionManager(cookieName, pool, args.Dev)
sessionStore := serverapi.NewSessionStore(db, sm)
// init session middleware
applySessionMiddleware := func(server goahttp.Server) {
server.Use(sm.LoadAndSave)
}
// init auth middleware
applyAuthMiddleware := func(server goahttp.Server) {
errResp := []byte(`{"Code":"` + serverapi.ExpiredCode.CodeStr() + `"}`)
getUserSession := func(ctx context.Context) bool {
return !sessionStore.GetVerified(ctx).Empty()
}
applySessionMiddleware(server)
}
// init config
c := serv.Config{
ExternalURL: "https://" + args.Domain,
DevMode: args.Dev,
Domain: args.Domain,
}
if c.Local {
c.ExternalURL = "http://" + domain.Localhost + ":3000"
c.Domain = domain.Localhost
}
serverPublicSvc := serverapi.NewServerPublic(sessionStore)
devPublicSvc := serverapi.NewDevPublic()
// Wrap the services in endpoints that can be invoked from other services
// potentially running in different processes.
var endpoints struct {
Server *server.Endpoints
ServerMiddleware func(server goahttp.Server)
DevPublic *devpublic.Endpoints
}
{
endpoints.Server = server.NewEndpoints(&serverSvc)
endpoints.ServerMiddleware = applyAuthMiddleware
endpoints.DevPublic = devpublic.NewEndpoints(devPublicSvc)
}
// Create channel used by both the signal handler and server goroutines
// to notify the main goroutine when to stop the server.
errc := make(chan error)
// Setup interrupt handler. This optional step configures the process so
// that SIGINT and SIGTERM signals cause the services to stop gracefully.
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-c)
}()
// set background ctx
ctx, cancel := context.WithCancel(context.Background())
// Start the servers and send errors (if any) to the error channel.
var waitShutdown sync.WaitGroup
var u *url.URL
switch args.Host {
case domain.Localhost:
{
addr := "http://" + domain.Localhost + servicePort
var err error
u, err = url.Parse(addr)
if err != nil {
logger.Fatalf("%s invalid URL %#v: %s", loggerPrefix, addr, err)
}
if args.Secure {
u.Scheme = domain.ProtocolHttps
}
if args.Domain != "" {
u.Host = args.Domain
}
if args.HttpPort != "" {
h, _, err := net.SplitHostPort(u.Host)
if err != nil {
logger.Fatalf("%s invalid URL %#v: %s", loggerPrefix, u.Host, err)
}
u.Host = net.JoinHostPort(h, args.HttpPort)
} else if u.Port() == "" {
u.Host = net.JoinHostPort(u.Host, "80")
}
}
default:
var err error
u, err = url.Parse(args.Host)
if err != nil {
logger.Fatalf("%s invalid URL %#v: %s", loggerPrefix, args.Host, err)
}
// TODO: expose a k8s service
u.Host = servicePort
}
handleHTTPServer(ctx, u, endpoints, &waitShutdown, errc, logger, args.DebugRequest)
mainReportError(func() error {
// Wait for signal.
err := <-errc
logger.Infof("%s exiting (%v)", loggerPrefix, err)
// Send cancellation signal to the goroutines.
cancel()
waitShutdown.Wait()
logger.Infof("%s exited", loggerPrefix)
// SIGTERM is the normal k8s deployment
if err.Error() == "terminated" {
return nil
}
return err
})
}
// MainReportError is only designed to be used in a top-level main function
// It will crash the program.
func mainReportError(mainError func() error) {
if err := recovery.Call(mainError); err != nil {
handleAndPanic(err)
}
}
func handleAndPanic(err error) {
slog.Error(fmt.Sprintf("%+v", err))
// sentry.CaptureException(err)
panic(fmt.Sprintf("%+v", err))
}