pye/config/config.go

51 lines
945 B
Go
Raw Normal View History

2024-10-12 21:45:00 +03:00
package config
2024-10-12 22:15:44 +03:00
import (
"encoding/json"
"log/slog"
"os"
)
2024-10-12 21:45:00 +03:00
type Config struct {
Port int `json:"port"`
KeyFile string `json:"key-file"`
SQLiteFile string `json:"sqlite-file"`
2024-10-13 11:25:00 +03:00
LogToFile bool `json:"log-to-file"`
LogFile string `json:"log-file"`
2024-10-12 21:45:00 +03:00
}
var DefaultConfig = Config{
2024-10-12 22:15:44 +03:00
Port: 7102,
KeyFile: "private.key",
2024-10-12 21:45:00 +03:00
SQLiteFile: "data.db",
2024-10-13 11:25:00 +03:00
LogToFile: false,
LogFile: "pye.log",
2024-10-12 21:45:00 +03:00
}
2024-10-12 22:15:44 +03:00
var (
DefaultLocation = "config.json"
Cfg Config
)
func LoadConfig(filename string) error {
data, err := os.ReadFile(filename)
if err != nil {
return err
}
temp_config := DefaultConfig
err = json.Unmarshal(data, &temp_config)
if err != nil {
return err
}
Cfg = temp_config
2024-10-13 16:38:13 +03:00
slog.Debug("Loaded config", "file", filename)
2024-10-12 22:15:44 +03:00
return nil
}
2024-10-13 16:38:13 +03:00
func MustLoadConfig(filename string) {
err := LoadConfig(filename)
2024-10-12 22:15:44 +03:00
if err != nil {
slog.Error("error initially loading config", "error", err)
os.Exit(1)
}
2024-10-13 16:38:13 +03:00
}