diff --git a/go.mod b/go.mod index 885ef53..4c3fd5d 100644 --- a/go.mod +++ b/go.mod @@ -8,3 +8,5 @@ require ( ) require golang.org/x/image v0.1.0 // indirect + +replace go.balki.me/anyhttp => ../anyhttp diff --git a/go.sum b/go.sum index 5febfba..8646212 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,4 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.balki.me/anyhttp v0.2.0 h1:W6aGcmjF5CMJvJYtbYCywxnYoErFhFc76vwaqUG5FAQ= -go.balki.me/anyhttp v0.2.0/go.mod h1:JhfekOIjgVODoVqUCficjpIgmB3wwlB7jhN0eN2EZ/s= go.oneofone.dev/resize v1.0.1 h1:HjpVar/4pxMGrjO44ThaMX1Q5UOBw0KxzbxxRDZPQuA= go.oneofone.dev/resize v1.0.1/go.mod h1:zGFmn7q4EUZVlnDmxqf+b0mWpxsTt0MH2yx6ng8tpq0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/main.go b/main.go index db9a528..adb68fa 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "crypto/rand" "embed" "encoding/json" @@ -17,6 +18,7 @@ import ( "log/slog" "go.balki.me/anyhttp" + "go.balki.me/anyhttp/idle" "go.balki.me/collage-maker/collage" ) @@ -90,10 +92,25 @@ func main() { w.WriteHeader(http.StatusInternalServerError) return } - w.Write([]byte(collageFile)) + if _, err := w.Write([]byte(collageFile)); err != nil { + slog.Error("Failed to write collageFile", "error", err) + } }) - if err := anyhttp.ListenAndServe(listenAddr, nil); err != nil { - slog.Error("http ListenAndServe failed", "error", err) + addrType, server, err := anyhttp.ListenAndServeHTTP(listenAddr, idle.WrapHandler(nil)) + if err != nil { + slog.Error("anyhttp ListenAndServeHTTP failed", "error", err) + } + + if addrType == anyhttp.SystemdFD { + if err := idle.Wait(1 * time.Minute); err != nil { + slog.Error("Failed to wait for idler", "error", err) + } + ctx, _ := context.WithTimeout(context.Background(), 1*time.Minute) // Don't want any stuck connections + if err := server.Shutdown(ctx); err != nil { + slog.Error("http server Shutdown failed", "error", err) + } + } else { + select {} } }