status/core/config.go

68 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-10-01 08:21:30 +00:00
package core
import (
"fmt"
"io"
"os"
2024-09-21 22:09:21 +00:00
"path"
2023-10-01 08:21:30 +00:00
"github.com/pelletier/go-toml/v2"
)
type Config struct {
2024-09-23 00:58:32 +00:00
_ConfigDirectory string `toml:"-"`
SiteName string
IconSrc string
2024-09-21 22:09:21 +00:00
// db location
Database string
UnixSocket string
BasicAuthUsername string
BasicAuthPassword string
2024-09-25 20:30:37 +00:00
Service []ServiceConfig
Matrix MatrixConfig
2023-10-01 08:21:30 +00:00
}
func LoadConfigFromTOML(configPath string) (*Config, error) {
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
fileData, err := io.ReadAll(file)
if err != nil {
return nil, err
}
var cfg Config
err = toml.Unmarshal([]byte(fileData), &cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}
func LoadConfig() (*Config, error) {
2024-09-21 22:09:21 +00:00
configLocations := []string{
2024-09-27 01:54:32 +00:00
//"/etc/status/",
2024-09-21 22:09:21 +00:00
"proto/",
2024-09-27 01:54:32 +00:00
//"../",
2024-09-21 22:09:21 +00:00
}
errMsgPath := ""
for _, directory := range configLocations {
configPath := path.Join(directory, "config.toml")
errMsgPath += fmt.Sprintf("\n- %v", configPath)
config, err := LoadConfigFromTOML(configPath)
if err == nil {
config._ConfigDirectory = directory
return config, nil
}
if os.IsNotExist(err) {
continue
} else {
return nil, err
2023-10-01 08:21:30 +00:00
}
}
2024-09-21 22:09:21 +00:00
return nil, fmt.Errorf("Cannot load config file:" + errMsgPath + "\nSuggestion: cp proto/config.toml.in proto/config.toml")
2023-10-01 08:21:30 +00:00
}