Changeset View
Changeset View
Standalone View
Standalone View
src/kernel.rs
//! # Service execution utilities. | //! # Service execution utilities. | ||||
//! | //! | ||||
//! Provides methods to start the server and handle the application | //! Provides methods to start the server and handle the application | ||||
use config::Config; | use config::Config; | ||||
use config::DefaultConfig; | use config::DefaultConfig; | ||||
use config::MinimalConfig; | |||||
use database::initialize_database_pool; | use database::initialize_database_pool; | ||||
use database::test_database_connection; | use database::test_database_connection; | ||||
use ErrorResult; | use ErrorResult; | ||||
use rocket::Route; | use rocket::Route; | ||||
use rocket::ignite; | use rocket::ignite; | ||||
use std::process; | use std::process; | ||||
/* ------------------------------------------------------------- | /* ------------------------------------------------------------- | ||||
Application | Application | ||||
Allow to define config and routes. Launch a server. | Allow to define config and routes. Launch a server. | ||||
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | ||||
pub trait Application { | pub trait Application { | ||||
fn get_config(&self) -> &dyn Config; | fn get_config(&self) -> &dyn Config; | ||||
fn get_routes(&self) -> &[Route]; | fn get_routes(&self) -> &[Route]; | ||||
fn launch_server(&mut self) -> ErrorResult<()> { | fn launch_server(&mut self) -> ErrorResult<()> { | ||||
let config = self.get_config(); | let config = self.get_config(); | ||||
let routes = self.get_routes(); | let routes = self.get_routes(); | ||||
ignite() | let mut server = ignite(); | ||||
.manage( | |||||
if config.with_database() { | |||||
server = server.manage( | |||||
initialize_database_pool(config.get_database_url(), config.get_database_pool_size())? | initialize_database_pool(config.get_database_url(), config.get_database_pool_size())? | ||||
) | ); | ||||
} | |||||
server | |||||
.mount(config.get_entry_point(), routes.to_vec()) | .mount(config.get_entry_point(), routes.to_vec()) | ||||
.launch(); | .launch(); | ||||
Ok(()) | Ok(()) | ||||
} | } | ||||
fn run (&mut self) -> ErrorResult<()> { | fn run (&mut self) -> ErrorResult<()> { | ||||
info!(target: "runner", "Server started."); | info!(target: "runner", "Server started."); | ||||
// Initial connection to test if the database configuration works | // Initial connection to test if the database configuration works | ||||
{ | { | ||||
let config = self.get_config(); | let config = self.get_config(); | ||||
if config.with_database() { | |||||
test_database_connection(config.get_database_url())?; | test_database_connection(config.get_database_url())?; | ||||
info!(target: "runner", "Connection to database established."); | info!(target: "runner", "Connection to database established."); | ||||
} | } | ||||
} | |||||
self.launch_server()?; | self.launch_server()?; | ||||
Ok(()) | Ok(()) | ||||
} | } | ||||
} | } | ||||
/* ------------------------------------------------------------- | /* ------------------------------------------------------------- | ||||
Show All 23 Lines | |||||
/// users::get_player, | /// users::get_player, | ||||
/// ]; | /// ]; | ||||
/// | /// | ||||
/// DefaultApplication::start_application(routes); | /// DefaultApplication::start_application(routes); | ||||
/// } | /// } | ||||
/// ``` | /// ``` | ||||
/// | /// | ||||
/// The default configuration will be used and the server started. | /// The default configuration will be used and the server started. | ||||
pub struct DefaultApplication { | pub struct DefaultApplication<T> | ||||
config: DefaultConfig, | where T: Config | ||||
{ | |||||
config: T, | |||||
routes: Box<Vec<Route>>, | routes: Box<Vec<Route>>, | ||||
} | } | ||||
impl Application for DefaultApplication { | impl<T> Application for DefaultApplication<T> | ||||
where T: Config | |||||
{ | |||||
fn get_config(&self) -> &dyn Config { | fn get_config(&self) -> &dyn Config { | ||||
&self.config | &self.config | ||||
} | } | ||||
fn get_routes(&self) -> &[Route] { | fn get_routes(&self) -> &[Route] { | ||||
self.routes.as_slice() | self.routes.as_slice() | ||||
} | } | ||||
} | } | ||||
impl DefaultApplication { | impl<T> DefaultApplication<T> | ||||
pub fn new (config: DefaultConfig, routes: Vec<Route>) -> Self { | where T: Config | ||||
{ | |||||
pub fn new (config: T, routes: Vec<Route>) -> Self { | |||||
DefaultApplication { | DefaultApplication { | ||||
config, | config, | ||||
routes: Box::new(routes), | routes: Box::new(routes), | ||||
} | } | ||||
} | } | ||||
/// Starts the application, prepares default configuration | /// Starts the application | ||||
/// | /// | ||||
/// # Exit codes | /// # Exit codes | ||||
/// | /// | ||||
/// The software will exit with the following error codes: | /// The software will exit with the following error codes: | ||||
/// | /// | ||||
/// - 0: Graceful exit (currently not in use, as the application never stops) | /// - 0: Graceful exit (currently not in use, as the application never stops) | ||||
/// - 1: Error during the application run (e.g. routes conflict or Rocket fairings issues) | /// - 1: Error during the application run (e.g. routes conflict or Rocket fairings issues) | ||||
/// - 2: Error parsing the configuration (e.g. no database URL has been defined) | /// - 2: Error parsing the configuration (e.g. no database URL has been defined) | ||||
pub fn start_application (routes: Vec<Route>) { | pub fn start (&mut self) { | ||||
info!(target: "runner", "Server initialized."); | info!(target: "runner", "Server initialized."); | ||||
if let Err(error) = self.run() { | |||||
error!(target: "runner", "{}", error.description()); | |||||
process::exit(1); | |||||
} | |||||
process::exit(0); | |||||
} | |||||
/// Prepares an application with the default application and starts it | |||||
pub fn start_application (routes: Vec<Route>) { | |||||
let config = DefaultConfig::parse_environment().unwrap_or_else(|_error| { | let config = DefaultConfig::parse_environment().unwrap_or_else(|_error| { | ||||
process::exit(2); | process::exit(2); | ||||
}); | }); | ||||
let mut app = Self::new(config, routes); | let mut app = DefaultApplication::new(config, routes); | ||||
app.start(); | |||||
} | |||||
} | |||||
/* ------------------------------------------------------------- | |||||
Minimal application | |||||
if let Err(error) = app.run() { | :: Application | ||||
error!(target: "runner", "{}", error.description()); | :: sui generis implementation, wrapper for DefaultApplication | ||||
process::exit(1); | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ | ||||
pub struct MinimalApplication {} | |||||
impl MinimalApplication { | |||||
pub fn new (config: MinimalConfig, routes: Vec<Route>) -> DefaultApplication<MinimalConfig> { | |||||
DefaultApplication { | |||||
config, | |||||
routes: Box::new(routes), | |||||
} | |||||
} | } | ||||
process::exit(0); | pub fn start_application (routes: Vec<Route>) { | ||||
let config = MinimalConfig::parse_environment().unwrap_or_else(|_error| { | |||||
process::exit(2); | |||||
}); | |||||
let mut app = DefaultApplication::new(config, routes); | |||||
app.start(); | |||||
} | } | ||||
} | } | ||||
Nasqueron DevCentral · If it had been much bigger the moon would have had a core of ice. · Powered by Phabricator