55 "net/http"
66 "net/url"
77 "os"
8+ "strings"
89
910 "github.com/gorilla/handlers"
1011 "github.com/gorilla/mux"
@@ -17,7 +18,7 @@ import (
1718 "github.com/rancher/steve/pkg/schemaserver/types"
1819)
1920
20- func ListenAndServe (ctx context.Context , address string , config * config.Config ) error {
21+ func ListenAndServe (ctx context.Context , address string , config * config.Config , pathPrefix string ) error {
2122 server := server .DefaultAPIServer ()
2223 server .Schemas .MustImportAndCustomize (model.Channel {}, func (schema * types.APISchema ) {
2324 schema .Store = store .New (config )
@@ -28,13 +29,16 @@ func ListenAndServe(ctx context.Context, address string, config *config.Config)
2829 schema .Store = release .New (config )
2930 schema .CollectionMethods = []string {http .MethodGet }
3031 })
31- apiroot .Register (server .Schemas , []string {"v1-release" }, nil )
3232
33+ pathPrefix = strings .TrimPrefix (pathPrefix , "/" )
34+ pathPrefix = strings .TrimSuffix (pathPrefix , "/" )
35+
36+ apiroot .Register (server .Schemas , []string {pathPrefix }, nil )
3337 router := mux .NewRouter ()
34- router .MatcherFunc (setType ("apiRoot" )).Path ("/" ).Handler (server )
35- router .MatcherFunc (setType ("apiRoot" )).Path ("/{name}" ).Handler (server )
36- router .Path ("/{prefix:v1-release }/{type}" ).Handler (server )
37- router .Path ("/{prefix:v1-release }/{type}/{name}" ).Handler (server )
38+ router .MatcherFunc (setType ("apiRoot" , pathPrefix )).Path ("/" ).Handler (server )
39+ router .MatcherFunc (setType ("apiRoot" , pathPrefix )).Path ("/{name}" ).Handler (server )
40+ router .Path ("/{prefix:" + pathPrefix + " }/{type}" ).Handler (server )
41+ router .Path ("/{prefix:" + pathPrefix + " }/{type}/{name}" ).Handler (server )
3842
3943 next := handlers .LoggingHandler (os .Stdout , router )
4044 handler := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
@@ -48,13 +52,13 @@ func ListenAndServe(ctx context.Context, address string, config *config.Config)
4852 return http .ListenAndServe (address , handler )
4953}
5054
51- func setType (t string ) mux.MatcherFunc {
55+ func setType (t string , pathPrefix string ) mux.MatcherFunc {
5256 return func (request * http.Request , match * mux.RouteMatch ) bool {
5357 if match .Vars == nil {
5458 match .Vars = map [string ]string {}
5559 }
5660 match .Vars ["type" ] = t
57- match .Vars ["prefix" ] = "v1-release"
61+ match .Vars ["prefix" ] = pathPrefix
5862 return true
5963 }
6064}
0 commit comments