mirror of
https://github.com/agersant/polaris
synced 2024-11-10 10:14:12 +00:00
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"
|
||||
|
||||
[dependencies.rocket_contrib]
|
||||
version = "0.4.0"
|
||||
version = "0.4.2"
|
||||
default_features = false
|
||||
features = ["json", "serve"]
|
||||
|
||||
|
|
|
@ -2,58 +2,67 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Polaris Swagger UI</title>
|
||||
<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;
|
||||
}
|
||||
<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" />
|
||||
<style>
|
||||
html {
|
||||
box-sizing: border-box;
|
||||
overflow: -moz-scrollbars-vertical;
|
||||
overflow-y: scroll;
|
||||
}
|
||||
|
||||
*,
|
||||
*:before,
|
||||
*:after {
|
||||
box-sizing: inherit;
|
||||
}
|
||||
*,
|
||||
*:before,
|
||||
*:after {
|
||||
box-sizing: inherit;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
background: #fafafa;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
body {
|
||||
margin: 0;
|
||||
background: #fafafa;
|
||||
}
|
||||
|
||||
<body>
|
||||
<div id="swagger-ui"></div>
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<script src="swagger-ui-bundle.js"> </script>
|
||||
<script src="swagger-ui-standalone-preset.js"> </script>
|
||||
<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
|
||||
<body>
|
||||
<div id="swagger-ui"></div>
|
||||
|
||||
window.ui = ui
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
<script src="swagger-ui-bundle.js"> </script>
|
||||
<script src="swagger-ui-standalone-preset.js"> </script>
|
||||
<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
|
||||
|
||||
</html>
|
||||
window.ui = ui
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use rocket;
|
||||
use rocket_contrib::serve::StaticFiles;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
|
@ -6,11 +7,6 @@ use crate::db::DB;
|
|||
use crate::errors;
|
||||
use crate::index::CommandSender;
|
||||
|
||||
pub struct StaticDirs {
|
||||
pub web_dir_path: PathBuf,
|
||||
pub swagger_dir_path: PathBuf,
|
||||
}
|
||||
|
||||
pub fn get_server(
|
||||
port: u16,
|
||||
auth_secret: Option<&[u8]>,
|
||||
|
@ -31,16 +27,14 @@ pub fn get_server(
|
|||
config.set_secret_key(encoded)?;
|
||||
}
|
||||
|
||||
let static_dirs = Arc::new(StaticDirs {
|
||||
web_dir_path: web_dir_path.to_path_buf(),
|
||||
swagger_dir_path: swagger_dir_path.to_path_buf(),
|
||||
});
|
||||
let swagger_routes_rank = 0;
|
||||
let web_routes_rank = swagger_routes_rank + 1;
|
||||
|
||||
Ok(rocket::custom(config)
|
||||
.manage(db)
|
||||
.manage(command_sender)
|
||||
.manage(static_dirs)
|
||||
.mount(&swagger_url, crate::swagger::get_routes())
|
||||
.mount(&web_url, crate::web::get_routes())
|
||||
.mount(&api_url, crate::api::get_routes()))
|
||||
.mount(&api_url, crate::api::get_routes())
|
||||
.mount(&swagger_url, StaticFiles::from(swagger_dir_path).rank(swagger_routes_rank))
|
||||
.mount(&web_url, StaticFiles::from(web_dir_path).rank(web_routes_rank))
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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]
|
||||
fn test_index() {
|
||||
use crate::test::get_test_environment;
|
||||
use rocket::http::Status;
|
||||
|
||||
let env = get_test_environment("swagger_index.sqlite");
|
||||
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);
|
||||
}
|
||||
|
|
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]
|
||||
fn test_index() {
|
||||
use crate::test::get_test_environment;
|
||||
|
|
Loading…
Reference in a new issue