diff --git a/auth.go b/auth.go index b448a06..85c7e7d 100644 --- a/auth.go +++ b/auth.go @@ -44,7 +44,6 @@ 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) -// TODO: why did I have to convert Handler from HandlerFunc? func BasicAuth(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { username, password, ok := r.BasicAuth() diff --git a/config.go b/config.go index fb0c0fd..06484e0 100644 --- a/config.go +++ b/config.go @@ -9,16 +9,18 @@ import ( "reflect" "strconv" "strings" + "time" ) var ConfigFile = "config/config.txt" type Config struct { - Username string `config:"username"` - Password string `config:"password"` - Port int `config:"port"` - LogToFile bool `config:"log_to_file"` - Scram bool `config:"enable_scram"` + Username string `config:"username"` + Password string `config:"password"` + Port int `config:"port"` + Timezone *time.Location `config:"timezone"` + LogToFile bool `config:"log_to_file"` + Scram bool `config:"enable_scram"` TelegramToken string `config:"tg_token"` TelegramChat string `config:"tg_chat"` @@ -78,6 +80,13 @@ func (c *Config) Reload() error { if err == nil { c.Port = numVal } + } else if key == "timezone" { + loc, err := time.LoadLocation(value) + if err != nil { + c.Timezone = time.UTC + } else { + c.Timezone = loc + } } else if key == "tg_token" { c.TelegramToken = value } else if key == "tg_chat" { @@ -105,7 +114,7 @@ func (c *Config) Reload() error { // ConfigInit loads config on startup func ConfigInit() Config { - cfg := Config{Port: 7101, Username: "admin", Password: "admin"} // Default values are declared here, I guess + cfg := Config{Port: 7101, Username: "admin", Password: "admin", Timezone: time.UTC} // Default values are declared here, I guess err := cfg.Reload() if err != nil { log.Fatal(err) diff --git a/config/config.txt b/config/config.txt index e270796..c56a9d1 100644 --- a/config/config.txt +++ b/config/config.txt @@ -1,5 +1,6 @@ username=admin password=admin port=7101 +timezone=UTC log_to_file=false enable_scram=false diff --git a/files.go b/files.go index 0a306ed..9aac325 100644 --- a/files.go +++ b/files.go @@ -64,10 +64,10 @@ func ListFiles(directory string) ([]string, error) { // ReadToday runs ReadFile with today's date as filename func ReadToday() ([]byte, error) { - return ReadFile("day/" + time.Now().Format(time.DateOnly)) + return ReadFile("day/" + time.Now().In(Cfg.Timezone).Format(time.DateOnly)) } // SaveToday runs SaveFile with today's date as filename func SaveToday(contents []byte) error { - return SaveFile("day/"+time.Now().Format(time.DateOnly), contents) + return SaveFile("day/"+time.Now().In(Cfg.Timezone).Format(time.DateOnly), contents) }