"log/slog"
"net/http"
"os"
- "path/filepath"
+ "path"
"runtime"
)
"error": slog.LevelError,
}
+// Values swapped in by go-releaser at build time
var (
version = "dev"
commit = "none"
)
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.")
),
)
+ // 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)
}
}
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)
}
}(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 {
_, _ = io.WriteString(w, string(u))
}
-func defaultConfigPath() (cfgPath string) {
+func defaultConfigPath() string {
+ var cfgPath string
+
switch runtime.GOOS {
case "windows":
cfgPath = "config/"
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 {
}
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 {
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
}