1
0
Fork 0
This repository has been archived on 2023-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
fedihub-website/src/web.rs

44 lines
1.3 KiB
Rust
Raw Normal View History

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
}