metosin / reitit

A fast data-driven routing library for Clojure/Script
https://cljdoc.org/d/metosin/reitit/
Eclipse Public License 1.0
1.43k stars 256 forks source link

swagger-ui with service not at root path #233

Open lilactown opened 5 years ago

lilactown commented 5 years ago

swagger-ui works while developing locally: localhost:4321/ redirects me to the swagger UI and I can send requests.

However, once deployed, my service is routed via domain.com/service-name/.

This causes 3 problems:

  1. The redirect when I go to domain.com/service-name/ goes to domain.com/index.html
  2. The swagger file by default is looked for at domain.com/swagger.json. Fixable by specifying full URL in :url config
  3. All API requests made through swagger-ui go to domain.com/endpoint, rather than domain.com/service-name/endpoint

What I want is:

  1. The redirect would go from domain.com/service-name/ to domain.com/service-name/index.html
  2. The swagger file can be specified by relative URL: swagger.json
  3. All API requests made through swagger-ui are relative
ikitommi commented 5 years ago

There is an option for this in the swagger spec called :basePath. if you add {:swagger {:basePath "/service-name"}} to the route data, it should add that to all paths.

I think that could be an example in the docs (https://github.com/metosin/reitit/blob/master/doc/ring/swagger.md)...

ikitommi commented 5 years ago

added an example to the docs.

lilactown commented 5 years ago

Thanks! The swagger :basePath did fix the API requests from swagger-ui (number 3).

However, the redirect from e.g. /service-name/api-docs/ still goes to /api-docs/index.html, instead of /service-name/api-docs/index.html.

markbastian commented 4 years ago

I'm running into the same issue when I try to convert an AWS Lambda being used with the API Gateway to a ring request being serviced with reitit. The index.html and swagger-ui.js routes don't prepend the basePath when the redirect occurs.

ccfontes commented 3 years ago

Did anyone find a solution to the index.html redirect problem? In my case /service-name is redirected to /index.html, yet I can find the resource at service-name/index.html.

lamplification commented 3 years ago

Did anyone find a solution to the index.html redirect problem? In my case /service-name is redirected to /index.html, yet I can find the resource at service-name/index.html.

yes, I ran into the same problem yesterday. Basically, I think swagger-ui generate all the swagger-ui stuff in the express path/. Someone suggested ngnix rewrite in stackoverflow. I am able to resolve that by redirected in express as following.

// serve the swagger ui in a temporary directory (you can go to "/service-name/temp-api-docs/" to see the swaggerUI")
app.use('/temp-api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

// generate the redirect path with your service-name, please notice the last slash
const apiDocsRedirectPath = "/service-name".concat('/temp-api-docs/');
// redirect the path that you want to what swaggerUI serving
app.get('/api-docs', function(req, res) {
    res.redirect(apiDocsRedirectPath);
});

// when you hit "/service-name/api-docs" which will redirect to "/service-name/temp-api-docs/"