-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
152 lines (135 loc) · 3.81 KB
/
cmd.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
package greenlight
import (
"context"
"errors"
"expvar"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
"github.com/rwx-yxu/greenlight/app"
"github.com/rwx-yxu/greenlight/database"
"github.com/rwx-yxu/greenlight/internal/jsonlog"
"github.com/rwx-yxu/greenlight/routes"
Z "github.com/rwxrob/bonzai/z"
"github.com/rwxrob/conf"
"github.com/rwxrob/help"
"gopkg.in/yaml.v3"
)
func init() {
Z.Conf.SoftInit()
}
var Cmd = &Z.Cmd{
Name: `greenlight`,
Version: `v0.0.1`,
Copyright: `Copyright 2023 Yongle Xu`,
License: `Apache-2.0`,
Site: `yonglexu.dev`,
Source: `git@github.com:rwx-yxu/greenlight.git`,
Issues: `github.com/rwx-yxu/greenlight/issues`,
Commands: []*Z.Cmd{
StartCmd,
// standard external branch imports (see rwxrob/{help,conf,vars})
help.Cmd, conf.Cmd,
},
Summary: help.S(_greenlight),
Description: help.D(_greenlight),
}
var StartCmd = &Z.Cmd{
Name: `start`,
Aliases: []string{`s`},
Commands: []*Z.Cmd{help.Cmd},
Summary: help.S(_start),
Description: help.D(_start),
Call: func(x *Z.Cmd, _ ...string) error {
var config app.Config
c, err := x.Caller.C("")
if err != nil {
return errors.New("Config has not been initialised")
}
err = yaml.Unmarshal([]byte(c), &config)
if err != nil {
return err
}
config.CORS.TrustedOrigins = strings.Fields(config.CORS.Origins)
logger := jsonlog.New(os.Stdout, jsonlog.LevelInfo)
db, err := database.OpenPostgres(config)
if err != nil {
return err
}
logger.PrintInfo("database connection pool established", nil)
defer db.Close()
config.Server.Version = x.Caller.GetVersion()
expvar.NewString("version").Set(config.Server.Version)
// Publish the number of active goroutines.
expvar.Publish("goroutines", expvar.Func(func() any {
return runtime.NumGoroutine()
}))
// Publish the database connection pool statistics.
expvar.Publish("database", expvar.Func(func() any {
return db.Stats()
}))
// Publish the current Unix timestamp.
expvar.Publish("timestamp", expvar.Func(func() any {
return time.Now().Unix()
}))
app := app.NewApp(config, db, logger)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", config.Server.Port),
Handler: routes.NewRouter(*app),
ErrorLog: log.New(logger, "", 0),
IdleTimeout: time.Minute,
ReadTimeout: 10 * time.Second,
WriteTimeout: 30 * time.Second,
}
shutdownError := make(chan error)
go func() {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
s := <-quit
app.Logger.PrintInfo("caught signal", map[string]string{
"signal": s.String(),
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Call Shutdown() on the server like before, but now we only send on the
// shutdownError channel if it returns an error.
err := srv.Shutdown(ctx)
if err != nil {
shutdownError <- err
}
// Log a message to say that we're waiting for any background goroutines to
// complete their tasks.
app.Logger.PrintInfo("completing background tasks", map[string]string{
"addr": srv.Addr,
})
// Call Wait() to block until our WaitGroup counter is zero --- essentially
// blocking until the background goroutines have finished. Then we return nil on
// the shutdownError channel, to indicate that the shutdown completed without
// any issues.
app.WG.Wait()
shutdownError <- nil
}()
app.Logger.PrintInfo("starting server", map[string]string{
"addr": srv.Addr,
"env": app.Config.Server.Env,
})
err = srv.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
return err
}
err = <-shutdownError
if err != nil {
return err
}
app.Logger.PrintInfo("stopped server", map[string]string{
"addr": srv.Addr,
})
return nil
},
}