Switch configuration file from json to toml

This commit is contained in:
2026-04-30 06:53:36 +02:00
parent 9f6c5b2880
commit d5c4cfa9d0
5 changed files with 40 additions and 15 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
data/
config.json
config.toml
*~
*.kate-swp
.*.kate-swp
+34 -13
View File
@@ -1,15 +1,18 @@
package config
import (
"encoding/json"
"log"
"os"
"github.com/BurntSushi/toml"
)
type Config struct {
SiteName string `json:"site_name"`
Port string `json:"port"`
DataDir string `json:"data_dir"`
SiteName string `toml:"site_name"`
Port string `toml:"port"`
DataDir string `toml:"data_dir"`
AdminUser string `toml:"admin_user"`
AdminPasswordHash string `toml:"admin_password_hash"`
}
func Load(path string) Config {
@@ -19,18 +22,36 @@ func Load(path string) Config {
DataDir: "data",
}
file, err := os.Open(path)
// Overwrite the default configuration (configData) with data from the file at 'path'
_, err := toml.DecodeFile(path, &configData)
if err != nil {
log.Println("No config file found, using defaults. Consider setting up a Site Name at the very least!")
return configData
}
defer file.Close()
// Overwrite the default configuration with data from 'file'
err = json.NewDecoder(file).Decode(&configData)
if err != nil {
log.Fatal("Config file exists but has errors:", err)
}
return configData
}
func Save(path string, configData Config) error {
file, err := os.Create(path)
if err != nil {
log.Println("An error occured while trying to save configuration data.")
return err
}
defer file.Close()
file.WriteString("site_name = \"" + configData.SiteName + "\"\n")
file.WriteString("port = \"" + configData.Port + "\"\n")
file.WriteString("data_dir = \"" + configData.DataDir + "\"\n")
if configData.AdminUser != "" {
file.WriteString("admin_user = \"" + configData.AdminUser + "\"\n")
file.WriteString("admin_password_hash = \"" + configData.AdminPasswordHash + "\"\n")
file.WriteString("# This admin_password_hash above is a hashed password, not your actual\"")
file.WriteString("# written password. You can't change your admin password here.\n")
file.WriteString("# If you've lost your password, delete admin_user and admin_hash\n")
file.WriteString("# from this file and restart the app to create a new account.\n")
file.WriteString("# Your uploaded content will stay intact.\n")
}
return nil
}
+2
View File
@@ -3,3 +3,5 @@ module 192.168.1.180/odonax/filehost-athome
go 1.26.2
require github.com/mattn/go-sqlite3 v1.14.44
require github.com/BurntSushi/toml v1.6.0 // indirect
+2
View File
@@ -1,2 +1,4 @@
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/mattn/go-sqlite3 v1.14.44 h1:3VSe+xafpbzsLbdr2AWlAZk9yRHiBhTBakioXaCKTF8=
github.com/mattn/go-sqlite3 v1.14.44/go.mod h1:pjEuOr8IwzLJP2MfGeTb0A35jauH+C2kbHKBr7yXKVQ=
+1 -1
View File
@@ -11,7 +11,7 @@ import (
)
func main() {
cfg := config.Load("config.json")
cfg := config.Load("config.toml")
os.MkdirAll(cfg.DataDir+"/files", 0755)