Skip to content

Commit

Permalink
Allow to listen on UNIX socket
Browse files Browse the repository at this point in the history
  • Loading branch information
BastienClement committed Jul 13, 2024
1 parent dccff9d commit c9dabab
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"context"
_ "embed"
"errors"
"flag"
"fmt"
"io"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
Expand All @@ -32,25 +34,42 @@ 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)

var err error
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")
}()
Expand All @@ -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'")
Expand Down

0 comments on commit c9dabab

Please sign in to comment.