leech/config/config.go

41 lines
744 B
Go
Raw Normal View History

2024-04-27 12:16:43 +02:00
package config
2024-04-27 12:40:07 +02:00
import (
"encoding/json"
"log"
"os"
"path/filepath"
)
2024-04-27 12:16:43 +02:00
2024-04-27 12:40:07 +02:00
type ConfigS struct {
Host string `json:"host"`
ServeDirs map[string]string `json:"servedirs"`
}
var Config ConfigS
func Init() {
chosenConfigPath := "/etc/leech/"
// Deal with a JSON configuration file in that folder.
configFile := filepath.Join(chosenConfigPath, "config.json")
// Does the file not exist?
if _, err := os.Stat(configFile); os.IsNotExist(err) {
log.Fatal("config file couldn't be found")
} else {
// Load the existing file.
fh, err := os.Open(configFile)
if err != nil {
panic(err)
}
defer fh.Close()
decoder := json.NewDecoder(fh)
2024-06-10 19:50:11 +02:00
err = decoder.Decode(&Config)
if err != nil {
panic(err)
}
2024-04-27 12:40:07 +02:00
}
2024-04-27 12:16:43 +02:00
}