-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sugar API for defining URL redirects #2022
Comments
@bajtos |
#2314 and #2512 implement redirects to static targets, where the target location is known at the time when the redirect is registered. To support redirects used by REST (redirect to externally hosted swagger-ui) and REST API Explorer (redirect from I am re-posting my proposal from #2314 (review): I would like to propose a solution that's more generic and can be useful to more consumers. The built-in redirect needs to obtain the full URL (including protocol, hostname and port) before the redirect URL can be constructed. Inside I am proposing to modify // simple redirect for ease of use
app.redirect('/foo', '/bar');
// the built-in redirect
app.redirect('/explorer', ({protocol, host, basePath} => {
const baseUrl = protocol === 'http' ? config.httpUrl : config.url
// host is "hostname:port"
const openApiUrl = `${protocol}://${host}${basePath}/openapi.json`;
const fullUrl = `${baseUrl}?url=${openApiUrl}`;
});
// api-explorer redirect, "urlPath" is the requested path (URL)
app.redirect('/explorer', {urlPath} => urlPath + '/');
// or even simpler - depending on how basePath is applied
app.redirect('/explorer', '/explorer/'); #2314 (comment) (...) Different applications will have different needs when it comes to target URLs. For example, when running behind a url-rewriting reverse proxy, it's probably easiest to redirect to a path only (e.g. That's why I proposed that LB4 runtime should provide |
This is a follow-up for #2014.
When using LB4+ to serve HTML pages, it's useful to add a trailing slash to the URL when serving a folder, for example redirect
/explorer
to/explorer/
. Without this redirect, relative URLs to assets like CSS & JS files are incorrectly resolved. For example, when served from/explorer
, relative links like./swagger-ui.css
are resolved in the parent directory, e.g./swagger-ui.css
instead of/explorer/swagger-ui.css
.Right now, a redirect can be implemented using a controller route that's hidden from the documentation and uses HTTP
response
object to send back the redirect. Such solution requires a lot of code and feels a bit hacky.Let's make redirects a first-class feature in LB4 and provide a high-level API that's easy to use.
For example:
Under the hood, this can be implemented as a new Route class, for example:
A mock-up implementation of RedirectRoute:
Acceptance criteria
#2512
RestServer
.RestApplication
at integration or acceptance level, just to ensure the new RestApplication API is covered.TODO
.redirect(
and consider updating them to use the new route and/or the new RestServer/RestApplication sugar APIs. E.g. REST API Explorer./explorer
to/explorer/
http://example.com
) instead of a local path (/home
). The trick is to skip appendingbasePath
.req.baseUrl
when the LB4 app is mounted on an external Express application.The text was updated successfully, but these errors were encountered: