Cleaned up static file serving
This commit is contained in:
parent
01af2ee742
commit
90fe1629eb
5 changed files with 77 additions and 135 deletions
|
@ -39,7 +39,7 @@ simplelog = "0.6"
|
||||||
toml = "0.5"
|
toml = "0.5"
|
||||||
|
|
||||||
[dependencies.rocket_contrib]
|
[dependencies.rocket_contrib]
|
||||||
version = "0.4.0"
|
version = "0.4.2"
|
||||||
default_features = false
|
default_features = false
|
||||||
features = ["json", "serve"]
|
features = ["json", "serve"]
|
||||||
|
|
||||||
|
|
|
@ -2,58 +2,67 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Polaris Swagger UI</title>
|
<title>Polaris Swagger UI</title>
|
||||||
<link rel="stylesheet" type="text/css" href="swagger-ui.css">
|
<script type="text/javascript">
|
||||||
<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
|
var pathname = document.location.pathname;
|
||||||
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />
|
pathname = pathname.replace(/\/index\.html$/, "");
|
||||||
<style>
|
if (!pathname.endsWith('/')) {
|
||||||
html {
|
pathname += "/";
|
||||||
box-sizing: border-box;
|
}
|
||||||
overflow: -moz-scrollbars-vertical;
|
document.write("<base href='" + pathname + "' />");
|
||||||
overflow-y: scroll;
|
</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" />
|
||||||
|
<style>
|
||||||
|
html {
|
||||||
|
box-sizing: border-box;
|
||||||
|
overflow: -moz-scrollbars-vertical;
|
||||||
|
overflow-y: scroll;
|
||||||
|
}
|
||||||
|
|
||||||
*,
|
*,
|
||||||
*:before,
|
*:before,
|
||||||
*:after {
|
*:after {
|
||||||
box-sizing: inherit;
|
box-sizing: inherit;
|
||||||
}
|
}
|
||||||
|
|
||||||
body {
|
body {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
background: #fafafa;
|
background: #fafafa;
|
||||||
}
|
}
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
</style>
|
||||||
<div id="swagger-ui"></div>
|
</head>
|
||||||
|
|
||||||
<script src="swagger-ui-bundle.js"> </script>
|
<body>
|
||||||
<script src="swagger-ui-standalone-preset.js"> </script>
|
<div id="swagger-ui"></div>
|
||||||
<script>
|
|
||||||
window.onload = function () {
|
|
||||||
// Begin Swagger UI call region
|
|
||||||
const ui = SwaggerUIBundle({
|
|
||||||
url: "polaris-api.json",
|
|
||||||
dom_id: '#swagger-ui',
|
|
||||||
deepLinking: true,
|
|
||||||
presets: [
|
|
||||||
SwaggerUIBundle.presets.apis,
|
|
||||||
SwaggerUIStandalonePreset
|
|
||||||
],
|
|
||||||
plugins: [
|
|
||||||
SwaggerUIBundle.plugins.DownloadUrl
|
|
||||||
],
|
|
||||||
layout: "StandaloneLayout"
|
|
||||||
})
|
|
||||||
// End Swagger UI call region
|
|
||||||
|
|
||||||
window.ui = ui
|
<script src="swagger-ui-bundle.js"> </script>
|
||||||
}
|
<script src="swagger-ui-standalone-preset.js"> </script>
|
||||||
</script>
|
<script>
|
||||||
</body>
|
window.onload = function() {
|
||||||
|
// Begin Swagger UI call region
|
||||||
|
const ui = SwaggerUIBundle({
|
||||||
|
url: "polaris-api.json",
|
||||||
|
dom_id: '#swagger-ui',
|
||||||
|
deepLinking: true,
|
||||||
|
presets: [
|
||||||
|
SwaggerUIBundle.presets.apis,
|
||||||
|
SwaggerUIStandalonePreset
|
||||||
|
],
|
||||||
|
plugins: [
|
||||||
|
SwaggerUIBundle.plugins.DownloadUrl
|
||||||
|
],
|
||||||
|
layout: "StandaloneLayout"
|
||||||
|
})
|
||||||
|
// End Swagger UI call region
|
||||||
|
|
||||||
</html>
|
window.ui = ui
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use rocket;
|
use rocket;
|
||||||
|
use rocket_contrib::serve::StaticFiles;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
@ -6,11 +7,6 @@ use crate::db::DB;
|
||||||
use crate::errors;
|
use crate::errors;
|
||||||
use crate::index::CommandSender;
|
use crate::index::CommandSender;
|
||||||
|
|
||||||
pub struct StaticDirs {
|
|
||||||
pub web_dir_path: PathBuf,
|
|
||||||
pub swagger_dir_path: PathBuf,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_server(
|
pub fn get_server(
|
||||||
port: u16,
|
port: u16,
|
||||||
auth_secret: Option<&[u8]>,
|
auth_secret: Option<&[u8]>,
|
||||||
|
@ -31,16 +27,14 @@ pub fn get_server(
|
||||||
config.set_secret_key(encoded)?;
|
config.set_secret_key(encoded)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let static_dirs = Arc::new(StaticDirs {
|
let swagger_routes_rank = 0;
|
||||||
web_dir_path: web_dir_path.to_path_buf(),
|
let web_routes_rank = swagger_routes_rank + 1;
|
||||||
swagger_dir_path: swagger_dir_path.to_path_buf(),
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(rocket::custom(config)
|
Ok(rocket::custom(config)
|
||||||
.manage(db)
|
.manage(db)
|
||||||
.manage(command_sender)
|
.manage(command_sender)
|
||||||
.manage(static_dirs)
|
.mount(&api_url, crate::api::get_routes())
|
||||||
.mount(&swagger_url, crate::swagger::get_routes())
|
.mount(&swagger_url, StaticFiles::from(swagger_dir_path).rank(swagger_routes_rank))
|
||||||
.mount(&web_url, crate::web::get_routes())
|
.mount(&web_url, StaticFiles::from(web_dir_path).rank(web_routes_rank))
|
||||||
.mount(&api_url, crate::api::get_routes()))
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,55 +1,19 @@
|
||||||
use rocket::http::uri::Origin;
|
|
||||||
use rocket::response::NamedFile;
|
|
||||||
use rocket::response::Redirect;
|
|
||||||
use rocket::{get, routes, State};
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::server::StaticDirs;
|
|
||||||
|
|
||||||
pub fn get_routes() -> Vec<rocket::Route> {
|
|
||||||
routes![index, files,]
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/", rank = 9)]
|
|
||||||
fn index(origin: &Origin<'_>) -> Redirect {
|
|
||||||
let mut new_path = origin.path().to_owned();
|
|
||||||
if !new_path.ends_with("/") {
|
|
||||||
new_path.push_str("/");
|
|
||||||
}
|
|
||||||
new_path.push_str("index.html");
|
|
||||||
let redirect = Redirect::permanent(new_path);
|
|
||||||
return redirect;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/<file..>", rank = 9)]
|
|
||||||
fn files(static_dirs: State<'_, Arc<StaticDirs>>, file: PathBuf) -> Option<NamedFile> {
|
|
||||||
let path = static_dirs.swagger_dir_path.clone().join(file.clone());
|
|
||||||
NamedFile::open(path).ok()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_index_redirect() {
|
|
||||||
use crate::test::get_test_environment;
|
|
||||||
use rocket::http::Status;
|
|
||||||
|
|
||||||
let env = get_test_environment("swagger_index_redirect.sqlite");
|
|
||||||
let client = &env.client;
|
|
||||||
let response = client.get("/swagger").dispatch();
|
|
||||||
assert_eq!(response.status(), Status::PermanentRedirect);
|
|
||||||
assert_eq!(
|
|
||||||
response.headers().get_one("Location"),
|
|
||||||
Some("/swagger/index.html")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_index() {
|
fn test_index() {
|
||||||
use crate::test::get_test_environment;
|
use crate::test::get_test_environment;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
|
|
||||||
let env = get_test_environment("swagger_index.sqlite");
|
let env = get_test_environment("swagger_index.sqlite");
|
||||||
let client = &env.client;
|
let client = &env.client;
|
||||||
let response = client.get("/swagger/index.html").dispatch();
|
let response = client.get("/swagger").dispatch();
|
||||||
|
assert_eq!(response.status(), Status::Ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_index_with_trailing_slash() {
|
||||||
|
use crate::test::get_test_environment;
|
||||||
|
use rocket::http::Status;
|
||||||
|
let env = get_test_environment("swagger_index_with_trailing_slash.sqlite");
|
||||||
|
let client = &env.client;
|
||||||
|
let response = client.get("/swagger/").dispatch();
|
||||||
assert_eq!(response.status(), Status::Ok);
|
assert_eq!(response.status(), Status::Ok);
|
||||||
}
|
}
|
||||||
|
|
25
src/web.rs
25
src/web.rs
|
@ -1,28 +1,3 @@
|
||||||
use rocket::response::NamedFile;
|
|
||||||
use rocket::{get, routes, State};
|
|
||||||
use std::io;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::server::StaticDirs;
|
|
||||||
|
|
||||||
pub fn get_routes() -> Vec<rocket::Route> {
|
|
||||||
routes![index, files,]
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/", rank = 10)]
|
|
||||||
fn index(static_dirs: State<'_, Arc<StaticDirs>>) -> io::Result<NamedFile> {
|
|
||||||
let mut path = static_dirs.web_dir_path.clone();
|
|
||||||
path.push("index.html");
|
|
||||||
NamedFile::open(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/<file..>", rank = 10)]
|
|
||||||
fn files(static_dirs: State<'_, Arc<StaticDirs>>, file: PathBuf) -> Option<NamedFile> {
|
|
||||||
let path = static_dirs.web_dir_path.clone().join(file.clone());
|
|
||||||
NamedFile::open(path).ok()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_index() {
|
fn test_index() {
|
||||||
use crate::test::get_test_environment;
|
use crate::test::get_test_environment;
|
||||||
|
|
Loading…
Add table
Reference in a new issue