diff --git a/backend/app/api/app.go b/backend/app/api/app.go index 8e1f742..7176758 100644 --- a/backend/app/api/app.go +++ b/backend/app/api/app.go @@ -20,7 +20,7 @@ type app struct { services *services.AllServices } -func NewApp(conf *config.Config) *app { +func new(conf *config.Config) *app { s := &app{ conf: conf, } @@ -36,7 +36,7 @@ func NewApp(conf *config.Config) *app { return s } -func (a *app) StartBgTask(t time.Duration, fn func()) { +func (a *app) startBgTask(t time.Duration, fn func()) { for { a.server.Background(fn) time.Sleep(t) diff --git a/backend/app/api/logger.go b/backend/app/api/logger.go new file mode 100644 index 0000000..b410cd1 --- /dev/null +++ b/backend/app/api/logger.go @@ -0,0 +1,41 @@ +package main + +import ( + "os" + "strings" + + "github.com/hay-kot/content/backend/internal/config" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" +) + +// setupLogger initializes the zerolog config +// for the shared logger. +func (a *app) setupLogger() { + // Logger Init + // zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + if a.conf.Mode != config.ModeProduction { + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + } + + log.Level(getLevel(a.conf.Log.Level)) +} + +func getLevel(l string) zerolog.Level { + switch strings.ToLower(l) { + case "debug": + return zerolog.DebugLevel + case "info": + return zerolog.InfoLevel + case "warn": + return zerolog.WarnLevel + case "error": + return zerolog.ErrorLevel + case "fatal": + return zerolog.FatalLevel + case "panic": + return zerolog.PanicLevel + default: + return zerolog.InfoLevel + } +} diff --git a/backend/app/api/main.go b/backend/app/api/main.go index 7519ee7..d06faa0 100644 --- a/backend/app/api/main.go +++ b/backend/app/api/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "os" "time" "github.com/hay-kot/content/backend/app/api/docs" @@ -12,7 +11,6 @@ import ( "github.com/hay-kot/content/backend/internal/services" "github.com/hay-kot/content/backend/pkgs/server" _ "github.com/mattn/go-sqlite3" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) @@ -27,10 +25,6 @@ import ( // @name Authorization // @description "Type 'Bearer TOKEN' to correctly set the API Key" func main() { - // Logger Init - // zerolog.TimeFieldFormat = zerolog.TimeFormatUnix - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) - log.Level(zerolog.DebugLevel) cfgFile := "config.yml" @@ -47,8 +41,9 @@ func main() { } func run(cfg *config.Config) error { - app := NewApp(cfg) + app := new(cfg) + app.setupLogger() // ========================================================================= // Initialize Database & Repos @@ -75,20 +70,19 @@ func run(cfg *config.Config) error { // ========================================================================= // Start Server - - app.conf.Print() - app.server = server.NewServer(app.conf.Web.Host, app.conf.Web.Port) - routes := app.newRouter(app.repos) - app.LogRoutes(routes) + + if app.conf.Mode != config.ModeDevelopment { + app.logRoutes(routes) + } log.Info().Msgf("Starting HTTP Server on %s:%s", app.server.Host, app.server.Port) // ========================================================================= // Start Reoccurring Tasks - go app.StartBgTask(time.Duration(24)*time.Hour, func() { + go app.startBgTask(time.Duration(24)*time.Hour, func() { _, err := app.repos.AuthTokens.PurgeExpiredTokens(context.Background()) if err != nil { log.Error(). diff --git a/backend/app/api/routes.go b/backend/app/api/routes.go index 1abed0b..1ff79f0 100644 --- a/backend/app/api/routes.go +++ b/backend/app/api/routes.go @@ -82,9 +82,9 @@ func (a *app) newRouter(repos *repo.AllRepos) *chi.Mux { return r } -// LogRoutes logs the routes of the server that are registered within Server.registerRoutes(). This is useful for debugging. +// logRoutes logs the routes of the server that are registered within Server.registerRoutes(). This is useful for debugging. // See https://github.com/go-chi/chi/issues/332 for details and inspiration. -func (a *app) LogRoutes(r *chi.Mux) { +func (a *app) logRoutes(r *chi.Mux) { desiredSpaces := 10 walkFunc := func(method string, route string, handler http.Handler, middleware ...func(http.Handler) http.Handler) error { @@ -115,6 +115,8 @@ func registerMimes() { } } +// notFoundHandler perform the main logic around handling the internal SPA embed and ensuring that +// the client side routing is handled correctly. func notFoundHandler() http.HandlerFunc { tryRead := func(fs embed.FS, prefix, requestedPath string, w http.ResponseWriter) error { f, err := fs.Open(path.Join(prefix, requestedPath))