diff --git a/conf/Caddyfile b/conf/Caddyfile index 62e9986..5f46415 100644 --- a/conf/Caddyfile +++ b/conf/Caddyfile @@ -27,7 +27,7 @@ auto_https disable_redirects on_demand_tls { - permission http http://{$GIT_PAGES_ADDRESS:localhost}:3001 + permission http http://localhost:3001 } servers :80 { @@ -46,12 +46,18 @@ } } +(backend_pages) { + @h2c `{env.FEATURES}.matches(r"\bh2c\b")` + reverse_proxy @h2c h2c://{$GIT_PAGES_ADDRESS:localhost}:3000 + reverse_proxy http://{$GIT_PAGES_ADDRESS:localhost}:3000 +} + http:// { @get method GET redir @get https://{host}{uri} 301 # initial PUT/POST for a new domain has to happen over HTTP - reverse_proxy h2c://{$GIT_PAGES_ADDRESS:localhost}:3000 + import backend_pages } https:// { @@ -60,9 +66,9 @@ https:// { } encode - reverse_proxy h2c://{$GIT_PAGES_ADDRESS:localhost}:3000 + import backend_pages } http://localhost:2002 { - reverse_proxy h2c://{$GIT_PAGES_ADDRESS:localhost}:3002 + reverse_proxy http://localhost:3002 } diff --git a/src/main.go b/src/main.go index 1174aaf..13ec16c 100644 --- a/src/main.go +++ b/src/main.go @@ -7,12 +7,23 @@ import ( "log/slog" "net" "net/http" + "os" "runtime/debug" + "slices" "strings" "github.com/KimMachineGun/automemlimit/memlimit" ) +var features []string + +func FeatureActive(feature string) bool { + if features == nil { + features = strings.Split(strings.ToLower(os.Getenv("FEATURES")), ",") + } + return slices.Contains(features, strings.ToLower(feature)) +} + func listen(name string, listen string) net.Listener { if listen == "" { return nil @@ -57,7 +68,9 @@ func serve(listener net.Listener, serve func(http.ResponseWriter, *http.Request) server := http.Server{Handler: handler} server.Protocols = new(http.Protocols) server.Protocols.SetHTTP1(true) - server.Protocols.SetUnencryptedHTTP2(true) + if FeatureActive("h2c") { + server.Protocols.SetUnencryptedHTTP2(true) + } log.Fatalln(server.Serve(listener)) } }