Usually the examples provided by swagger setup simple servers, where all the api calls for json functions are on the document root. e.g. the famous "hello world" sits at "/hello" .
I want to find a way to define a given prefix, so that can put the a specific api under this prefix. if the prefix would be "/api/2.0", the example api would become
/hello --> /api/2.0/hello # The prefix would be "/api/2.0"
/goodbye --> /api/2.0/goodbye #
in the end i want to keep this dynamic and really provide a commandline switch with some
server -prefix '/api/2.0'
and the server would use this for the api. Note that i can then also specify no prefix, and the system should fall back to "/hello" and "/goodbye".
Is this possible with the go-lang implementation of swagger (https://github.com/go-swagger/go-swagger)
Can somebody point me to an example / or give a short example?
Addendum: I want to do this basically with a swagger 2.0 interface. I learned now that it needs something like this:
http.Handle("/", middlewareOne(middlewareTwo(finalHandler)))
Basically the task is to leave the swagger generated "finalHandler" untouched (so it thinks still handles "only" /hello, or /goodbye). But the middleware would catch all traffic before the finalHandler. The "/api/2.0" would be removed/transformed (this what i am searching, how???) and the middleware would pass this to the finalhandler, which still believes that it serves /hello. I am not sure if the information that it runs "/api/2.0" is ever neeeded, but mabybe it should be added as context.
See https://github.com/go-swagger/go-swagger/blob/master/docs/use/middleware.md
Quirky solution, which seems to work.
func setupGlobalMiddleware(handler http.Handler) http.Handler {
prefix:="/api/2.0"
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, prefix) {
r.URL.Path = r.URL.Path[len(prefix):]
}
handler.ServeHTTP(w, r)
})
}
I would rather prefer if would have access to the "handlers" map in the generated code, but it is not accessible. This way i could just "re-register" everything with a prefix. Maybe ther is a better solution?