Automatically redirect /swagger to /swagger/

This commit is contained in:
Antoine Gersant 2020-05-30 16:34:06 -07:00
parent eb7c833de5
commit d7c66c3745
3 changed files with 9 additions and 13 deletions
docs/swagger
src/service

View file

@ -5,14 +5,6 @@
<head>
<meta charset="UTF-8">
<title>Polaris Swagger UI</title>
<script type="text/javascript">
var pathname = document.location.pathname;
pathname = pathname.replace(/\/index\.html$/, "");
if (!pathname.endsWith('/')) {
pathname += "/";
}
document.write("<base href='" + pathname + "' />");
</script>
<link rel="stylesheet" type="text/css" href="swagger-ui.css">
<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />

View file

@ -1,7 +1,7 @@
use anyhow::*;
use rocket;
use rocket::config::{Environment, LoggingLevel};
use rocket_contrib::serve::StaticFiles;
use rocket_contrib::serve::{Options, StaticFiles};
use std::path::PathBuf;
use super::api;
@ -30,6 +30,7 @@ pub fn get_server(
let swagger_routes_rank = 0;
let web_routes_rank = swagger_routes_rank + 1;
let static_file_options = Options::Index | Options::NormalizeDirs;
Ok(rocket::custom(config)
.manage(db)
@ -37,11 +38,11 @@ pub fn get_server(
.mount(&api_url, api::get_routes())
.mount(
&swagger_url,
StaticFiles::from(swagger_dir_path).rank(swagger_routes_rank),
StaticFiles::new(swagger_dir_path, static_file_options).rank(swagger_routes_rank),
)
.mount(
&web_url,
StaticFiles::from(web_dir_path).rank(web_routes_rank),
StaticFiles::new(web_dir_path, static_file_options).rank(web_routes_rank),
))
}

View file

@ -89,13 +89,16 @@ fn test_service_index() {
#[test]
fn test_service_swagger_index() {
let mut service = ServiceType::new(&format!("{}{}", TEST_DB_PREFIX, line!()));
assert!(service.get("/swagger").status() == StatusCode::OK);
assert_eq!(
service.get("/swagger").status(),
StatusCode::PERMANENT_REDIRECT
);
}
#[test]
fn test_service_swagger_index_with_trailing_slash() {
let mut service = ServiceType::new(&format!("{}{}", TEST_DB_PREFIX, line!()));
assert!(service.get("/swagger/").status() == StatusCode::OK);
assert_eq!(service.get("/swagger/").status(), StatusCode::OK);
}
#[test]