"flag"
"fmt"
"github.com/jhalter/mobius/hotline"
+ "github.com/jhalter/mobius/internal/mobius"
"gopkg.in/natefinch/lumberjack.v2"
"io"
"log"
"log/slog"
"net/http"
"os"
- "path/filepath"
+ "os/signal"
+ "path"
"runtime"
+ "syscall"
)
//go:embed mobius/config
"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())
- // 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():
- // }
- // }()
+ sigChan := make(chan os.Signal, 1)
+ signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT, os.Interrupt)
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.")
statsPort := flag.String("stats-port", "", "Enable stats HTTP endpoint on address and port")
configDir := flag.String("config", defaultConfigPath(), "Path to config root")
- printVersion := flag.Bool("version", false, "print version and exit")
+ printVersion := flag.Bool("version", false, "Print version and exit")
logLevel := flag.String("log-level", "info", "Log level")
logFile := flag.String("log-file", "", "Path to log file")
),
)
+ // 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)
+ }
+
+ config, err := mobius.LoadConfig(path.Join(*configDir, "config.yaml"))
+ if err != nil {
+ slogger.Error(fmt.Sprintf("Error loading config: %v", err))
os.Exit(1)
}
- srv, err := hotline.NewServer(*configDir, *netInterface, *basePort, slogger, &hotline.OSFileStore{})
+ srv, err := hotline.NewServer(*config, *configDir, *netInterface, *basePort, slogger, &hotline.OSFileStore{})
if err != nil {
slogger.Error(fmt.Sprintf("Error starting server: %s", err))
os.Exit(1)
}
+ srv.MessageBoard, err = mobius.NewFlatNews(path.Join(*configDir, "MessageBoard.txt"))
+ if err != nil {
+ slogger.Error(fmt.Sprintf("Error loading message board: %v", err))
+ os.Exit(1)
+ }
+
+ srv.BanList, err = mobius.NewBanFile(path.Join(*configDir, "Banlist.yaml"))
+ if err != nil {
+ slogger.Error(fmt.Sprintf("Error loading ban list: %v", err))
+ os.Exit(1)
+ }
+
+ srv.ThreadedNewsMgr, err = mobius.NewThreadedNewsYAML(path.Join(*configDir, "ThreadedNews.yaml"))
+ if err != nil {
+ slogger.Error(fmt.Sprintf("Error loading news: %v", err))
+ os.Exit(1)
+ }
+
sh := statHandler{hlServer: srv}
if *statsPort != "" {
http.HandleFunc("/", sh.RenderStats)
}(srv)
}
+ go func() {
+ for {
+ sig := <-sigChan
+ switch sig {
+ case syscall.SIGHUP:
+ slogger.Info("SIGHUP received. Reloading configuration.")
+
+ if err := srv.MessageBoard.(*mobius.FlatNews).Reload(); err != nil {
+ slogger.Error("Error reloading news", "err", err)
+ }
+
+ if err := srv.BanList.(*mobius.BanFile).Load(); err != nil {
+ slogger.Error("Error reloading ban list", "err", err)
+ }
+
+ if err := srv.ThreadedNewsMgr.(*mobius.ThreadedNewsYAML).Load(); err != nil {
+ slogger.Error("Error reloading threaded news list", "err", err)
+ }
+ default:
+ signal.Stop(sigChan)
+ cancel()
+ os.Exit(0)
+ }
+
+ }
+ }()
+
slogger.Info("Hotline server started",
"version", version,
"API port", fmt.Sprintf("%s:%v", *netInterface, *basePort),
)
// 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?
+// 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
}