- commit
- 8339c4a366616270a7c9711baf79aab3cc392f4b
- parent
- 682ca66fd153ac09843bdd82db3724d0b5fd8f85
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2026-10-24 14:49
allow to run via systemd socket activation
Diffstat
| M | audiothek.go | 21 | ++++++++++++++++----- |
1 files changed, 16 insertions, 5 deletions
diff --git a/audiothek.go b/audiothek.go
@@ -332,6 +332,16 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
332 332 }
333 333 }
334 334
-1 335 func getListener(port int) (net.Listener, error) {
-1 336 if os.Getenv("LISTEN_PID") == strconv.Itoa(os.Getpid()) {
-1 337 f := os.NewFile(uintptr(3), "systemd-socket")
-1 338 return net.FileListener(f)
-1 339 } else {
-1 340 addr := fmt.Sprintf("localhost:%d", port)
-1 341 return net.Listen("tcp", addr)
-1 342 }
-1 343 }
-1 344
335 345 func main() {
336 346 port := 8000
337 347
@@ -344,8 +354,6 @@ func main() {
344 354 flag.StringVar(&origin, "o", "", "origin for links")
345 355 flag.Parse()
346 356
347 -1 addr := fmt.Sprintf("localhost:%d", port)
348 -1
349 357 http.HandleFunc("/", handle)
350 358 http.HandleFunc("/search/", handleSearch)
351 359
@@ -353,11 +361,14 @@ func main() {
353 361 context.Background(), os.Interrupt, syscall.SIGTERM,
354 362 )
355 363 ctxFactory := func(l net.Listener) context.Context { return ctx }
356 -1 server := &http.Server{Addr: addr, BaseContext: ctxFactory}
-1 364 server := &http.Server{BaseContext: ctxFactory}
357 365
358 366 go func() {
359 -1 log.Printf("Serving on http://%s", addr)
360 -1 err := server.ListenAndServe()
-1 367 listener, err := getListener(port)
-1 368 if err == nil {
-1 369 log.Printf("Serving on %s", listener.Addr())
-1 370 err = server.Serve(listener)
-1 371 }
361 372 if err != nil && err != http.ErrServerClosed {
362 373 log.Fatal(err)
363 374 }