confirm casing

This commit is contained in:
Hayden 2022-09-03 19:27:02 -08:00
parent e159087e5f
commit 687282ca68
4 changed files with 54 additions and 17 deletions

View file

@ -20,7 +20,7 @@ type app struct {
services *services.AllServices services *services.AllServices
} }
func NewApp(conf *config.Config) *app { func new(conf *config.Config) *app {
s := &app{ s := &app{
conf: conf, conf: conf,
} }
@ -36,7 +36,7 @@ func NewApp(conf *config.Config) *app {
return s return s
} }
func (a *app) StartBgTask(t time.Duration, fn func()) { func (a *app) startBgTask(t time.Duration, fn func()) {
for { for {
a.server.Background(fn) a.server.Background(fn)
time.Sleep(t) time.Sleep(t)

41
backend/app/api/logger.go Normal file
View file

@ -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
}
}

View file

@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"os"
"time" "time"
"github.com/hay-kot/content/backend/app/api/docs" "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/internal/services"
"github.com/hay-kot/content/backend/pkgs/server" "github.com/hay-kot/content/backend/pkgs/server"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@ -27,10 +25,6 @@ import (
// @name Authorization // @name Authorization
// @description "Type 'Bearer TOKEN' to correctly set the API Key" // @description "Type 'Bearer TOKEN' to correctly set the API Key"
func main() { func main() {
// Logger Init
// zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.Level(zerolog.DebugLevel)
cfgFile := "config.yml" cfgFile := "config.yml"
@ -47,8 +41,9 @@ func main() {
} }
func run(cfg *config.Config) error { func run(cfg *config.Config) error {
app := NewApp(cfg) app := new(cfg)
app.setupLogger()
// ========================================================================= // =========================================================================
// Initialize Database & Repos // Initialize Database & Repos
@ -75,20 +70,19 @@ func run(cfg *config.Config) error {
// ========================================================================= // =========================================================================
// Start Server // Start Server
app.conf.Print()
app.server = server.NewServer(app.conf.Web.Host, app.conf.Web.Port) app.server = server.NewServer(app.conf.Web.Host, app.conf.Web.Port)
routes := app.newRouter(app.repos) 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) log.Info().Msgf("Starting HTTP Server on %s:%s", app.server.Host, app.server.Port)
// ========================================================================= // =========================================================================
// Start Reoccurring Tasks // 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()) _, err := app.repos.AuthTokens.PurgeExpiredTokens(context.Background())
if err != nil { if err != nil {
log.Error(). log.Error().

View file

@ -82,9 +82,9 @@ func (a *app) newRouter(repos *repo.AllRepos) *chi.Mux {
return r 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. // 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 desiredSpaces := 10
walkFunc := func(method string, route string, handler http.Handler, middleware ...func(http.Handler) http.Handler) error { 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 { func notFoundHandler() http.HandlerFunc {
tryRead := func(fs embed.FS, prefix, requestedPath string, w http.ResponseWriter) error { tryRead := func(fs embed.FS, prefix, requestedPath string, w http.ResponseWriter) error {
f, err := fs.Open(path.Join(prefix, requestedPath)) f, err := fs.Open(path.Join(prefix, requestedPath))