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
-port int
override port
-debug
show debug log
```

View file

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

View file

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

View file

@ -9,6 +9,7 @@ import (
)
var LogFile = "config/log.txt"
var DebugMode = false
// LogInit makes slog output to both stdout and a file if needed
func LogInit() {
@ -26,6 +27,10 @@ func LogInit() {
}
// 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})
}

View file

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

View file

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