-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
250 lines (219 loc) · 5.87 KB
/
service.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package http
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"sync"
"time"
"github.com/andrescosta/goico/pkg/env"
"github.com/andrescosta/goico/pkg/service"
"github.com/andrescosta/goico/pkg/service/obs"
"github.com/gorilla/mux"
"github.com/rs/zerolog"
)
type Service struct {
service *service.Service
server *http.Server
healthCheckFunc HealthChkFn
pool *sync.Pool
}
type healthStatus struct {
status string
details map[string]string
}
type initRoutesFn = func(context.Context, *mux.Router) error
type HealthChkFn = func(context.Context) (map[string]string, error)
type ServiceOptions struct {
extras *extrasOptions
service *service.Service
}
type RouterOptions struct {
addr *string
extras *extrasOptions
ctx context.Context
name string
initRoutesFn initRoutesFn
}
type extrasOptions struct {
healthChkFn HealthChkFn
}
func NewWithWouter(opts ...func(*RouterOptions)) (*Service, error) {
svc := &Service{}
svc.initWithDefaults()
opt := &RouterOptions{
extras: &extrasOptions{},
ctx: context.Background(),
initRoutesFn: func(ctx context.Context, r *mux.Router) error { return nil },
addr: nil,
name: "",
}
for _, o := range opts {
o(opt)
}
//
s, err := service.New(
service.WithName(opt.name),
service.WithAddr(opt.addr),
service.WithContext(opt.ctx),
service.WithKind("rest"),
)
if err != nil {
return nil, err
}
svc.service = s
// Mux Router config
router := svc.getRouter()
//// routes initialization
if err := opt.initRoutesFn(svc.service.Ctx, router); err != nil {
return nil, err
}
// add health check handler if provided
if opt.extras.healthChkFn != nil {
svc.initHealthCheckFn(opt.extras.healthChkFn, router)
}
return svc, nil
}
func NewWithServiceContainer(opts ...func(*ServiceOptions)) (*Service, error) {
svc := &Service{}
svc.initWithDefaults()
opt := &ServiceOptions{
extras: &extrasOptions{},
service: nil,
}
for _, o := range opts {
o(opt)
}
svc.service = opt.service
// Mux Router config
router := svc.getRouter()
// add health check handler if provided
svc.initHealthCheckFn(opt.extras.healthChkFn, router)
return svc, nil
}
func (s *Service) Serve() error {
logger := zerolog.Ctx(s.service.Ctx)
logger.Info().Msgf("Starting process %d ", os.Getpid())
if s.service.Addr == nil {
return errors.New("the address was not configured")
}
listener, err := net.Listen("tcp", *s.service.Addr)
s.server.BaseContext = func(l net.Listener) context.Context { return s.service.Ctx }
if err != nil {
return fmt.Errorf("net.Listen: failed to create listener on %s: %w", *s.service.Addr, err)
}
errCh := make(chan error, 1)
go func() {
<-s.service.Ctx.Done()
logger.Debug().Msg("HTTP service: context closed")
shutdownCtx, done := context.WithTimeout(context.Background(), 5*time.Second)
defer done()
logger.Debug().Msg("HTTP service: shutting down")
errCh <- s.server.Shutdown(shutdownCtx)
}()
logger.Debug().Msgf("HTTP service: started on %s", *s.service.Addr)
s.service.Started()
if err := s.server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("http.Serve: failed to serve: %w", err)
}
logger.Debug().Msg("HTTP service: serving stopped")
if err := <-errCh; err != nil {
return fmt.Errorf("http.Shutdown: failed to shutdown server: %w", err)
}
logger.Info().Msgf("Process %d ended ", os.Getpid())
return nil
}
func (s *Service) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/JSON")
status := &healthStatus{}
m, err := s.healthCheckFunc(r.Context())
status.details = m
var statusCode int
if err != nil {
status.status = "error"
statusCode = http.StatusInternalServerError
} else {
status.status = "alive"
statusCode = http.StatusOK
}
b := s.pool.Get().(*bytes.Buffer)
b.Reset()
WriteJSONBody(b, status, statusCode, `{error:"error getting health check status"}`, w)
}
func (s *Service) metadataHandler(w http.ResponseWriter, _ *http.Request) {
b := s.pool.Get().(*bytes.Buffer)
b.Reset()
WriteJSONBody(b, s.service.Metadata(), http.StatusOK, `{error:"error getting metadata"}`, w)
}
// Setters
func WithInitRoutesFn(i initRoutesFn) func(*RouterOptions) {
return func(w *RouterOptions) {
w.initRoutesFn = i
}
}
func WithAddr(a *string) func(*RouterOptions) {
return func(r *RouterOptions) {
r.addr = a
}
}
func WithName(n string) func(*RouterOptions) {
return func(r *RouterOptions) {
r.name = n
}
}
func WithContext(ctx context.Context) func(*RouterOptions) {
return func(r *RouterOptions) {
r.ctx = ctx
}
}
func WithHealthCheck[T *ServiceOptions | *RouterOptions](healthChkFn HealthChkFn) func(T) {
return func(t T) {
switch v := any(t).(type) {
case *ServiceOptions:
v.extras.healthChkFn = healthChkFn
case *RouterOptions:
v.extras.healthChkFn = healthChkFn
}
}
}
func WithContainer(svc *service.Service) func(*ServiceOptions) {
return func(opts *ServiceOptions) {
opts.service = svc
}
}
func (s *Service) initHealthCheckFn(h HealthChkFn, r *mux.Router) {
s.healthCheckFunc = h
r.HandleFunc("/health", s.healthCheckHandler)
}
func (s *Service) setServer(r *mux.Router) {
s.server = &http.Server{
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r,
}
}
func (s *Service) getRouter() (r *mux.Router) {
// Mux Router config
r = mux.NewRouter()
//// setting middlewares
rf := RecoveryFunc{StackLevel: StackLevelFullStack}
r.Use(rf.TryToRecover())
r.Use(obs.GetLoggingMiddleware)
s.service.OtelProvider.InstrRouter(s.service.Name, r)
if env.AsBool("metadata.enabled", false) {
r.HandleFunc("/meta", s.metadataHandler)
}
s.setServer(r)
return
}
func (s *Service) initWithDefaults() {
s.pool = &sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 1024))
},
}
}