From a1b515074e52d22fdf7c3989a35bef5a4dc90198 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Wed, 11 Mar 2026 02:23:43 +0900 Subject: [PATCH] cmd/pkgserver: constant string in pattern This resolves patterns at compile time. Signed-off-by: Ophestra --- cmd/pkgserver/api.go | 12 +++++++----- cmd/pkgserver/api_test.go | 2 +- cmd/pkgserver/main.go | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/cmd/pkgserver/api.go b/cmd/pkgserver/api.go index f90d4de..2a3d26a 100644 --- a/cmd/pkgserver/api.go +++ b/cmd/pkgserver/api.go @@ -106,12 +106,14 @@ func (index *packageIndex) handleGet(w http.ResponseWriter, r *http.Request) { }{len(values), values}) } -const ApiVersion = "v1" +// apiVersion is the name of the current API revision, as part of the pattern. +const apiVersion = "v1" -func apiRoutes(mux *http.ServeMux, index *packageIndex) { - mux.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), handleInfo) - mux.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), index.handleGet) - mux.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), index.handleStatus) +// registerAPI registers API handler functions. +func (index *packageIndex) registerAPI(mux *http.ServeMux) { + mux.HandleFunc("GET /api/"+apiVersion+"/info", handleInfo) + mux.HandleFunc("GET /api/"+apiVersion+"/get", index.handleGet) + mux.HandleFunc("GET /api/"+apiVersion+"/status/", index.handleStatus) mux.HandleFunc("GET /status/", index.handleStatus) } diff --git a/cmd/pkgserver/api_test.go b/cmd/pkgserver/api_test.go index 26595d4..2337c92 100644 --- a/cmd/pkgserver/api_test.go +++ b/cmd/pkgserver/api_test.go @@ -12,7 +12,7 @@ import ( ) // prefix is prepended to every API path. -const prefix = "/api/" + ApiVersion + "/" +const prefix = "/api/" + apiVersion + "/" func TestAPIInfo(t *testing.T) { t.Parallel() diff --git a/cmd/pkgserver/main.go b/cmd/pkgserver/main.go index 9814ea1..9fa6d01 100644 --- a/cmd/pkgserver/main.go +++ b/cmd/pkgserver/main.go @@ -70,7 +70,7 @@ func main() { var mux http.ServeMux uiRoutes(&mux) - apiRoutes(&mux, &index) + index.registerAPI(&mux) server := http.Server{ Addr: flagAddr, Handler: &mux,