diff --git a/config.json b/config.json new file mode 100644 index 0000000..eedf3f0 --- /dev/null +++ b/config.json @@ -0,0 +1,6 @@ +{ + "host": ":3125", + "servedirs": { + "leech": "/home/ontake/Dev/go/leech" + } +} diff --git a/config/config.go b/config/config.go index f236258..fc48851 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,37 @@ package config -var Host = ":3125" +import ( + "encoding/json" + "log" + "os" + "path/filepath" +) -var ServeDirs = map[string]string{ - "leech": "/home/ontake/Dev/go/leech", +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) + decoder.Decode(&Config) + } } diff --git a/leech b/leech index 51cb351..0c95f6c 100755 Binary files a/leech and b/leech differ diff --git a/main.go b/main.go index c717fda..6c37067 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( var assetsEmbed embed.FS func main() { + config.Init() app := fiber.New() app.Use(logger.New(logger.Config{ Format: "[${ip}]:${port} ${status} - ${method} ${path}\n", @@ -28,9 +29,9 @@ func main() { Browse: true, })) - for dirName, dirToServe := range config.ServeDirs { + for dirName, dirToServe := range config.Config.ServeDirs { app.Static("/serve/"+dirName, dirToServe) } - app.Listen(config.Host) + app.Listen(config.Config.Host) } diff --git a/route/route.go b/route/route.go index 7dbdc62..2de0f0e 100644 --- a/route/route.go +++ b/route/route.go @@ -16,15 +16,15 @@ func HandleList(c *fiber.Ctx) error { req := c.Params("req") c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) if req == "" { - keys := make([]string, 0, len(config.ServeDirs)) - for k := range config.ServeDirs { + keys := make([]string, 0, len(config.Config.ServeDirs)) + for k := range config.Config.ServeDirs { keys = append(keys, k) } sort.Slice(keys, func(i, j int) bool { return i < j }) - entries := make([]html.Entry, 0, len(config.ServeDirs)) + entries := make([]html.Entry, 0, len(config.Config.ServeDirs)) for _, key := range keys { entries = append(entries, html.Entry{ Name: key, @@ -36,7 +36,7 @@ func HandleList(c *fiber.Ctx) error { return c.SendString(html.FileListPage(req, entries)) } else { pathSlice := strings.Split(req, ".") - pathBase, ok := config.ServeDirs[pathSlice[0]] + pathBase, ok := config.Config.ServeDirs[pathSlice[0]] if ok { pathSlice[0] = pathBase completePath := path.Join(pathSlice...)