cmd/pkgserver: constant string in pattern

This resolves patterns at compile time.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-03-11 02:23:43 +09:00
parent e130443cf4
commit a1b515074e
3 changed files with 9 additions and 7 deletions

View File

@@ -106,12 +106,14 @@ func (index *packageIndex) handleGet(w http.ResponseWriter, r *http.Request) {
}{len(values), values}) }{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) { // registerAPI registers API handler functions.
mux.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), handleInfo) func (index *packageIndex) registerAPI(mux *http.ServeMux) {
mux.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), index.handleGet) mux.HandleFunc("GET /api/"+apiVersion+"/info", handleInfo)
mux.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), index.handleStatus) mux.HandleFunc("GET /api/"+apiVersion+"/get", index.handleGet)
mux.HandleFunc("GET /api/"+apiVersion+"/status/", index.handleStatus)
mux.HandleFunc("GET /status/", index.handleStatus) mux.HandleFunc("GET /status/", index.handleStatus)
} }

View File

@@ -12,7 +12,7 @@ import (
) )
// prefix is prepended to every API path. // prefix is prepended to every API path.
const prefix = "/api/" + ApiVersion + "/" const prefix = "/api/" + apiVersion + "/"
func TestAPIInfo(t *testing.T) { func TestAPIInfo(t *testing.T) {
t.Parallel() t.Parallel()

View File

@@ -70,7 +70,7 @@ func main() {
var mux http.ServeMux var mux http.ServeMux
uiRoutes(&mux) uiRoutes(&mux)
apiRoutes(&mux, &index) index.registerAPI(&mux)
server := http.Server{ server := http.Server{
Addr: flagAddr, Addr: flagAddr,
Handler: &mux, Handler: &mux,