diff --git a/configuration/config.go b/configuration/config.go index 2f2832054bf128d0c37086b7b96ed7c754f942b1..d0183c26d33c6617f775799a91951db80fe15681 100644 --- a/configuration/config.go +++ b/configuration/config.go @@ -14,6 +14,8 @@ const BODY_SIZE_LIMIT = 32 * 1024 * 1024 // 32 MB, in bytes type Config struct { Version string `env:"VERSION" envDefault:"N/A"` + LogLevel string `env:"LOG_LEVE" envDefault:"error"` + Environment string `env:"ENV_NAME" envDefault:"development"` Host string `env:"HOST" envDefault:"127.0.0.1"` Port int16 `env:"PORT" envDefault:"3000"` @@ -44,5 +46,19 @@ func New() ( *Config, error ){ ) } + + if cfg.Environment == "development" { cfg.LogLevel = "debug" } + + possibleLogLevels := map[ string ] bool { + "error": true, + "debug": true, + } + if _, ok := possibleLogLevels[ cfg.LogLevel ]; !ok { + return nil, errors.New( + fmt.Sprintf( "Invalid log level: %s", cfg.LogLevel ), + ) + } + + return &cfg, nil } diff --git a/routing/routes.go b/routing/routes.go index 30b607f32a2700d087b06747f5d0e8a56c8fe462..0455c7677ac002c6e340d0cd7e765121575663c9 100644 --- a/routing/routes.go +++ b/routing/routes.go @@ -25,6 +25,18 @@ func SetRoutes( router *f.App, config *configuration.Config, store state.Store, log.Fatal( err ) } + if config.LogLevel == "debug" { + router.All( "*", func( c *f.Ctx ) error { + log.Printf( "%s %s mime:%s agent:%s", + c.Path(), + c.Method(), + c.Get( f.HeaderContentType ), + c.Get( f.HeaderUserAgent ), + ) + return c.Next() + }) + } + router.Get( "/", func( c *f.Ctx ) error { headers := c.GetReqHeaders()