cmd/pkgserver: do not assume default mux

This helps with testing.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-03-10 23:59:16 +09:00
parent 4a63fbbc2a
commit fa9bc70b39
3 changed files with 12 additions and 12 deletions

View File

@@ -36,7 +36,7 @@ func serveInfo(index *PackageIndex) func(http.ResponseWriter, *http.Request) {
}
}
func serveStatus(index *PackageIndex, cache *pkg.Cache) func(w http.ResponseWriter, r *http.Request) {
func serveStatus(index *PackageIndex) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
download := path.Dir(r.URL.Path) == "/status"
if index == nil {
@@ -125,11 +125,11 @@ func serveGet(index *PackageIndex) func(http.ResponseWriter, *http.Request) {
const ApiVersion = "v1"
func apiRoutes(index *PackageIndex, cache *pkg.Cache) {
http.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), serveInfo(index))
http.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), serveGet(index))
http.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), serveStatus(index, cache))
http.HandleFunc("GET /status/", serveStatus(index, cache))
func apiRoutes(mux *http.ServeMux, index *PackageIndex) {
mux.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), serveInfo(index))
mux.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), serveGet(index))
mux.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), serveStatus(index))
mux.HandleFunc("GET /status/", serveStatus(index))
}
func WritePayload(w http.ResponseWriter, payload any) {

View File

@@ -49,8 +49,8 @@ func main() {
if err != nil {
return err
}
uiRoutes()
apiRoutes(index, cache)
uiRoutes(http.DefaultServeMux)
apiRoutes(http.DefaultServeMux, index)
err = http.ListenAndServe(fmt.Sprintf(":%d", flagPort), nil)
if err != nil {
return err

View File

@@ -40,8 +40,8 @@ func serveStaticContent(w http.ResponseWriter, r *http.Request) {
}
}
func uiRoutes() {
http.HandleFunc("GET /{$}", serveWebUI)
http.HandleFunc("GET /favicon.ico", serveStaticContent)
http.HandleFunc("GET /static/", serveStaticContent)
func uiRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /{$}", serveWebUI)
mux.HandleFunc("GET /favicon.ico", serveStaticContent)
mux.HandleFunc("GET /static/", serveStaticContent)
}