From 6f342c8888686603a81800cbc800f65caf22b1ce Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Fri, 8 Sep 2023 12:45:46 -0400 Subject: [PATCH 1/4] Use anyhttp --- go.mod | 2 +- go.sum | 4 ++-- main.go | 5 ++++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index d302191..885ef53 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module go.balki.me/collage-maker go 1.21 require ( - github.com/oliamb/cutter v0.2.2 + go.balki.me/anyhttp v0.2.0 go.oneofone.dev/resize v1.0.1 ) diff --git a/go.sum b/go.sum index b1f0e63..5febfba 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ -github.com/oliamb/cutter v0.2.2 h1:Lfwkya0HHNU1YLnGv2hTkzHfasrSMkgv4Dn+5rmlk3k= -github.com/oliamb/cutter v0.2.2/go.mod h1:4BenG2/4GuRBDbVm/OPahDVqbrOemzpPiG5mi1iryBU= 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 1880d0b..db9a528 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( "log/slog" + "go.balki.me/anyhttp" "go.balki.me/collage-maker/collage" ) @@ -25,6 +26,7 @@ var ( localAssets bool collageNameGen *nameGen imagesDirFs fs.FS + listenAddr string // go:embed web/* webFS embed.FS @@ -34,6 +36,7 @@ func main() { flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir") flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir") flag.BoolVar(&localAssets, "local-assets", false, "Serve local assets for testing") + flag.StringVar(&listenAddr, "addr", "127.0.0.1:8767", "Sets the collages dir") flag.Parse() @@ -89,7 +92,7 @@ func main() { } w.Write([]byte(collageFile)) }) - if err := http.ListenAndServe(":8767", nil); err != nil { + if err := anyhttp.ListenAndServe(listenAddr, nil); err != nil { slog.Error("http ListenAndServe failed", "error", err) } } -- 2.34.1 From eb032c50f1bdfb540e8d53cb12264b0ea461f18e Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Mon, 11 Sep 2023 12:37:44 -0400 Subject: [PATCH 2/4] Add anyhttp idle support --- go.mod | 2 ++ go.sum | 2 -- main.go | 23 ++++++++++++++++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) 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 {} } } -- 2.34.1 From 0ab134a36486c184ea5de1e8289ec6f025bb32fd Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Mon, 11 Sep 2023 21:09:12 -0400 Subject: [PATCH 3/4] Remove local override for anyhttp --- go.mod | 4 +--- go.sum | 2 ++ main.go | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 4c3fd5d..8f385f8 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,8 @@ module go.balki.me/collage-maker go 1.21 require ( - go.balki.me/anyhttp v0.2.0 + go.balki.me/anyhttp v0.3.0 go.oneofone.dev/resize v1.0.1 ) require golang.org/x/image v0.1.0 // indirect - -replace go.balki.me/anyhttp => ../anyhttp diff --git a/go.sum b/go.sum index 8646212..0840792 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +go.balki.me/anyhttp v0.3.0 h1:WtBQ0rnkg567sX/O4ij/+qBbdCIUt5VURSe718sITBY= +go.balki.me/anyhttp v0.3.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 adb68fa..d0c31ab 100644 --- a/main.go +++ b/main.go @@ -96,13 +96,13 @@ func main() { slog.Error("Failed to write collageFile", "error", err) } }) - addrType, server, err := anyhttp.ListenAndServeHTTP(listenAddr, idle.WrapHandler(nil)) + addrType, server, done, err := anyhttp.Serve(listenAddr, idle.WrapHandler(nil)) if err != nil { - slog.Error("anyhttp ListenAndServeHTTP failed", "error", err) + slog.Error("anyhttp Serve failed", "error", err) } if addrType == anyhttp.SystemdFD { - if err := idle.Wait(1 * time.Minute); err != nil { + if err := idle.Wait(30 * 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 @@ -110,7 +110,7 @@ func main() { slog.Error("http server Shutdown failed", "error", err) } } else { - select {} + <-done } } -- 2.34.1 From 53229b4d208f9db0c0be302588bbe616c9dac4c8 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Mon, 11 Sep 2023 21:14:02 -0400 Subject: [PATCH 4/4] Fix listen address flag description --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index d0c31ab..2545ef3 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ func main() { flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir") flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir") flag.BoolVar(&localAssets, "local-assets", false, "Serve local assets for testing") - flag.StringVar(&listenAddr, "addr", "127.0.0.1:8767", "Sets the collages dir") + flag.StringVar(&listenAddr, "addr", "127.0.0.1:8767", "Web listen address, see https://pkg.go.dev/go.balki.me/anyhttp#readme-address-syntax") flag.Parse() -- 2.34.1