From 90c84117c09a30c0e2622c613f94906491b80ce8 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Sun, 28 Jul 2024 16:47:35 +0200 Subject: [PATCH 1/2] chore: switch to go-chi The net/http router has gone weird since Go 1.21. --- default.nix | 2 +- go.mod | 4 ++-- go.sum | 4 ++-- main.go | 35 +++++++++++++++++++---------------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/default.nix b/default.nix index eb17099..17431c2 100644 --- a/default.nix +++ b/default.nix @@ -7,7 +7,7 @@ rec { pname = "nar-serve"; version = "latest"; src = nixpkgs.lib.cleanSource ./.; - vendorHash = "sha256-IfXhuVwZf43FcQQ+i77aJHWG0auHBaHnKgTQJKa0L/M="; + vendorHash = "sha256-hi0KK+TQ3JG6LSMy8wnLDBRnTCwlfwo3ru22sbgX7dc="; doCheck = false; }; diff --git a/go.mod b/go.mod index 543635a..ad4e569 100644 --- a/go.mod +++ b/go.mod @@ -7,10 +7,11 @@ toolchain go1.22.5 require ( cloud.google.com/go/storage v1.43.0 github.com/aws/aws-sdk-go v1.55.3 + github.com/go-chi/chi/v5 v5.1.0 github.com/google/go-cmp v0.6.0 + github.com/klauspost/compress v1.17.9 github.com/stretchr/testify v1.9.0 github.com/ulikunitz/xz v0.5.12 - github.com/urfave/negroni v1.0.0 ) require ( @@ -29,7 +30,6 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/googleapis/gax-go/v2 v2.13.0 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect - github.com/klauspost/compress v1.17.9 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0 // indirect diff --git a/go.sum b/go.sum index fcaf208..9f3ff8b 100644 --- a/go.sum +++ b/go.sum @@ -28,6 +28,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= @@ -87,8 +89,6 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc= github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= -github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc= -github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0 h1:9G6E0TXzGFVfTnawRzrPl83iHOAV7L8NJiR8RSGYV1g= diff --git a/main.go b/main.go index 0ed32b5..1a045b6 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,15 @@ package main import ( "embed" - "net/http" "io" + "log" + "net/http" "os" unpack "github.com/numtide/nar-serve/api/unpack" - "github.com/urfave/negroni" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" ) //go:embed views/* @@ -40,20 +43,21 @@ func main() { addr = ":" + port } - mux := http.NewServeMux() - mux.HandleFunc("/", indexHandler) - mux.HandleFunc("/healthz", healthzHandler) - mux.HandleFunc("/robots.txt", robotsHandler) - mux.HandleFunc(unpack.MountPath, unpack.Handler) + r := chi.NewRouter() - // Includes some default middlewares - // Serve static files from ./public - n := negroni.New( - negroni.NewRecovery(), - negroni.NewLogger(), - ) - n.UseHandler(mux) - n.Run(addr) + r.Use(middleware.RequestID) + r.Use(middleware.Logger) + r.Use(middleware.Recoverer) + r.Use(middleware.CleanPath) + r.Use(middleware.GetHead) + + r.Get("/", indexHandler) + r.Get("/healthz", healthzHandler) + r.Get("/robots.txt", robotsHandler) + r.Get(unpack.MountPath, unpack.Handler) + + log.Println("addr=", addr) + log.Fatal(http.ListenAndServe(addr, r)) } func getEnv(name, def string) string { @@ -63,4 +67,3 @@ func getEnv(name, def string) string { } return value } - From 9ce6009c1c266b00ac75f2f550912fef28b56964 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Sun, 28 Jul 2024 16:48:19 +0200 Subject: [PATCH 2/2] chore: fix unpack package name Last remainder of now.sh --- api/unpack/index.go | 2 +- main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api/unpack/index.go b/api/unpack/index.go index a22fc7e..0d5fd21 100644 --- a/api/unpack/index.go +++ b/api/unpack/index.go @@ -1,4 +1,4 @@ -package handler +package unpack import ( "compress/bzip2" diff --git a/main.go b/main.go index 1a045b6..8ba5e6e 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ import ( "net/http" "os" - unpack "github.com/numtide/nar-serve/api/unpack" + "github.com/numtide/nar-serve/api/unpack" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware"