forked from security/hakurei
cmd/pkgserver: do not assume default mux
This helps with testing. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
download := path.Dir(r.URL.Path) == "/status"
|
download := path.Dir(r.URL.Path) == "/status"
|
||||||
if index == nil {
|
if index == nil {
|
||||||
@@ -125,11 +125,11 @@ func serveGet(index *PackageIndex) func(http.ResponseWriter, *http.Request) {
|
|||||||
|
|
||||||
const ApiVersion = "v1"
|
const ApiVersion = "v1"
|
||||||
|
|
||||||
func apiRoutes(index *PackageIndex, cache *pkg.Cache) {
|
func apiRoutes(mux *http.ServeMux, index *PackageIndex) {
|
||||||
http.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), serveInfo(index))
|
mux.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), serveInfo(index))
|
||||||
http.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), serveGet(index))
|
mux.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), serveGet(index))
|
||||||
http.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), serveStatus(index, cache))
|
mux.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), serveStatus(index))
|
||||||
http.HandleFunc("GET /status/", serveStatus(index, cache))
|
mux.HandleFunc("GET /status/", serveStatus(index))
|
||||||
}
|
}
|
||||||
|
|
||||||
func WritePayload(w http.ResponseWriter, payload any) {
|
func WritePayload(w http.ResponseWriter, payload any) {
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
uiRoutes()
|
uiRoutes(http.DefaultServeMux)
|
||||||
apiRoutes(index, cache)
|
apiRoutes(http.DefaultServeMux, index)
|
||||||
err = http.ListenAndServe(fmt.Sprintf(":%d", flagPort), nil)
|
err = http.ListenAndServe(fmt.Sprintf(":%d", flagPort), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -40,8 +40,8 @@ func serveStaticContent(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func uiRoutes() {
|
func uiRoutes(mux *http.ServeMux) {
|
||||||
http.HandleFunc("GET /{$}", serveWebUI)
|
mux.HandleFunc("GET /{$}", serveWebUI)
|
||||||
http.HandleFunc("GET /favicon.ico", serveStaticContent)
|
mux.HandleFunc("GET /favicon.ico", serveStaticContent)
|
||||||
http.HandleFunc("GET /static/", serveStaticContent)
|
mux.HandleFunc("GET /static/", serveStaticContent)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user