mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-02 07:40:28 +00:00
refactor out last httpkit changes
This commit is contained in:
parent
b78bdedab6
commit
068af273fa
5 changed files with 58 additions and 39 deletions
|
@ -1,22 +1,18 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/hay-kot/homebox/backend/internal/core/services"
|
"github.com/hay-kot/homebox/backend/internal/core/services"
|
||||||
"github.com/hay-kot/homebox/backend/internal/core/services/reporting/eventbus"
|
"github.com/hay-kot/homebox/backend/internal/core/services/reporting/eventbus"
|
||||||
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
||||||
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
||||||
"github.com/hay-kot/homebox/backend/internal/sys/config"
|
"github.com/hay-kot/homebox/backend/internal/sys/config"
|
||||||
"github.com/hay-kot/homebox/backend/pkgs/mailer"
|
"github.com/hay-kot/homebox/backend/pkgs/mailer"
|
||||||
"github.com/hay-kot/httpkit/server"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type app struct {
|
type app struct {
|
||||||
conf *config.Config
|
conf *config.Config
|
||||||
mailer mailer.Mailer
|
mailer mailer.Mailer
|
||||||
db *ent.Client
|
db *ent.Client
|
||||||
server *server.Server
|
|
||||||
repos *repo.AllRepos
|
repos *repo.AllRepos
|
||||||
services *services.AllServices
|
services *services.AllServices
|
||||||
bus *eventbus.EventBus
|
bus *eventbus.EventBus
|
||||||
|
@ -37,13 +33,3 @@ func new(conf *config.Config) *app {
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *app) startBgTask(t time.Duration, fn func()) {
|
|
||||||
timer := time.NewTimer(t)
|
|
||||||
|
|
||||||
for {
|
|
||||||
timer.Reset(t)
|
|
||||||
a.server.Background(fn)
|
|
||||||
<-timer.C
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
37
backend/app/api/bgrunner.go
Normal file
37
backend/app/api/bgrunner.go
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BackgroundTask struct {
|
||||||
|
name string
|
||||||
|
Interval time.Duration
|
||||||
|
Fn func(context.Context)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bg *BackgroundTask) Name() string {
|
||||||
|
return bg.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTask(name string, interval time.Duration, fn func(context.Context)) *BackgroundTask {
|
||||||
|
return &BackgroundTask{
|
||||||
|
Interval: interval,
|
||||||
|
Fn: fn,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tsk *BackgroundTask) Start(ctx context.Context) error {
|
||||||
|
timer := time.NewTimer(tsk.Interval)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil
|
||||||
|
case <-timer.C:
|
||||||
|
timer.Reset(tsk.Interval)
|
||||||
|
tsk.Fn(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -178,7 +178,7 @@ func run(cfg *config.Config) error {
|
||||||
middleware.StripSlashes,
|
middleware.StripSlashes,
|
||||||
)
|
)
|
||||||
|
|
||||||
chain := errchain.New(mid.Errors(app.server, logger))
|
chain := errchain.New(mid.Errors(logger))
|
||||||
|
|
||||||
app.mountRoutes(router, chain, app.repos)
|
app.mountRoutes(router, chain, app.repos)
|
||||||
|
|
||||||
|
@ -186,8 +186,8 @@ func run(cfg *config.Config) error {
|
||||||
|
|
||||||
runner.AddFunc("server", func(ctx context.Context) error {
|
runner.AddFunc("server", func(ctx context.Context) error {
|
||||||
httpserver := http.Server{
|
httpserver := http.Server{
|
||||||
Addr: fmt.Sprintf("%s:%s", app.server.Host, app.server.Port),
|
Addr: fmt.Sprintf("%s:%s", cfg.Web.Host, cfg.Web.Port),
|
||||||
Handler: router,
|
Handler: router,
|
||||||
ReadTimeout: cfg.Web.ReadTimeout,
|
ReadTimeout: cfg.Web.ReadTimeout,
|
||||||
WriteTimeout: cfg.Web.WriteTimeout,
|
WriteTimeout: cfg.Web.WriteTimeout,
|
||||||
IdleTimeout: cfg.Web.IdleTimeout,
|
IdleTimeout: cfg.Web.IdleTimeout,
|
||||||
|
@ -195,36 +195,37 @@ func run(cfg *config.Config) error {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
httpserver.Shutdown(context.Background())
|
_ = httpserver.Shutdown(context.Background())
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
log.Info().Msgf("Server is running on %s:%s", cfg.Web.Host, cfg.Web.Port)
|
||||||
return httpserver.ListenAndServe()
|
return httpserver.ListenAndServe()
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Info().Msgf("Starting HTTP Server on %s:%s", app.server.Host, app.server.Port)
|
|
||||||
|
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
// Start Reoccurring Tasks
|
// Start Reoccurring Tasks
|
||||||
|
|
||||||
runner.AddFunc("eventbus", app.bus.Run)
|
runner.AddFunc("eventbus", app.bus.Run)
|
||||||
|
|
||||||
go app.startBgTask(time.Duration(24)*time.Hour, func() {
|
runner.AddPlugin(NewTask("purge-tokens", time.Duration(24)*time.Hour, func(ctx context.Context) {
|
||||||
_, err := app.repos.AuthTokens.PurgeExpiredTokens(context.Background())
|
_, err := app.repos.AuthTokens.PurgeExpiredTokens(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().
|
log.Error().
|
||||||
Err(err).
|
Err(err).
|
||||||
Msg("failed to purge expired tokens")
|
Msg("failed to purge expired tokens")
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
go app.startBgTask(time.Duration(24)*time.Hour, func() {
|
|
||||||
_, err := app.repos.Groups.InvitationPurge(context.Background())
|
runner.AddPlugin(NewTask("purge-invitations", time.Duration(24)*time.Hour, func(ctx context.Context) {
|
||||||
|
_, err := app.repos.Groups.InvitationPurge(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().
|
log.Error().
|
||||||
Err(err).
|
Err(err).
|
||||||
Msg("failed to purge expired invitations")
|
Msg("failed to purge expired invitations")
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
go app.startBgTask(time.Duration(1)*time.Hour, func() {
|
|
||||||
|
runner.AddPlugin(NewTask("send-notifications", time.Duration(1)*time.Hour, func(ctx context.Context) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
if now.Hour() == 8 {
|
if now.Hour() == 8 {
|
||||||
|
@ -236,7 +237,7 @@ func run(cfg *config.Config) error {
|
||||||
Msg("failed to send notifiers")
|
Msg("failed to send notifiers")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
|
|
||||||
// TODO: Remove through external API that does setup
|
// TODO: Remove through external API that does setup
|
||||||
if cfg.Demo {
|
if cfg.Demo {
|
||||||
|
@ -256,9 +257,10 @@ func run(cfg *config.Config) error {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
debugserver.Shutdown(context.Background())
|
_ = debugserver.Shutdown(context.Background())
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
log.Info().Msgf("Debug server is running on %s:%s", cfg.Web.Host, cfg.Debug.Port)
|
||||||
return debugserver.ListenAndServe()
|
return debugserver.ListenAndServe()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,7 +45,9 @@ func TestMain(m *testing.M) {
|
||||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go tbus.Run()
|
go func() {
|
||||||
|
_ = tbus.Run(context.Background())
|
||||||
|
}()
|
||||||
|
|
||||||
err = client.Schema.Create(context.Background())
|
err = client.Schema.Create(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -16,7 +16,7 @@ type ErrorResponse struct {
|
||||||
Fields map[string]string `json:"fields,omitempty"`
|
Fields map[string]string `json:"fields,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Errors(svr *server.Server, log zerolog.Logger) errchain.ErrorHandler {
|
func Errors(log zerolog.Logger) errchain.ErrorHandler {
|
||||||
return func(h errchain.Handler) http.Handler {
|
return func(h errchain.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := h.ServeHTTP(w, r)
|
err := h.ServeHTTP(w, r)
|
||||||
|
@ -71,14 +71,6 @@ func Errors(svr *server.Server, log zerolog.Logger) errchain.ErrorHandler {
|
||||||
if err := server.JSON(w, code, resp); err != nil {
|
if err := server.JSON(w, code, resp); err != nil {
|
||||||
log.Err(err).Msg("failed to write response")
|
log.Err(err).Msg("failed to write response")
|
||||||
}
|
}
|
||||||
|
|
||||||
// If Showdown error, return error
|
|
||||||
if server.IsShutdownError(err) {
|
|
||||||
err := svr.Shutdown(err.Error())
|
|
||||||
if err != nil {
|
|
||||||
log.Err(err).Msg("failed to shutdown server")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue