From d5c4cfa9d096b003c44cdbde16b99526a05a1f05 Mon Sep 17 00:00:00 2001 From: Odonax Date: Thu, 30 Apr 2026 06:53:36 +0200 Subject: [PATCH] Switch configuration file from json to toml --- .gitignore | 2 +- config/config.go | 47 ++++++++++++++++++++++++++++++++++------------- go.mod | 2 ++ go.sum | 2 ++ main.go | 2 +- 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 1c38749..1b074fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ data/ -config.json +config.toml *~ *.kate-swp .*.kate-swp diff --git a/config/config.go b/config/config.go index 8ba6c62..3b1cf7f 100644 --- a/config/config.go +++ b/config/config.go @@ -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 +} diff --git a/go.mod b/go.mod index 2a8eb01..79e2773 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index cf1e561..e5adf48 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 67380c1..9129385 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( ) func main() { - cfg := config.Load("config.json") + cfg := config.Load("config.toml") os.MkdirAll(cfg.DataDir+"/files", 0755)