Skip to content

Commit 1121659

Browse files
authored
Merge pull request #7 from MonzElmasry/add_prefix
add path-prefix flag
2 parents 647dd1a + 744e5ef commit 1121659

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var (
2424
ListenAddress string
2525
SubKey string
2626
ChannelServerVersion string
27+
PathPrefix string
2728
)
2829

2930
func main() {
@@ -58,6 +59,12 @@ func main() {
5859
EnvVar: "CHANNEL_SERVER_VERSION",
5960
Destination: &ChannelServerVersion,
6061
},
62+
cli.StringFlag{
63+
Name: "path-prefix",
64+
EnvVar: "PATH_PREFIX",
65+
Value: "v1-release",
66+
Destination: &PathPrefix,
67+
},
6168
}
6269
app.Action = run
6370

@@ -80,5 +87,5 @@ func run(c *cli.Context) error {
8087
return err
8188
}
8289

83-
return server.ListenAndServe(ctx, ListenAddress, config)
90+
return server.ListenAndServe(ctx, ListenAddress, config, PathPrefix)
8491
}

pkg/server/server.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
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

Comments
 (0)