Add timezones
This commit is contained in:
parent
b88260ff14
commit
40c56822e5
4 changed files with 18 additions and 9 deletions
1
auth.go
1
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()
|
||||
|
|
11
config.go
11
config.go
|
@ -9,6 +9,7 @@ import (
|
|||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ConfigFile = "config/config.txt"
|
||||
|
@ -17,6 +18,7 @@ type Config struct {
|
|||
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"`
|
||||
|
||||
|
@ -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)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
username=admin
|
||||
password=admin
|
||||
port=7101
|
||||
timezone=UTC
|
||||
log_to_file=false
|
||||
enable_scram=false
|
||||
|
|
4
files.go
4
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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue