diff --git a/README.md b/README.md index aae5703..dbb8277 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ config ### Config options: Below are defaults of config.txt. The settings are defined in newline separated key=value pairs. -Please don't include the bash-style "comments" in your config, +Please don't include the bash-style "comments" in your actual config, they are provided purely for demonstration only and **will break the config if present**. ``` username=admin # Your username @@ -48,7 +48,8 @@ password=admin # Your password port=7101 # What port to run on (probably leave on 7101 if using docker) timezone=Local # IANA Time zone database identifier ("UTC", Local", "Europe/Moscow" etc.), Defaults to Local if can't parse. language=en # ISO-639 language code (currently supported - en, ru) -log_to_file=false # Whether to write logs to a file (located in /log.txt) +log_to_file=false # Whether to write logs to a file +log_file=config/log.txt # Where to store the log file if it is enabled enable_scram=false # Whether the app should shut down if there are 3 or more failed login attempts within 100 seconds # Not present by default, set only if you want to be notified of any failed login attempts over telegram diff --git a/auth.go b/auth.go index 85c7e7d..4104243 100644 --- a/auth.go +++ b/auth.go @@ -43,7 +43,7 @@ func NoteLoginFail(username string, password string, r *http.Request) { // BasicAuth is a middleware that handles authentication & authorization for the app. // It uses BasicAuth because I doubt there is a need for something sophisticated in a small hobby project -// Originally taken from https://www.alexedwards.net/blog/basic-authentication-in-go (13.03.2024) +// Originally taken from Alex Edwards's https://www.alexedwards.net/blog/basic-authentication-in-go, MIT Licensed. (13.03.2024) func BasicAuth(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { username, password, ok := r.BasicAuth() @@ -81,7 +81,7 @@ func Scram() { // NotifyTelegram attempts to send a message to admin through telegram func NotifyTelegram(msg string) { if Cfg.TelegramChat == "" || Cfg.TelegramToken == "" { - slog.Warn("ignoring telegram request due to lack of credentials") + slog.Debug("ignoring telegram request due to lack of credentials") return } client := &http.Client{} diff --git a/config.go b/config.go index e1ba946..015c077 100644 --- a/config.go +++ b/config.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "log" + "log/slog" "os" "reflect" "strconv" @@ -21,6 +22,7 @@ type Config struct { Timezone *time.Location `config:"timezone" type:"location"` Language string `config:"language" type:"string"` LogToFile bool `config:"log_to_file" type:"bool"` + LogFile string `config:"log_file" type:"string"` Scram bool `config:"enable_scram" type:"bool"` TelegramToken string `config:"tg_token" type:"string"` @@ -114,13 +116,22 @@ func (c *Config) Reload() error { } else { c.Timezone = loc } + slog.Debug("reloaded config", "config", c) return LoadLanguage(c.Language) // Load selected language } // ConfigInit loads config on startup +// Some defaults are declared here func ConfigInit() Config { - cfg := Config{Port: 7101, Username: "admin", Password: "admin", Timezone: time.Local, Language: "en"} // Some defaults are declared here + cfg := Config{ + Port: 7101, + Username: "admin", + Password: "admin", + Timezone: time.Local, + Language: "en", + LogFile: "config/log.txt", + } err := cfg.Reload() if err != nil { log.Fatal(err) diff --git a/config/config.txt b/config/config.txt index f9398f4..d0a665e 100644 --- a/config/config.txt +++ b/config/config.txt @@ -4,4 +4,5 @@ port=7101 timezone=Local language=en log_to_file=false +log_file=config/log.txt enable_scram=false diff --git a/i18n.go b/i18n.go index 03d4f12..2088b55 100644 --- a/i18n.go +++ b/i18n.go @@ -9,6 +9,7 @@ import ( var Translations = map[string]string{} +// LoadLanguage loads a json file for selected language into the Translations map func LoadLanguage(language string) error { filename := "i18n/" + language + ".json" @@ -28,6 +29,7 @@ func LoadLanguage(language string) error { return err } +// TranslatableText attempts to match an id to a string in current language func TranslatableText(id string) string { if v, ok := Translations[id]; !ok { return id diff --git a/logger.go b/logger.go index 7b5d043..2c90028 100644 --- a/logger.go +++ b/logger.go @@ -8,16 +8,15 @@ import ( "os" ) -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, and enables debug mode if selected func LogInit() { var w io.Writer if Cfg.LogToFile { - f, err := os.OpenFile(LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + f, err := os.OpenFile(Cfg.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { - slog.Error("error opening log file, logging to stdout", "path", LogFile, "error", err) + slog.Error("error opening log file, logging to stdout", "path", Cfg.LogFile, "error", err) return } // No defer f.Close() because that breaks the MultiWriter