Refactor everything again
This commit is contained in:
parent
7fdb0bf0f4
commit
96c369e4b1
14 changed files with 72 additions and 53 deletions
54
internal/config/config.go
Normal file
54
internal/config/config.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Config stores app configuration
|
||||
type Config struct {
|
||||
Port int `json:"port"`
|
||||
KeyFile string `json:"key-file"`
|
||||
SQLiteFile string `json:"sqlite-file"`
|
||||
LogToFile bool `json:"log-to-file"`
|
||||
LogFile string `json:"log-file"`
|
||||
}
|
||||
|
||||
var DefaultConfig = Config{
|
||||
Port: 7102,
|
||||
KeyFile: "private.key",
|
||||
SQLiteFile: "data.db",
|
||||
LogToFile: false,
|
||||
LogFile: "pye.log",
|
||||
}
|
||||
|
||||
var (
|
||||
DefaultLocation = "config.json"
|
||||
Cfg Config
|
||||
)
|
||||
|
||||
// Load parses a JSON-formatted configuration file
|
||||
func load(filename string) (Config, error) {
|
||||
data, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
conf := DefaultConfig
|
||||
err = json.Unmarshal(data, &conf)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
slog.Debug("Loaded config", "file", filename, "config", conf)
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
// MustLoad handles initial configuration loading
|
||||
func MustLoad(filename string) {
|
||||
conf, err := load(filename)
|
||||
if err != nil {
|
||||
slog.Error("error initially loading config", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
Cfg = conf
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue