Moved server initialization outside of main for easier testing

This commit is contained in:
Antoine Gersant 2018-11-11 12:11:18 -08:00
parent ed81d24b7b
commit a3968e9cb7
2 changed files with 40 additions and 12 deletions

View file

@ -54,12 +54,13 @@ use unix_daemonize::{daemonize_redirect, ChdirMode};
use core::ops::Deref;
use crate::errors::*;
use getopts::Options;
use rocket_contrib::serve::StaticFiles;
use simplelog::{Level, LevelFilter, SimpleLogger, TermLogger};
use std::path::Path;
use std::sync::Arc;
mod api;
#[cfg(test)]
mod api_tests;
mod config;
mod db;
mod ddns;
@ -69,6 +70,7 @@ mod lastfm;
mod metadata;
mod playlist;
mod serve;
mod server;
mod thumbnails;
mod ui;
mod user;
@ -233,18 +235,16 @@ fn run() -> Result<()> {
.parse()
.or(Err("invalid port number"))?;
let config = rocket::Config::build(rocket::config::Environment::Production)
.port(port)
.finalize()?;
let db_server = db.clone();
let server = server::get_server(
port,
&static_url,
&api_url,
&web_dir_path,
db.clone(),
command_sender,
)?;
std::thread::spawn(move || {
rocket::custom(config)
.manage(db_server)
.manage(command_sender)
.mount(&static_url, StaticFiles::from(web_dir_path))
.mount(&api_url, api::get_routes())
.launch();
server.launch();
});
// Start DDNS updates

28
src/server.rs Normal file
View 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()))
}