Moved server initialization outside of main for easier testing
This commit is contained in:
parent
ed81d24b7b
commit
a3968e9cb7
2 changed files with 40 additions and 12 deletions
24
src/main.rs
24
src/main.rs
|
@ -54,12 +54,13 @@ use unix_daemonize::{daemonize_redirect, ChdirMode};
|
||||||
use core::ops::Deref;
|
use core::ops::Deref;
|
||||||
use crate::errors::*;
|
use crate::errors::*;
|
||||||
use getopts::Options;
|
use getopts::Options;
|
||||||
use rocket_contrib::serve::StaticFiles;
|
|
||||||
use simplelog::{Level, LevelFilter, SimpleLogger, TermLogger};
|
use simplelog::{Level, LevelFilter, SimpleLogger, TermLogger};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
|
#[cfg(test)]
|
||||||
|
mod api_tests;
|
||||||
mod config;
|
mod config;
|
||||||
mod db;
|
mod db;
|
||||||
mod ddns;
|
mod ddns;
|
||||||
|
@ -69,6 +70,7 @@ mod lastfm;
|
||||||
mod metadata;
|
mod metadata;
|
||||||
mod playlist;
|
mod playlist;
|
||||||
mod serve;
|
mod serve;
|
||||||
|
mod server;
|
||||||
mod thumbnails;
|
mod thumbnails;
|
||||||
mod ui;
|
mod ui;
|
||||||
mod user;
|
mod user;
|
||||||
|
@ -233,18 +235,16 @@ fn run() -> Result<()> {
|
||||||
.parse()
|
.parse()
|
||||||
.or(Err("invalid port number"))?;
|
.or(Err("invalid port number"))?;
|
||||||
|
|
||||||
let config = rocket::Config::build(rocket::config::Environment::Production)
|
let server = server::get_server(
|
||||||
.port(port)
|
port,
|
||||||
.finalize()?;
|
&static_url,
|
||||||
|
&api_url,
|
||||||
let db_server = db.clone();
|
&web_dir_path,
|
||||||
|
db.clone(),
|
||||||
|
command_sender,
|
||||||
|
)?;
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
rocket::custom(config)
|
server.launch();
|
||||||
.manage(db_server)
|
|
||||||
.manage(command_sender)
|
|
||||||
.mount(&static_url, StaticFiles::from(web_dir_path))
|
|
||||||
.mount(&api_url, api::get_routes())
|
|
||||||
.launch();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start DDNS updates
|
// Start DDNS updates
|
||||||
|
|
28
src/server.rs
Normal file
28
src/server.rs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
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 fn get_server(
|
||||||
|
port: u16,
|
||||||
|
static_url: &str,
|
||||||
|
api_url: &str,
|
||||||
|
web_dir_path: &PathBuf,
|
||||||
|
db: Arc<DB>,
|
||||||
|
command_sender: Arc<CommandSender>,
|
||||||
|
) -> Result<rocket::Rocket, errors::Error> {
|
||||||
|
let config = rocket::Config::build(rocket::config::Environment::Production)
|
||||||
|
.port(port)
|
||||||
|
.finalize()?;
|
||||||
|
|
||||||
|
Ok(rocket::custom(config)
|
||||||
|
.manage(db)
|
||||||
|
.manage(command_sender)
|
||||||
|
.mount(&static_url, StaticFiles::from(web_dir_path))
|
||||||
|
.mount(&api_url, api::get_routes()))
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue