pye/config/config.go

52 lines
851 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"`
}
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-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
slog.Info("Loaded config", "file", filename)
return nil
}
func MustLoadConfig() {
err := LoadConfig(DefaultLocation)
if err != nil {
slog.Error("error initially loading config", "error", err)
os.Exit(1)
}
}
2024-10-12 21:45:00 +03:00
2024-10-12 22:15:44 +03:00
func init() {
MustLoadConfig()
}