@@ -23,7 +23,7 @@ import (
23
23
"github.com/hay-kot/homebox/backend/internal/sys/config"
24
24
"github.com/hay-kot/homebox/backend/internal/web/mid"
25
25
"github.com/hay-kot/httpkit/errchain"
26
- "github.com/hay-kot/httpkit/server "
26
+ "github.com/hay-kot/httpkit/graceful "
27
27
"github.com/rs/zerolog"
28
28
"github.com/rs/zerolog/log"
29
29
"github.com/rs/zerolog/pkgerrors"
@@ -178,41 +178,54 @@ func run(cfg *config.Config) error {
178
178
middleware .StripSlashes ,
179
179
)
180
180
181
- chain := errchain .New (mid .Errors (app . server , logger ))
181
+ chain := errchain .New (mid .Errors (logger ))
182
182
183
183
app .mountRoutes (router , chain , app .repos )
184
184
185
- app .server = server .NewServer (
186
- server .WithHost (app .conf .Web .Host ),
187
- server .WithPort (app .conf .Web .Port ),
188
- server .WithReadTimeout (app .conf .Web .ReadTimeout ),
189
- server .WithWriteTimeout (app .conf .Web .WriteTimeout ),
190
- server .WithIdleTimeout (app .conf .Web .IdleTimeout ),
191
- )
192
- log .Info ().Msgf ("Starting HTTP Server on %s:%s" , app .server .Host , app .server .Port )
185
+ runner := graceful .NewRunner ()
186
+
187
+ runner .AddFunc ("server" , func (ctx context.Context ) error {
188
+ httpserver := http.Server {
189
+ Addr : fmt .Sprintf ("%s:%s" , cfg .Web .Host , cfg .Web .Port ),
190
+ Handler : router ,
191
+ ReadTimeout : cfg .Web .ReadTimeout ,
192
+ WriteTimeout : cfg .Web .WriteTimeout ,
193
+ IdleTimeout : cfg .Web .IdleTimeout ,
194
+ }
195
+
196
+ go func () {
197
+ <- ctx .Done ()
198
+ _ = httpserver .Shutdown (context .Background ())
199
+ }()
200
+
201
+ log .Info ().Msgf ("Server is running on %s:%s" , cfg .Web .Host , cfg .Web .Port )
202
+ return httpserver .ListenAndServe ()
203
+ })
193
204
194
205
// =========================================================================
195
206
// Start Reoccurring Tasks
196
207
197
- go app .bus .Run ( )
208
+ runner . AddFunc ( "eventbus" , app .bus .Run )
198
209
199
- go app . startBgTask ( time .Duration (24 )* time .Hour , func () {
200
- _ , err := app .repos .AuthTokens .PurgeExpiredTokens (context . Background () )
210
+ runner . AddPlugin ( NewTask ( "purge-tokens" , time .Duration (24 )* time .Hour , func (ctx context. Context ) {
211
+ _ , err := app .repos .AuthTokens .PurgeExpiredTokens (ctx )
201
212
if err != nil {
202
213
log .Error ().
203
214
Err (err ).
204
215
Msg ("failed to purge expired tokens" )
205
216
}
206
- })
207
- go app .startBgTask (time .Duration (24 )* time .Hour , func () {
208
- _ , err := app .repos .Groups .InvitationPurge (context .Background ())
217
+ }))
218
+
219
+ runner .AddPlugin (NewTask ("purge-invitations" , time .Duration (24 )* time .Hour , func (ctx context.Context ) {
220
+ _ , err := app .repos .Groups .InvitationPurge (ctx )
209
221
if err != nil {
210
222
log .Error ().
211
223
Err (err ).
212
224
Msg ("failed to purge expired invitations" )
213
225
}
214
- })
215
- go app .startBgTask (time .Duration (1 )* time .Hour , func () {
226
+ }))
227
+
228
+ runner .AddPlugin (NewTask ("send-notifications" , time .Duration (1 )* time .Hour , func (ctx context.Context ) {
216
229
now := time .Now ()
217
230
218
231
if now .Hour () == 8 {
@@ -224,7 +237,7 @@ func run(cfg *config.Config) error {
224
237
Msg ("failed to send notifiers" )
225
238
}
226
239
}
227
- })
240
+ }))
228
241
229
242
// TODO: Remove through external API that does setup
230
243
if cfg .Demo {
@@ -233,13 +246,24 @@ func run(cfg *config.Config) error {
233
246
}
234
247
235
248
if cfg .Debug .Enabled {
236
- debugrouter := app .debugRouter ()
237
- go func () {
238
- if err := http .ListenAndServe (":" + cfg .Debug .Port , debugrouter ); err != nil {
239
- log .Fatal ().Err (err ).Msg ("failed to start debug server" )
249
+ runner .AddFunc ("debug" , func (ctx context.Context ) error {
250
+ debugserver := http.Server {
251
+ Addr : fmt .Sprintf ("%s:%s" , cfg .Web .Host , cfg .Debug .Port ),
252
+ Handler : app .debugRouter (),
253
+ ReadTimeout : cfg .Web .ReadTimeout ,
254
+ WriteTimeout : cfg .Web .WriteTimeout ,
255
+ IdleTimeout : cfg .Web .IdleTimeout ,
240
256
}
241
- }()
257
+
258
+ go func () {
259
+ <- ctx .Done ()
260
+ _ = debugserver .Shutdown (context .Background ())
261
+ }()
262
+
263
+ log .Info ().Msgf ("Debug server is running on %s:%s" , cfg .Web .Host , cfg .Debug .Port )
264
+ return debugserver .ListenAndServe ()
265
+ })
242
266
}
243
267
244
- return app . server . Start (router )
268
+ return runner . Start (context . Background () )
245
269
}
0 commit comments