filebrowser/backend/settings/config.go

65 lines
1.2 KiB
Go
Raw Normal View History

2023-09-01 14:00:02 +00:00
package settings
import (
"log"
"os"
"github.com/goccy/go-yaml"
)
var GlobalConfiguration Settings
2023-09-03 17:28:00 +00:00
var configYml = "filebrowser.yaml"
2023-09-01 14:00:02 +00:00
2023-09-03 00:16:19 +00:00
func Initialize() {
2023-09-03 17:28:00 +00:00
yamlData := loadConfigFile()
GlobalConfiguration = setDefaults()
err := yaml.Unmarshal(yamlData, &GlobalConfiguration)
2023-09-01 14:00:02 +00:00
if err != nil {
2023-09-03 17:28:00 +00:00
log.Fatalf("Error unmarshaling YAML data: %v", err)
}
}
2023-09-03 00:16:19 +00:00
2023-09-03 17:28:00 +00:00
func loadConfigFile() []byte {
// Open and read the YAML file
yamlFile, err := os.Open(configYml)
if err != nil {
log.Printf("Error opening config file: %v\nUsing default config only", err)
2023-09-03 00:16:19 +00:00
setDefaults()
2023-09-03 17:28:00 +00:00
return []byte{}
2023-09-01 14:00:02 +00:00
}
defer yamlFile.Close()
2023-09-02 02:03:45 +00:00
stat, err := yamlFile.Stat()
if err != nil {
2023-09-03 00:16:19 +00:00
log.Fatalf("Error getting file information: %s", err.Error())
2023-09-02 02:03:45 +00:00
}
yamlData := make([]byte, stat.Size())
_, err = yamlFile.Read(yamlData)
2023-09-01 14:00:02 +00:00
if err != nil {
log.Fatalf("Error reading YAML data: %v", err)
}
2023-09-03 17:28:00 +00:00
return yamlData
2023-09-01 14:00:02 +00:00
}
2023-09-03 17:28:00 +00:00
func setDefaults() Settings {
return Settings{
2023-09-01 14:00:02 +00:00
Signup: true,
2023-09-02 00:51:13 +00:00
Server: Server{
IndexingInterval: 5,
Port: 8080,
2023-09-03 17:28:00 +00:00
NumImageProcessors: 4,
2023-09-02 02:03:45 +00:00
BaseURL: "",
2023-09-02 00:51:13 +00:00
},
2023-09-01 14:00:02 +00:00
Auth: Auth{
2023-09-02 00:51:13 +00:00
Method: "password",
2023-09-01 14:00:02 +00:00
Recaptcha: Recaptcha{
Host: "",
},
},
2023-09-03 17:28:00 +00:00
UserDefaults: UserDefaults{
HideDotfiles: true,
},
2023-09-02 00:51:13 +00:00
}
}