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) {
|
||||
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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user