]> git.r.bdr.sh - rbdr/mobius/blobdiff - cmd/mobius-hotline-server/main.go
Fix Windows compatibility for -init flag
[rbdr/mobius] / cmd / mobius-hotline-server / main.go
index 8e66938a76131fb8b31539b0dd2ad7c0177d3a78..1e9b5b6fd55f77eb1758f888cf84af7492923523 100644 (file)
@@ -13,7 +13,7 @@ import (
        "log/slog"
        "net/http"
        "os"
-       "path/filepath"
+       "path"
        "runtime"
 )
 
@@ -28,6 +28,7 @@ var logLevels = map[string]slog.Level{
        "error": slog.LevelError,
 }
 
+// Values swapped in by go-releaser at build time
 var (
        version = "dev"
        commit  = "none"
@@ -35,22 +36,22 @@ var (
 )
 
 func main() {
-       ctx, cancel := context.WithCancel(context.Background())
+       ctx, _ := context.WithCancel(context.Background())
 
        // TODO: implement graceful shutdown by closing context
-       // c := make(chan os.Signal, 1)
-       // signal.Notify(c, os.Interrupt)
-       // defer func() {
-       //      signal.Stop(c)
-       //      cancel()
-       // }()
-       // go func() {
-       //      select {
-       //      case <-c:
-       //              cancel()
-       //      case <-ctx.Done():
-       //      }
-       // }()
+       //c := make(chan os.Signal, 1)
+       //signal.Notify(c, os.Interrupt)
+       //defer func() {
+       //      signal.Stop(c)
+       //      cancel()
+       //}()
+       //go func() {
+       //      select {
+       //      case <-c:
+       //              cancel()
+       //      case <-ctx.Done():
+       //      }
+       //}()
 
        netInterface := flag.String("interface", "", "IP addr of interface to listen on.  Defaults to all interfaces.")
        basePort := flag.Int("bind", defaultPort, "Base Hotline server port.  File transfer port is base port + 1.")
@@ -81,14 +82,15 @@ func main() {
                ),
        )
 
+       // It's important for Windows compatibility to use path.Join and not filepath.Join for the config dir initialization.
+       // https://github.com/golang/go/issues/44305
        if *init {
-               if _, err := os.Stat(filepath.Join(*configDir, "/config.yaml")); os.IsNotExist(err) {
+               if _, err := os.Stat(path.Join(*configDir, "/config.yaml")); os.IsNotExist(err) {
                        if err := os.MkdirAll(*configDir, 0750); err != nil {
                                slogger.Error(fmt.Sprintf("error creating config dir: %s", err))
                                os.Exit(1)
                        }
-
-                       if err := copyDir("mobius/config", *configDir); err != nil {
+                       if err := copyDir(path.Join("mobius", "config"), *configDir); err != nil {
                                slogger.Error(fmt.Sprintf("error copying config dir: %s", err))
                                os.Exit(1)
                        }
@@ -99,7 +101,7 @@ func main() {
        }
 
        if _, err := os.Stat(*configDir); os.IsNotExist(err) {
-               slogger.Error(fmt.Sprintf("Configuration directory not found.  Correct the path or re-run with -init to generate initial config."))
+               slogger.Error("Configuration directory not found.  Correct the path or re-run with -init to generate initial config.")
                os.Exit(1)
        }
 
@@ -122,8 +124,14 @@ func main() {
                }(srv)
        }
 
+       slogger.Info("Hotline server started",
+               "version", version,
+               "API port", fmt.Sprintf("%s:%v", *netInterface, *basePort),
+               "Transfer port", fmt.Sprintf("%s:%v", *netInterface, *basePort+1),
+       )
+
        // Serve Hotline requests until program exit
-       log.Fatal(srv.ListenAndServe(ctx, cancel))
+       log.Fatal(srv.ListenAndServe(ctx))
 }
 
 type statHandler struct {
@@ -139,7 +147,9 @@ func (sh *statHandler) RenderStats(w http.ResponseWriter, _ *http.Request) {
        _, _ = io.WriteString(w, string(u))
 }
 
-func defaultConfigPath() (cfgPath string) {
+func defaultConfigPath() string {
+       var cfgPath string
+
        switch runtime.GOOS {
        case "windows":
                cfgPath = "config/"
@@ -158,7 +168,25 @@ func defaultConfigPath() (cfgPath string) {
        return cfgPath
 }
 
-// TODO: Simplify this mess.  Why is it so difficult to recursively copy a directory?
+// copyFile copies a file from src to dst. If dst does not exist, it is created.
+func copyFile(src, dst string) error {
+       sourceFile, err := os.Open(src)
+       if err != nil {
+               return err
+       }
+       defer sourceFile.Close()
+
+       destinationFile, err := os.Create(dst)
+       if err != nil {
+               return err
+       }
+       defer destinationFile.Close()
+
+       _, err = io.Copy(destinationFile, sourceFile)
+       return err
+}
+
+// copyDir recursively copies a directory tree, attempting to preserve permissions.
 func copyDir(src, dst string) error {
        entries, err := cfgTemplate.ReadDir(src)
        if err != nil {
@@ -166,19 +194,19 @@ func copyDir(src, dst string) error {
        }
        for _, dirEntry := range entries {
                if dirEntry.IsDir() {
-                       if err := os.MkdirAll(filepath.Join(dst, dirEntry.Name()), 0777); err != nil {
+                       if err := os.MkdirAll(path.Join(dst, dirEntry.Name()), 0777); err != nil {
                                panic(err)
                        }
-                       subdirEntries, _ := cfgTemplate.ReadDir(filepath.Join(src, dirEntry.Name()))
+                       subdirEntries, _ := cfgTemplate.ReadDir(path.Join(src, dirEntry.Name()))
                        for _, subDirEntry := range subdirEntries {
-                               f, err := os.Create(filepath.Join(dst, dirEntry.Name(), subDirEntry.Name()))
+                               f, err := os.Create(path.Join(dst, dirEntry.Name(), subDirEntry.Name()))
                                if err != nil {
                                        return err
                                }
 
-                               srcFile, err := cfgTemplate.Open(filepath.Join(src, dirEntry.Name(), subDirEntry.Name()))
+                               srcFile, err := cfgTemplate.Open(path.Join(src, dirEntry.Name(), subDirEntry.Name()))
                                if err != nil {
-                                       return err
+                                       return fmt.Errorf("error copying srcFile: %w", err)
                                }
                                _, err = io.Copy(f, srcFile)
                                if err != nil {
@@ -187,12 +215,12 @@ func copyDir(src, dst string) error {
                                f.Close()
                        }
                } else {
-                       f, err := os.Create(filepath.Join(dst, dirEntry.Name()))
+                       f, err := os.Create(path.Join(dst, dirEntry.Name()))
                        if err != nil {
                                return err
                        }
 
-                       srcFile, err := cfgTemplate.Open(filepath.Join(src, dirEntry.Name()))
+                       srcFile, err := cfgTemplate.Open(path.Join(src, dirEntry.Name()))
                        if err != nil {
                                return err
                        }