Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d5c4cfa9d0 | ||
|
|
9f6c5b2880 |
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
data/
|
data/
|
||||||
config.json
|
config.toml
|
||||||
*~
|
*~
|
||||||
*.kate-swp
|
*.kate-swp
|
||||||
.*.kate-swp
|
.*.kate-swp
|
||||||
|
|||||||
+34
-13
@@ -1,15 +1,18 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/BurntSushi/toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SiteName string `json:"site_name"`
|
SiteName string `toml:"site_name"`
|
||||||
Port string `json:"port"`
|
Port string `toml:"port"`
|
||||||
DataDir string `json:"data_dir"`
|
DataDir string `toml:"data_dir"`
|
||||||
|
AdminUser string `toml:"admin_user"`
|
||||||
|
AdminPasswordHash string `toml:"admin_password_hash"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Load(path string) Config {
|
func Load(path string) Config {
|
||||||
@@ -19,18 +22,36 @@ func Load(path string) Config {
|
|||||||
DataDir: "data",
|
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 {
|
if err != nil {
|
||||||
log.Println("No config file found, using defaults. Consider setting up a Site Name at the very least!")
|
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
|
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
|
||||||
|
}
|
||||||
|
|||||||
Binary file not shown.
@@ -3,3 +3,5 @@ module 192.168.1.180/odonax/filehost-athome
|
|||||||
go 1.26.2
|
go 1.26.2
|
||||||
|
|
||||||
require github.com/mattn/go-sqlite3 v1.14.44
|
require github.com/mattn/go-sqlite3 v1.14.44
|
||||||
|
|
||||||
|
require github.com/BurntSushi/toml v1.6.0 // indirect
|
||||||
|
|||||||
@@ -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 h1:3VSe+xafpbzsLbdr2AWlAZk9yRHiBhTBakioXaCKTF8=
|
||||||
github.com/mattn/go-sqlite3 v1.14.44/go.mod h1:pjEuOr8IwzLJP2MfGeTb0A35jauH+C2kbHKBr7yXKVQ=
|
github.com/mattn/go-sqlite3 v1.14.44/go.mod h1:pjEuOr8IwzLJP2MfGeTb0A35jauH+C2kbHKBr7yXKVQ=
|
||||||
|
|||||||
Reference in New Issue
Block a user