-
Notifications
You must be signed in to change notification settings - Fork 15
/
cmd.go
209 lines (179 loc) · 5.49 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package cmd
import (
"context"
"errors"
"fmt"
"net"
"github.com/telkomindonesia/go-boilerplate/internal/httpserver"
"github.com/telkomindonesia/go-boilerplate/internal/kafka"
"github.com/telkomindonesia/go-boilerplate/internal/otelwrap"
"github.com/telkomindonesia/go-boilerplate/internal/postgres"
"github.com/telkomindonesia/go-boilerplate/internal/tenantservice"
"github.com/telkomindonesia/go-boilerplate/pkg/cmd"
"github.com/telkomindonesia/go-boilerplate/pkg/cmd/env"
"github.com/telkomindonesia/go-boilerplate/pkg/httpclient"
"github.com/telkomindonesia/go-boilerplate/pkg/log"
"github.com/telkomindonesia/go-boilerplate/pkg/log/loggable"
"github.com/telkomindonesia/go-boilerplate/pkg/tinkx"
"github.com/telkomindonesia/go-boilerplate/pkg/tlswrap"
)
type OptFunc func(*CMD) error
func WithEnvPrefix(p string) OptFunc {
return func(s *CMD) (err error) {
s.envPrefix = p
return
}
}
func WithoutDotEnv() OptFunc {
return func(s *CMD) (err error) {
s.dotenv = false
return
}
}
var _ log.Loggable = CMD{}
type CMD struct {
envPrefix string
dotenv bool
HTTPAddr string `env:"HTTP_LISTEN_ADDRESS,expand" envDefault:":8080" json:"http_listen_addr"`
PostgresUrl loggable.MaskedStringUserURL `env:"POSTGRES_URL,required,notEmpty,expand" json:"postgres_url"`
KafkaBrokers []string `env:"KAFKA_BROKERS,expand" json:"kafka_brokers"`
KafkaTopicOutbox string `env:"KAFKA_TOPIC_OUTBOX,expand" json:"kafka_topic_outbox"`
TenantServiceBaseUrl loggable.MaskedStringUserURL `env:"TENANT_SERVICE_BASE_URL,required,notEmpty,expand" json:"tenant_service_base_url"`
CMD *cmd.CMD `env:"-" json:"cmd"`
logger log.Logger
aead *tinkx.DerivableKeyset[tinkx.PrimitiveAEAD]
bidx *tinkx.DerivableKeyset[tinkx.PrimitiveBIDX]
hc httpclient.HTTPClient
tlsw *tlswrap.TLSWrap
canceler func(ctx context.Context) context.Context
loadOtel func(ctx context.Context) func()
h *httpserver.HTTPServer
p *postgres.Postgres
k *kafka.Kafka
ts *tenantservice.TenantService
closers []func(context.Context) error
}
func New(opts ...OptFunc) (c *CMD, err error) {
c = &CMD{
envPrefix: "PROFILE_",
dotenv: true,
}
for _, opt := range opts {
if err = opt(c); err != nil {
return
}
}
err = env.Load(c, env.Options{
Prefix: c.envPrefix,
DotEnv: c.dotenv,
})
if err != nil {
return nil, err
}
if err = c.initCMD(); err != nil {
return
}
if err = c.initKafka(); err != nil {
return
}
if err = c.initPostgres(); err != nil {
return
}
if err = c.initTenantService(); err != nil {
return
}
if err = c.initHTTPServer(); err != nil {
return
}
return
}
func (c *CMD) initCMD() (err error) {
c.CMD, err = cmd.New(cmd.WithEnv(c.envPrefix, c.dotenv))
if err != nil {
return fmt.Errorf("failed to instantiate cmd: %w", err)
}
c.loadOtel = c.CMD.LoadOtel
c.canceler = c.CMD.CancelOnExit
c.logger = c.CMD.Logger()
c.aead = c.CMD.AEADDerivableKeyset()
c.bidx = c.CMD.BIDXDerivableKeyset()
c.tlsw = c.CMD.TLSWrap()
c.hc = c.CMD.HTTPClient()
return
}
func (c *CMD) initKafka() (err error) {
if len(c.KafkaBrokers) == 0 {
return
}
c.k, err = kafka.New(
kafka.WithBrokers(c.KafkaBrokers),
)
if err != nil {
return fmt.Errorf("failed to instantiate kafka: %w", err)
}
if c.k != nil && c.KafkaTopicOutbox == "" {
return fmt.Errorf("invalid kafka outboox topic: %s", c.KafkaTopicOutbox)
}
c.closers = append(c.closers, c.k.Close)
return
}
func (c *CMD) initPostgres() (err error) {
opts := []postgres.OptFunc{
postgres.WithConnString(c.PostgresUrl.String()),
postgres.WithDerivableKeysets(c.aead, c.bidx),
postgres.WithLogger(c.logger.WithLog(log.String("logger-name", "postgres"))),
}
if c.k != nil {
opts = append(opts, postgres.WithOutboxCERelayFunc(c.k.OutboxCERelayFunc()))
}
c.p, err = postgres.New(opts...)
if err != nil {
return fmt.Errorf("failed to instantiate postges: %w", err)
}
c.closers = append(c.closers, c.p.Close)
return
}
func (c *CMD) initTenantService() (err error) {
c.ts, err = tenantservice.New(
tenantservice.WithBaseUrl(c.TenantServiceBaseUrl.String()),
tenantservice.WithHTTPClient(c.hc.Client),
tenantservice.WithLogger(c.logger.WithLog(log.String("logger-name", "tenant-service"))),
)
if err != nil {
return fmt.Errorf("failed to instantiate tenant service: %w", err)
}
return
}
func (c *CMD) initHTTPServer() (err error) {
l, err := net.Listen("tcp", c.HTTPAddr)
if err != nil {
return fmt.Errorf("failed to start listener: %w", err)
}
c.h, err = httpserver.New(
httpserver.WithListener(c.tlsw.Listener(l)),
httpserver.WithProfileRepository(otelwrap.NewProfileRepositoryWrapper(c.p, otelwrap.Tracer, "Postgres")),
httpserver.WithTenantRepository(otelwrap.NewTenantRepositoryWrapper(c.ts, otelwrap.Tracer, "TenantService")),
httpserver.WithLogger(c.logger.WithLog(log.String("logger-name", "http-server"))),
)
if err != nil {
return fmt.Errorf("failed to instantiate http server: %w", err)
}
c.closers = append(c.closers, c.h.Close)
return
}
func (c *CMD) Run(ctx context.Context) (err error) {
defer func() { c.logger.Error("error", log.Error("error", err)) }()
defer func() { err = c.close(ctx, err) }()
defer c.loadOtel(ctx)()
c.logger.Info("server starting", log.Any("server", c))
return c.h.Start(c.canceler(ctx))
}
func (c *CMD) close(ctx context.Context, err error) error {
for _, fn := range c.closers {
err = errors.Join(err, fn(ctx))
}
return err
}
func (c CMD) AsLog() any {
return loggable.AsLog(c)
}