From c8655a2447ddedcdcfa2d0ca6d18f1348ef2f0f2 Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Sun, 7 Apr 2019 23:24:15 -0700 Subject: [PATCH] Serve swagger files under /swagger --- src/api_tests.rs | 60 ++-------------------------------------------- src/main.rs | 5 +++- src/server.rs | 20 ++++++++++++---- src/swagger.rs | 37 +++++++++++++++++++++++++++++ src/test.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ src/web.rs | 37 +++++++++++++++++++++++++++++ 6 files changed, 157 insertions(+), 64 deletions(-) create mode 100644 src/swagger.rs create mode 100644 src/test.rs create mode 100644 src/web.rs diff --git a/src/api_tests.rs b/src/api_tests.rs index 0a59487..5f3077c 100644 --- a/src/api_tests.rs +++ b/src/api_tests.rs @@ -2,77 +2,21 @@ use rocket::http::hyper::header::*; use rocket::http::uri::Uri; use rocket::http::Status; use rocket::local::Client; -use std::fs; -use std::ops::Deref; -use std::path::PathBuf; -use std::sync::Arc; use std::{thread, time}; use crate::api; use crate::config; -use crate::db; use crate::ddns; use crate::index; -use crate::server; use crate::vfs; +use crate::test::get_test_environment; + const TEST_USERNAME: &str = "test_user"; const TEST_PASSWORD: &str = "test_password"; const TEST_MOUNT_NAME: &str = "collection"; const TEST_MOUNT_SOURCE: &str = "test/collection"; -struct TestEnvironment { - pub client: Client, - command_sender: Arc, - db: Arc, -} - -impl TestEnvironment { - pub fn update_index(&self) { - index::update(self.db.deref()).unwrap(); - } -} - -impl Drop for TestEnvironment { - fn drop(&mut self) { - self.command_sender.deref().exit().unwrap(); - } -} - -fn get_test_environment(db_name: &str) -> TestEnvironment { - let mut db_path = PathBuf::new(); - db_path.push("test"); - db_path.push(db_name); - if db_path.exists() { - fs::remove_file(&db_path).unwrap(); - } - - let db = Arc::new(db::DB::new(&db_path).unwrap()); - - let web_dir_path = PathBuf::from("web"); - let mut swagger_dir_path = PathBuf::from("docs"); - swagger_dir_path.push("swagger"); - let command_sender = index::init(db.clone()); - - let server = server::get_server( - 5050, - "/api", - "/", - &web_dir_path, - "/swagger", - &swagger_dir_path, - db.clone(), - command_sender.clone(), - ) - .unwrap(); - let client = Client::new(server).unwrap(); - TestEnvironment { - client, - command_sender, - db, - } -} - fn complete_initial_setup(client: &Client) { let configuration = config::Config { album_art_pattern: None, diff --git a/src/main.rs b/src/main.rs index f54c46f..098772f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,11 +71,14 @@ mod metadata; mod playlist; mod serve; mod server; +mod swagger; +mod test; mod thumbnails; mod ui; mod user; mod utils; mod vfs; +mod web; static LOG_CONFIG: simplelog::Config = simplelog::Config { time: Some(Level::Error), @@ -221,7 +224,7 @@ fn run() -> Result<()> { // API mount target let prefix_url = config.prefix_url.unwrap_or_else(|| "".to_string()); - let api_url = format!("{}/api", &prefix_url); + let api_url = format!("/{}api", &prefix_url); info!("Mounting API on {}", api_url); // Web client mount target diff --git a/src/server.rs b/src/server.rs index a9812b8..cdaf255 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,13 +1,16 @@ use rocket; -use rocket_contrib::serve::StaticFiles; use std::path::PathBuf; use std::sync::Arc; -use crate::api; 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, api_url: &str, @@ -18,14 +21,21 @@ pub fn get_server( db: Arc, command_sender: Arc, ) -> Result { + let config = rocket::Config::build(rocket::config::Environment::Production) .port(port) .finalize()?; + let static_dirs = Arc::new(StaticDirs { + web_dir_path: web_dir_path.to_path_buf(), + swagger_dir_path: swagger_dir_path.to_path_buf(), + }); + Ok(rocket::custom(config) .manage(db) .manage(command_sender) - .mount(&swagger_url, StaticFiles::from(swagger_dir_path)) - .mount(&web_url, StaticFiles::from(web_dir_path)) - .mount(&api_url, api::get_routes())) + .manage(static_dirs) + .mount(&swagger_url, crate::swagger::get_routes()) + .mount(&web_url, crate::web::get_routes()) + .mount(&api_url, crate::api::get_routes())) } diff --git a/src/swagger.rs b/src/swagger.rs new file mode 100644 index 0000000..1d959c1 --- /dev/null +++ b/src/swagger.rs @@ -0,0 +1,37 @@ +use rocket::http::Status; +use rocket::response::NamedFile; +use rocket::State; +use std::io; +use std::path::PathBuf; +use std::sync::Arc; + +use crate::server::StaticDirs; +use crate::test::get_test_environment; + +pub fn get_routes() -> Vec { + routes![ + index, + files, + ] +} + +#[get("/", rank = 9)] +fn index(static_dirs: State>) -> io::Result { + let mut path = static_dirs.swagger_dir_path.clone(); + path.push("index.html"); + NamedFile::open(path) +} + +#[get("/", rank = 9)] +fn files(static_dirs: State>, file: PathBuf) -> Option { + let path = static_dirs.swagger_dir_path.clone().join(file.clone()); + NamedFile::open(path).ok() +} + +#[test] +fn test_index() { + let env = get_test_environment("swagger_index.sqlite"); + let client = &env.client; + let response = client.get("/swagger").dispatch(); + assert_eq!(response.status(), Status::Ok); +} diff --git a/src/test.rs b/src/test.rs new file mode 100644 index 0000000..668de13 --- /dev/null +++ b/src/test.rs @@ -0,0 +1,62 @@ +use rocket; +use rocket::local::Client; +use std::fs; +use std::ops::Deref; +use std::path::PathBuf; +use std::sync::Arc; + +use crate::db::DB; +use crate::index; +use crate::server; + +pub struct TestEnvironment { + pub client: Client, + command_sender: Arc, + db: Arc, +} + +impl TestEnvironment { + pub fn update_index(&self) { + index::update(self.db.deref()).unwrap(); + } +} + +impl Drop for TestEnvironment { + fn drop(&mut self) { + self.command_sender.deref().exit().unwrap(); + } +} + +pub fn get_test_environment(db_name: &str) -> TestEnvironment { + let mut db_path = PathBuf::new(); + db_path.push("test"); + db_path.push(db_name); + if db_path.exists() { + fs::remove_file(&db_path).unwrap(); + } + + let db = Arc::new(DB::new(&db_path).unwrap()); + + let web_dir_path = PathBuf::from("web"); + let mut swagger_dir_path = PathBuf::from("docs"); + swagger_dir_path.push("swagger"); + let command_sender = index::init(db.clone()); + + let server = server::get_server( + 5050, + "/api", + "/", + &web_dir_path, + "/swagger", + &swagger_dir_path, + db.clone(), + command_sender.clone(), + ) + .unwrap(); + let client = Client::new(server).unwrap(); + TestEnvironment { + client, + command_sender, + db, + } +} diff --git a/src/web.rs b/src/web.rs new file mode 100644 index 0000000..695b616 --- /dev/null +++ b/src/web.rs @@ -0,0 +1,37 @@ +use rocket::http::Status; +use rocket::response::NamedFile; +use rocket::State; +use std::io; +use std::path::PathBuf; +use std::sync::Arc; + +use crate::server::StaticDirs; +use crate::test::get_test_environment; + +pub fn get_routes() -> Vec { + routes![ + index, + files, + ] +} + +#[get("/", rank = 10)] +fn index(static_dirs: State>) -> io::Result { + let mut path = static_dirs.web_dir_path.clone(); + path.push("index.html"); + NamedFile::open(path) +} + +#[get("/", rank = 10)] +fn files(static_dirs: State>, file: PathBuf) -> Option { + let path = static_dirs.web_dir_path.clone().join(file.clone()); + NamedFile::open(path).ok() +} + +#[test] +fn test_index() { + let env = get_test_environment("web_index.sqlite"); + let client = &env.client; + let response = client.get("/").dispatch(); + assert_eq!(response.status(), Status::Ok); +}