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.
// 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()

View file

@ -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)

View file

@ -1,5 +1,6 @@
username=admin
password=admin
port=7101
timezone=UTC
log_to_file=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
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)
}