-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable for operating behind a proxy that modifies the url
Assume the backend is hosted on a server with a proxy that maps requests to `/backend/<routes>` to `/<routes>`, then we need a mechanism so that links still work (the link from the error page to the swagger UI, the links in the swagger UI to the api docs). This solution assumes that `X-forwarded-...` headers are set by the proxy, e.g.: * X-Forwarded-For * X-Forwarded-Proto * X-Forwarded-Prefix
- Loading branch information
1 parent
5a6e856
commit 78f0231
Showing
4 changed files
with
71 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
lapis2/src/main/kotlin/org/genspectrum/lapis/controller/ErrorController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.genspectrum.lapis.controller | ||
|
||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import mu.KotlinLogging | ||
import org.springframework.boot.autoconfigure.web.ErrorProperties | ||
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController | ||
import org.springframework.boot.web.servlet.error.ErrorAttributes | ||
import org.springframework.http.MediaType | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.servlet.ModelAndView | ||
import org.springframework.web.servlet.View | ||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder | ||
|
||
private val log = KotlinLogging.logger { } | ||
|
||
@Component | ||
class ErrorController(errorAttributes: ErrorAttributes) : | ||
BasicErrorController(errorAttributes, ErrorProperties()) { | ||
|
||
@RequestMapping(produces = [MediaType.TEXT_HTML_VALUE]) | ||
override fun errorHtml(request: HttpServletRequest, response: HttpServletResponse): ModelAndView { | ||
val modelAndView = super.errorHtml(request, response) | ||
|
||
response.addHeader("Content-Type", MediaType.TEXT_HTML_VALUE) | ||
|
||
val urlPrefix = removeErrorSegmentFromUrl(ServletUriComponentsBuilder.fromCurrentRequest().toUriString()) | ||
val url = "$urlPrefix/swagger-ui/index.html" | ||
|
||
log.debug { "Generated url $url to Swagger UI in 'not found page'" } | ||
|
||
modelAndView.view = NotFoundView(url) | ||
return modelAndView | ||
} | ||
|
||
fun removeErrorSegmentFromUrl(url: String): String { | ||
val lastSlashIndex = url.trimEnd('/').lastIndexOf("error") | ||
return url.substring(0, lastSlashIndex).trim('/') | ||
} | ||
} | ||
|
||
data class NotFoundView(private val url: String?) : View { | ||
|
||
override fun render(model: MutableMap<String, *>?, request: HttpServletRequest, response: HttpServletResponse) { | ||
val html: String = """ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Error 404</title> | ||
</head> | ||
<body> | ||
<h1>LAPIS</h1> | ||
<h3>Page not found!</h3> | ||
<a href="$url">Visit our swagger-ui</a> | ||
</body> | ||
</html> | ||
""".trimIndent() | ||
|
||
response.outputStream.write(html.toByteArray()) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters