forked from security/hakurei
cmd/pkgserver: constant string in pattern
This resolves patterns at compile time. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user