-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathinits.go
135 lines (119 loc) · 3.72 KB
/
inits.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/naughtygopher/errors"
"github.com/naughtygopher/proberesponder"
"github.com/naughtygopher/proberesponder/extensions/depprober"
proberespHTTP "github.com/naughtygopher/proberesponder/extensions/http"
"github.com/naughtygopher/webgo/v7"
"github.com/naughtygopher/goapp/cmd/server/grpc"
xhttp "github.com/naughtygopher/goapp/cmd/server/http"
"github.com/naughtygopher/goapp/internal/api"
"github.com/naughtygopher/goapp/internal/configs"
"github.com/naughtygopher/goapp/internal/pkg/apm"
"github.com/naughtygopher/goapp/internal/pkg/logger"
"github.com/naughtygopher/goapp/internal/pkg/postgres"
"github.com/naughtygopher/goapp/internal/users"
)
var now = time.Now()
func startAPM(ctx context.Context, cfg *configs.Configs) *apm.APM {
ap, err := apm.New(ctx, &apm.Options{
Debug: cfg.Environment == configs.EnvLocal,
Environment: cfg.Environment.String(),
ServiceName: cfg.AppName,
ServiceVersion: cfg.AppVersion,
TracesSampleRate: 50.00,
UseStdOut: cfg.Environment == configs.EnvLocal,
})
if err != nil {
panic(errors.Wrap(err, "failed to start APM"))
}
return ap
}
func startServers(svr api.Server, cfgs *configs.Configs, fatalErr chan<- error) (*xhttp.HTTP, *grpc.GRPC) {
hcfg, _ := cfgs.HTTP()
hserver, err := xhttp.NewService(hcfg, svr)
if err != nil {
fatalErr <- errors.Wrap(err, "failed to initialize HTTP server")
}
go func() {
defer func() {
rec := recover()
if rec != nil {
fatalErr <- errors.New(fmt.Sprintf("%+v", rec))
}
}()
err = hserver.Start()
if err != nil {
fatalErr <- errors.Wrap(err, "failed to start HTTP server")
}
}()
return hserver, nil
}
func healthResponseHandler(ps *proberesponder.ProbeResponder) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
payload := map[string]any{
"env": "testing",
"version": "v0.1.0",
"commit": "<git commit hash>",
"status": "all systems up and running",
"startedAt": now.String(),
"releasedOn": now.String(),
}
for key, value := range ps.HealthResponse() {
payload[key] = value
}
b, _ := json.Marshal(payload)
w.Header().Add(webgo.HeaderContentType, webgo.JSONContentType)
_, _ = w.Write(b)
}
}
func startHealthResponder(ctx context.Context, ps *proberesponder.ProbeResponder, fatalErr chan<- error) (*http.Server, error) {
port := uint32(2000)
srv := proberespHTTP.Server(
ps, "", uint16(port),
proberespHTTP.Handler{
Method: http.MethodGet,
Path: "/-/health",
Handler: healthResponseHandler(ps),
},
)
go func() {
defer logger.Info(ctx, fmt.Sprintf("[http/healthresponder] :%d shutdown complete", port))
logger.Info(ctx, fmt.Sprintf("[http/healthresponder] listening on :%d", port))
fatalErr <- srv.ListenAndServe()
}()
return srv, nil
}
func start(
ctx context.Context,
probestatus *proberesponder.ProbeResponder,
cfgs *configs.Configs,
fatalErr chan<- error,
) (hserver *xhttp.HTTP, gserver *grpc.GRPC) {
_ = ctx
pqdriver, err := postgres.NewPool(cfgs.Postgres())
if err != nil {
panic(errors.Wrap(err))
}
depprober.Start(time.Minute, probestatus, &depprober.Probe{
ID: "postgres",
AffectedStatuses: []proberesponder.Statuskey{proberesponder.StatusLive, proberesponder.StatusReady},
Checker: depprober.CheckerFunc(func(ctx context.Context) error {
err := pqdriver.Ping(ctx)
if err != nil {
return errors.Wrap(err, "postgres ping failed")
}
return nil
}),
})
userPGstore := users.NewPostgresStore(pqdriver, cfgs.UserPostgresTable())
userSvc := users.NewService(userPGstore)
svrAPIs := api.NewServer(userSvc, nil)
hserver, gserver = startServers(svrAPIs, cfgs, fatalErr)
return
}