Add timezones

This commit is contained in:
Andrew-71 2024-04-14 11:45:42 +03:00
parent b88260ff14
commit 40c56822e5
4 changed files with 18 additions and 9 deletions

View file

@ -44,7 +44,6 @@ func NoteLoginFail(username string, password string, r *http.Request) {
// BasicAuth is a middleware that handles authentication & authorization for the app. // 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 // 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 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 { func BasicAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth() username, password, ok := r.BasicAuth()

View file

@ -9,16 +9,18 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"time"
) )
var ConfigFile = "config/config.txt" var ConfigFile = "config/config.txt"
type Config struct { type Config struct {
Username string `config:"username"` Username string `config:"username"`
Password string `config:"password"` Password string `config:"password"`
Port int `config:"port"` Port int `config:"port"`
LogToFile bool `config:"log_to_file"` Timezone *time.Location `config:"timezone"`
Scram bool `config:"enable_scram"` LogToFile bool `config:"log_to_file"`
Scram bool `config:"enable_scram"`
TelegramToken string `config:"tg_token"` TelegramToken string `config:"tg_token"`
TelegramChat string `config:"tg_chat"` TelegramChat string `config:"tg_chat"`
@ -78,6 +80,13 @@ func (c *Config) Reload() error {
if err == nil { if err == nil {
c.Port = numVal 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" { } else if key == "tg_token" {
c.TelegramToken = value c.TelegramToken = value
} else if key == "tg_chat" { } else if key == "tg_chat" {
@ -105,7 +114,7 @@ func (c *Config) Reload() error {
// ConfigInit loads config on startup // ConfigInit loads config on startup
func ConfigInit() Config { 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() err := cfg.Reload()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)

View file

@ -1,5 +1,6 @@
username=admin username=admin
password=admin password=admin
port=7101 port=7101
timezone=UTC
log_to_file=false log_to_file=false
enable_scram=false enable_scram=false

View file

@ -64,10 +64,10 @@ func ListFiles(directory string) ([]string, error) {
// ReadToday runs ReadFile with today's date as filename // ReadToday runs ReadFile with today's date as filename
func ReadToday() ([]byte, error) { 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 // SaveToday runs SaveFile with today's date as filename
func SaveToday(contents []byte) error { 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)
} }