hibiscus/config.go

197 lines
4.8 KiB
Go
Raw Permalink Normal View History

2024-03-17 17:12:41 +03:00
package main
import (
"bufio"
"errors"
2024-03-18 15:22:50 +03:00
"fmt"
2024-03-17 17:12:41 +03:00
"log"
2024-05-04 15:39:15 +03:00
"log/slog"
"net/http"
2024-03-17 17:12:41 +03:00
"os"
2024-03-23 15:36:01 +03:00
"reflect"
2024-03-17 17:12:41 +03:00
"strconv"
"strings"
2024-04-14 11:45:42 +03:00
"time"
2024-03-17 17:12:41 +03:00
)
2024-03-18 15:22:50 +03:00
var ConfigFile = "config/config.txt"
2024-03-17 17:12:41 +03:00
type Config struct {
2024-05-08 15:56:11 +03:00
Username string `config:"username" type:"string" mandatory:"true"`
Password string `config:"password" type:"string" mandatory:"true"`
Port int `config:"port" type:"int" mandatory:"true"`
Timezone *time.Location `config:"timezone" type:"location" mandatory:"true"`
2024-05-05 13:06:20 +03:00
GraceTime time.Duration `config:"grace_period" type:"duration"`
2024-05-08 15:56:11 +03:00
Language string `config:"language" type:"string" mandatory:"true"`
2024-05-08 13:12:10 +03:00
Theme string `config:"theme" type:"string"`
2024-05-08 15:56:11 +03:00
Title string `config:"title" type:"string"`
2024-05-03 16:48:17 +03:00
LogToFile bool `config:"log_to_file" type:"bool"`
2024-05-04 15:39:15 +03:00
LogFile string `config:"log_file" type:"string"`
2024-05-03 16:48:17 +03:00
Scram bool `config:"enable_scram" type:"bool"`
2024-03-20 16:18:23 +03:00
2024-05-03 16:48:17 +03:00
TelegramToken string `config:"tg_token" type:"string"`
TelegramChat string `config:"tg_chat" type:"string"`
TelegramTopic string `config:"tg_topic" type:"string"`
2024-03-17 17:12:41 +03:00
}
2024-05-08 15:56:11 +03:00
var DefaultConfig = Config{
Username: "admin",
Password: "admin",
Port: 7101,
Timezone: time.Local,
GraceTime: 0,
Language: "en",
2024-08-28 14:53:54 +03:00
Theme: "",
2024-05-08 15:56:11 +03:00
Title: "🌺 Hibiscus.txt",
LogToFile: false,
LogFile: "config/log.txt",
Scram: false,
TelegramToken: "",
TelegramChat: "",
TelegramTopic: "",
2024-05-08 15:56:11 +03:00
}
// String returns string representation of modified and mandatory config options.
2024-05-09 23:44:37 +03:00
func (c *Config) String() string {
2024-03-23 15:36:01 +03:00
output := ""
v := reflect.ValueOf(*c)
2024-05-08 15:56:11 +03:00
vDefault := reflect.ValueOf(DefaultConfig)
2024-03-23 15:36:01 +03:00
typeOfS := v.Type()
for i := 0; i < v.NumField(); i++ {
key := typeOfS.Field(i).Tag.Get("config")
value := v.Field(i).Interface()
2024-05-08 15:56:11 +03:00
mandatory := typeOfS.Field(i).Tag.Get("mandatory")
if (mandatory == "true") || (value != vDefault.Field(i).Interface()) { // Only save non-default values
2024-03-23 15:36:01 +03:00
output += fmt.Sprintf("%s=%v\n", key, value)
}
2024-03-20 16:18:23 +03:00
}
2024-05-09 23:44:37 +03:00
return output
2024-03-18 15:22:50 +03:00
}
2024-03-17 17:12:41 +03:00
// Reload resets, then loads config from the ConfigFile.
// It creates the file with mandatory options if it is missing.
2024-03-20 00:02:22 +03:00
func (c *Config) Reload() error {
2024-05-08 16:49:34 +03:00
*c = DefaultConfig // Reset config
2024-03-18 15:22:50 +03:00
if _, err := os.Stat(ConfigFile); errors.Is(err, os.ErrNotExist) {
err := c.Save()
2024-03-18 15:22:50 +03:00
if err != nil {
2024-03-20 00:02:22 +03:00
return err
2024-03-18 15:22:50 +03:00
}
2024-03-20 00:02:22 +03:00
return nil
2024-03-18 15:22:50 +03:00
}
file, err := os.Open(ConfigFile)
2024-03-17 17:12:41 +03:00
if err != nil {
2024-03-20 00:02:22 +03:00
return err
2024-03-17 17:12:41 +03:00
}
2024-05-03 16:48:17 +03:00
options := map[string]string{}
2024-03-17 17:12:41 +03:00
scanner := bufio.NewScanner(file)
for scanner.Scan() {
2024-05-08 15:56:11 +03:00
entry := strings.Split(strings.Trim(scanner.Text(), " \t"), "=")
2024-03-17 17:12:41 +03:00
if len(entry) != 2 {
continue
}
2024-05-03 16:48:17 +03:00
options[entry[0]] = entry[1]
2024-03-17 17:12:41 +03:00
}
if err := scanner.Err(); err != nil {
2024-03-20 00:02:22 +03:00
return err
2024-03-17 17:12:41 +03:00
}
2024-05-04 21:36:43 +03:00
err = file.Close()
if err != nil {
return err
}
2024-03-17 17:12:41 +03:00
2024-05-03 16:48:17 +03:00
timezone := "Local" // Timezone is handled separately because reflection
refStruct := reflect.ValueOf(*c)
refElem := reflect.ValueOf(&c).Elem()
typeOfS := refStruct.Type()
for i := 0; i < refStruct.NumField(); i++ {
fieldElem := reflect.Indirect(refElem).Field(i)
key := typeOfS.Field(i).Tag.Get("config")
if v, ok := options[key]; ok && fieldElem.CanSet() {
switch typeOfS.Field(i).Tag.Get("type") {
case "int":
{
numVal, err := strconv.Atoi(v)
if err == nil {
fieldElem.SetInt(int64(numVal))
}
}
case "bool":
{
if v == "true" {
fieldElem.SetBool(true)
} else {
fieldElem.SetBool(false)
}
}
case "location":
timezone = v
2024-05-05 13:06:20 +03:00
case "duration":
{
numVal, err := time.ParseDuration(v)
if err == nil {
fieldElem.SetInt(int64(numVal))
}
}
2024-05-03 16:48:17 +03:00
default:
fieldElem.SetString(v)
}
}
}
loc, err := time.LoadLocation(timezone)
if err != nil {
c.Timezone = time.Local
} else {
c.Timezone = loc
}
2024-05-04 15:39:15 +03:00
slog.Debug("reloaded config", "config", c)
2024-05-03 16:48:17 +03:00
2024-05-18 16:15:20 +03:00
return SetLanguage(c.Language) // Load selected language
2024-03-20 00:02:22 +03:00
}
// Read gets raw contents from ConfigFile.
2024-05-09 23:44:37 +03:00
func (c *Config) Read() ([]byte, error) {
return ReadFile(ConfigFile)
}
// Save writes config's contents to the ConfigFile.
func (c *Config) Save() error {
return SaveFile(ConfigFile, []byte(c.String()))
2024-05-09 23:44:37 +03:00
}
// PostConfig calls PostEntry for config file, then reloads the config.
func PostConfig(w http.ResponseWriter, r *http.Request) {
PostEntry(ConfigFile, w, r)
err := Cfg.Reload()
if err != nil {
slog.Error("error reloading config", "error", err)
}
}
// ConfigReloadApi reloads the config. It then redirects back if Referer field is present.
func ConfigReloadApi(w http.ResponseWriter, r *http.Request) {
err := Cfg.Reload()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
HandleWrite(w.Write([]byte(err.Error())))
}
if r.Referer() != "" {
http.Redirect(w, r, r.Header.Get("Referer"), 302)
return
}
w.WriteHeader(http.StatusOK)
}
// ConfigInit loads config on startup.
2024-03-20 00:02:22 +03:00
func ConfigInit() Config {
2024-05-08 16:49:34 +03:00
cfg := Config{}
2024-03-20 00:02:22 +03:00
err := cfg.Reload()
if err != nil {
log.Fatal(err)
}
return cfg
2024-03-17 17:12:41 +03:00
}