2020-10-15 00:16:06 +00:00
|
|
|
use crate::config;
|
|
|
|
use crate::database;
|
2020-10-24 14:53:53 +00:00
|
|
|
use crate::i18n::{self, I18n};
|
2020-10-15 00:16:06 +00:00
|
|
|
use crate::routes;
|
|
|
|
|
2020-10-15 19:06:09 +00:00
|
|
|
use rocket_contrib::serve::{Options as ServeOptions, StaticFiles};
|
2020-10-15 00:16:06 +00:00
|
|
|
use rocket_contrib::templates::Template;
|
|
|
|
|
2020-10-16 05:33:52 +00:00
|
|
|
pub fn rocket(config: &config::Config) -> Result<rocket::Rocket, ()> {
|
2020-10-15 18:59:58 +00:00
|
|
|
let rocket_config = config.to_rocket_config()?;
|
|
|
|
|
2020-10-21 04:05:54 +00:00
|
|
|
let public_path = config.public_path()?;
|
|
|
|
let locales_path = config.locales_path()?;
|
|
|
|
|
2020-10-21 10:09:03 +00:00
|
|
|
let i18n = match I18n::new(&locales_path, &["en", "ru"]) {
|
|
|
|
Err(_) => return Err(()),
|
|
|
|
Ok(i18n) => i18n,
|
|
|
|
};
|
2020-10-15 18:59:58 +00:00
|
|
|
|
2020-10-24 14:53:53 +00:00
|
|
|
let i18n_arc = match I18n::new(&locales_path, &["en", "ru"]) {
|
|
|
|
Err(_) => return Err(()),
|
|
|
|
Ok(i18n) => std::sync::Arc::new(i18n),
|
|
|
|
};
|
|
|
|
|
2020-10-15 18:59:58 +00:00
|
|
|
let result = rocket::custom(rocket_config)
|
2020-10-21 04:05:54 +00:00
|
|
|
.manage(i18n)
|
2020-10-15 00:16:06 +00:00
|
|
|
.manage(database::create_db_pool(config))
|
2020-10-16 21:33:24 +00:00
|
|
|
.attach(rocket_csrf::Fairing::new())
|
2020-10-24 14:53:53 +00:00
|
|
|
.attach(Template::custom(move |engines| {
|
2020-10-21 10:21:30 +00:00
|
|
|
engines.handlebars.set_strict_mode(true);
|
2020-10-24 14:53:53 +00:00
|
|
|
|
|
|
|
let i18n_arc = i18n_arc.clone();
|
|
|
|
|
|
|
|
engines.handlebars.register_helper(
|
|
|
|
"translate",
|
|
|
|
i18n::handlebars_helpers::translate(i18n_arc),
|
|
|
|
);
|
2020-10-21 10:21:30 +00:00
|
|
|
}))
|
2020-10-15 00:16:06 +00:00
|
|
|
.mount("/", routes::routes())
|
2020-10-15 20:41:22 +00:00
|
|
|
.mount("/", StaticFiles::new(public_path, ServeOptions::None));
|
2020-10-15 18:59:58 +00:00
|
|
|
|
|
|
|
Ok(result)
|
2020-10-15 00:16:06 +00:00
|
|
|
}
|