From c9dababf7f5075f99fb6eaa1d0acd302d31e660b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastien=20Cl=C3=A9ment?= Date: Sat, 13 Jul 2024 21:28:29 +0200 Subject: [PATCH] Allow to listen on UNIX socket --- main.go | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 5d7a118..06d7ddc 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,11 @@ import ( "context" _ "embed" "errors" + "flag" "fmt" "io" "log/slog" + "net" "net/http" "os" "os/signal" @@ -32,7 +34,12 @@ var mountPoints []MountPoint //go:embed page.html var pageHtml []byte +var port = flag.Int("port", 8080, "port to listen on") +var socket = flag.String("socket", "", "socket to listen on") + func main() { + flag.Parse() + prepareMountPoints() slog.Info("initializing", "mountPoints", mountPoints) @@ -40,17 +47,29 @@ func main() { client, err = storage.NewClient(context.Background(), storage.WithJSONReads()) if err != nil { slog.Error("failed to create storage client", "err", err) - os.Exit(3) + os.Exit(4) } - server := &http.Server{Addr: fmt.Sprintf(":%s", envOr("PORT", "8080"))} + server := &http.Server{} http.HandleFunc("/", handle) + var listener net.Listener + if *socket != "" { + slog.Info("listening on socket", "socket", *socket) + listener, err = net.Listen("unix", *socket) + } else { + slog.Info("listening on port", "port", *port) + listener, err = net.Listen("tcp", fmt.Sprintf(":%d", *port)) + } + if err != nil { + slog.Error("failed to listen", "err", err) + os.Exit(3) + } + go func() { - slog.Info("server started", "addr", server.Addr) - if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { + if err := server.Serve(listener); !errors.Is(err, http.ErrServerClosed) { slog.Error("server error", "err", err) - os.Exit(4) + os.Exit(5) } slog.Warn("server stopped") }() @@ -67,26 +86,19 @@ func main() { if err := server.Shutdown(shutdownCtx); err != nil { slog.Error("shutdown error", "err", err) - os.Exit(5) + os.Exit(6) } slog.Info("shutdown completed") } -func envOr(key, defaultValue string) string { - if value, ok := os.LookupEnv(key); ok { - return value - } else { - return defaultValue - } -} - func prepareMountPoints() { - if len(os.Args) < 2 { + args := flag.Args() + if len(args) < 1 { fmt.Fprintf(os.Stderr, "Usage: %s path:bucket:prefix [path:bucket:prefix ...]\n", os.Args[0]) os.Exit(1) } - for _, arg := range os.Args[1:] { + for _, arg := range args { mountPointParts := strings.SplitN(arg, ":", 3) if len(mountPointParts) != 3 { slog.Error("invalid mount point", "arg", arg, "reason", "expected 'path:bucket:prefix'")