Serve swagger files under /swagger
This commit is contained in:
parent
b190385dbd
commit
c8655a2447
6 changed files with 157 additions and 64 deletions
|
@ -2,77 +2,21 @@ use rocket::http::hyper::header::*;
|
||||||
use rocket::http::uri::Uri;
|
use rocket::http::uri::Uri;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket::local::Client;
|
use rocket::local::Client;
|
||||||
use std::fs;
|
|
||||||
use std::ops::Deref;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::{thread, time};
|
use std::{thread, time};
|
||||||
|
|
||||||
use crate::api;
|
use crate::api;
|
||||||
use crate::config;
|
use crate::config;
|
||||||
use crate::db;
|
|
||||||
use crate::ddns;
|
use crate::ddns;
|
||||||
use crate::index;
|
use crate::index;
|
||||||
use crate::server;
|
|
||||||
use crate::vfs;
|
use crate::vfs;
|
||||||
|
|
||||||
|
use crate::test::get_test_environment;
|
||||||
|
|
||||||
const TEST_USERNAME: &str = "test_user";
|
const TEST_USERNAME: &str = "test_user";
|
||||||
const TEST_PASSWORD: &str = "test_password";
|
const TEST_PASSWORD: &str = "test_password";
|
||||||
const TEST_MOUNT_NAME: &str = "collection";
|
const TEST_MOUNT_NAME: &str = "collection";
|
||||||
const TEST_MOUNT_SOURCE: &str = "test/collection";
|
const TEST_MOUNT_SOURCE: &str = "test/collection";
|
||||||
|
|
||||||
struct TestEnvironment {
|
|
||||||
pub client: Client,
|
|
||||||
command_sender: Arc<index::CommandSender>,
|
|
||||||
db: Arc<db::DB>,
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
fn complete_initial_setup(client: &Client) {
|
||||||
let configuration = config::Config {
|
let configuration = config::Config {
|
||||||
album_art_pattern: None,
|
album_art_pattern: None,
|
||||||
|
|
|
@ -71,11 +71,14 @@ mod metadata;
|
||||||
mod playlist;
|
mod playlist;
|
||||||
mod serve;
|
mod serve;
|
||||||
mod server;
|
mod server;
|
||||||
|
mod swagger;
|
||||||
|
mod test;
|
||||||
mod thumbnails;
|
mod thumbnails;
|
||||||
mod ui;
|
mod ui;
|
||||||
mod user;
|
mod user;
|
||||||
mod utils;
|
mod utils;
|
||||||
mod vfs;
|
mod vfs;
|
||||||
|
mod web;
|
||||||
|
|
||||||
static LOG_CONFIG: simplelog::Config = simplelog::Config {
|
static LOG_CONFIG: simplelog::Config = simplelog::Config {
|
||||||
time: Some(Level::Error),
|
time: Some(Level::Error),
|
||||||
|
@ -221,7 +224,7 @@ fn run() -> Result<()> {
|
||||||
|
|
||||||
// API mount target
|
// API mount target
|
||||||
let prefix_url = config.prefix_url.unwrap_or_else(|| "".to_string());
|
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);
|
info!("Mounting API on {}", api_url);
|
||||||
|
|
||||||
// Web client mount target
|
// Web client mount target
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
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;
|
||||||
|
|
||||||
use crate::api;
|
|
||||||
use crate::db::DB;
|
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,
|
||||||
api_url: &str,
|
api_url: &str,
|
||||||
|
@ -18,14 +21,21 @@ pub fn get_server(
|
||||||
db: Arc<DB>,
|
db: Arc<DB>,
|
||||||
command_sender: Arc<CommandSender>,
|
command_sender: Arc<CommandSender>,
|
||||||
) -> Result<rocket::Rocket, errors::Error> {
|
) -> Result<rocket::Rocket, errors::Error> {
|
||||||
|
|
||||||
let config = rocket::Config::build(rocket::config::Environment::Production)
|
let config = rocket::Config::build(rocket::config::Environment::Production)
|
||||||
.port(port)
|
.port(port)
|
||||||
.finalize()?;
|
.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)
|
Ok(rocket::custom(config)
|
||||||
.manage(db)
|
.manage(db)
|
||||||
.manage(command_sender)
|
.manage(command_sender)
|
||||||
.mount(&swagger_url, StaticFiles::from(swagger_dir_path))
|
.manage(static_dirs)
|
||||||
.mount(&web_url, StaticFiles::from(web_dir_path))
|
.mount(&swagger_url, crate::swagger::get_routes())
|
||||||
.mount(&api_url, api::get_routes()))
|
.mount(&web_url, crate::web::get_routes())
|
||||||
|
.mount(&api_url, crate::api::get_routes()))
|
||||||
}
|
}
|
||||||
|
|
37
src/swagger.rs
Normal file
37
src/swagger.rs
Normal file
|
@ -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<rocket::Route> {
|
||||||
|
routes![
|
||||||
|
index,
|
||||||
|
files,
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/", rank = 9)]
|
||||||
|
fn index(static_dirs: State<Arc<StaticDirs>>) -> io::Result<NamedFile> {
|
||||||
|
let mut path = static_dirs.swagger_dir_path.clone();
|
||||||
|
path.push("index.html");
|
||||||
|
NamedFile::open(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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() {
|
||||||
|
let env = get_test_environment("swagger_index.sqlite");
|
||||||
|
let client = &env.client;
|
||||||
|
let response = client.get("/swagger").dispatch();
|
||||||
|
assert_eq!(response.status(), Status::Ok);
|
||||||
|
}
|
62
src/test.rs
Normal file
62
src/test.rs
Normal file
|
@ -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<index::CommandSender>,
|
||||||
|
db: Arc<DB>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
}
|
37
src/web.rs
Normal file
37
src/web.rs
Normal file
|
@ -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<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() {
|
||||||
|
let env = get_test_environment("web_index.sqlite");
|
||||||
|
let client = &env.client;
|
||||||
|
let response = client.get("/").dispatch();
|
||||||
|
assert_eq!(response.status(), Status::Ok);
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue