Add debug flag

This commit is contained in:
Andrew-71 2024-05-04 14:29:49 +03:00
parent 4ea67fc2e9
commit 8d4cbdac90
6 changed files with 13 additions and 3 deletions

View file

@ -73,4 +73,6 @@ If you for some reason decide to run plain executable instead of docker, it supp
override password override password
-port int -port int
override port override port
-debug
show debug log
``` ```

View file

@ -8,6 +8,6 @@ List of things to add to this project
* API revamp * API revamp
* Check export function for improvements * Check export function for improvements
* Customise log file * Customise log file
* More slog.Debug and a debug flag? * More slog.Debug
* Consider less clunky auth method * Consider less clunky auth method
* *Go* dependency-less? <-- this is a terrible idea * *Go* dependency-less? <-- this is a terrible idea

View file

@ -11,6 +11,7 @@ func FlagInit() {
username := flag.String("user", "", "override username") username := flag.String("user", "", "override username")
password := flag.String("pass", "", "override password") password := flag.String("pass", "", "override password")
port := flag.Int("port", 0, "override port") port := flag.Int("port", 0, "override port")
debug := flag.Bool("debug", false, "debug logging")
flag.Parse() flag.Parse()
if *config != "" { if *config != "" {
@ -29,4 +30,5 @@ func FlagInit() {
if *port != 0 { if *port != 0 {
Cfg.Port = *port Cfg.Port = *port
} }
DebugMode = *debug
} }

View file

@ -9,6 +9,7 @@ import (
) )
var LogFile = "config/log.txt" var LogFile = "config/log.txt"
var DebugMode = false
// LogInit makes slog output to both stdout and a file if needed // LogInit makes slog output to both stdout and a file if needed
func LogInit() { func LogInit() {
@ -26,6 +27,10 @@ func LogInit() {
} }
// Make slog and chi use intended format // Make slog and chi use intended format
slog.SetDefault(slog.New(slog.NewTextHandler(w, nil))) var opts *slog.HandlerOptions
if DebugMode {
opts = &slog.HandlerOptions{Level: slog.LevelDebug}
}
slog.SetDefault(slog.New(slog.NewTextHandler(w, opts)))
middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: log.Default(), NoColor: true}) middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: log.Default(), NoColor: true})
} }

View file

@ -3,7 +3,7 @@ package main
var Cfg = ConfigInit() var Cfg = ConfigInit()
func main() { func main() {
LogInit()
FlagInit() FlagInit()
LogInit()
Serve() Serve()
} }

View file

@ -44,5 +44,6 @@ func Serve() {
r.Handle("/public/*", http.StripPrefix("/public/", fs)) r.Handle("/public/*", http.StripPrefix("/public/", fs))
slog.Info("🌺 Website working", "port", Cfg.Port) slog.Info("🌺 Website working", "port", Cfg.Port)
slog.Debug("Debug mode enabled")
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(Cfg.Port), r)) log.Fatal(http.ListenAndServe(":"+strconv.Itoa(Cfg.Port), r))
} }