"fmt"
"github.com/jhalter/mobius/hotline"
"github.com/jhalter/mobius/internal/mobius"
- "gopkg.in/natefinch/lumberjack.v2"
+ "github.com/oleksandr/bonjour"
"io"
"log"
- "log/slog"
"os"
"os/signal"
"path"
//go:embed mobius/config
var cfgTemplate embed.FS
-var logLevels = map[string]slog.Level{
- "debug": slog.LevelDebug,
- "info": slog.LevelInfo,
- "error": slog.LevelError,
-}
-
// Values swapped in by go-releaser at build time
var (
version = "dev"
commit = "none"
- date = "unknown"
)
func main() {
flag.Parse()
if *printVersion {
- fmt.Printf("mobius-hotline-server %s, commit %s, built at %s", version, commit, date)
+ fmt.Printf("mobius-hotline-server version %s, commit %s\n", version, commit)
os.Exit(0)
}
- slogger := slog.New(
- slog.NewTextHandler(
- io.MultiWriter(os.Stdout, &lumberjack.Logger{
- Filename: *logFile,
- MaxSize: 100, // MB
- MaxBackups: 3,
- MaxAge: 365, // days
- }),
- &slog.HandlerOptions{Level: logLevels[*logLevel]},
- ),
- )
+ slogger := mobius.NewLogger(logLevel, logFile)
// 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
}
}()
- slogger.Info("Hotline server started",
- "version", version,
- "config", *configDir,
- "API port", fmt.Sprintf("%s:%v", *netInterface, *basePort),
- "Transfer port", fmt.Sprintf("%s:%v", *netInterface, *basePort+1),
- )
+ slogger.Info("Hotline server started", "version", version, "config", *configDir)
// Assign functions to handle specific Hotline transaction types
mobius.RegisterHandlers(srv)
+ if srv.Config.EnableBonjour {
+ s, err := bonjour.Register(srv.Config.Name, "_hotline._tcp", "", *basePort, []string{"txtv=1", "app=hotline"}, nil)
+ if err != nil {
+ slogger.Error("Error registering Hotline server with Bonjour", "err", err)
+ }
+ defer s.Shutdown()
+ }
+
// Serve Hotline requests until program exit
log.Fatal(srv.ListenAndServe(ctx))
}