2021-08-19 22:46:50 +02:00
|
|
|
// This needs to be explicitely included before diesel is imported to make sure
|
2021-08-21 20:22:19 +02:00
|
|
|
// compilation succeeds in the release Docker image.
|
2021-08-19 22:46:50 +02:00
|
|
|
extern crate openssl;
|
2021-08-20 23:09:22 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rocket;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_migrations;
|
2021-08-29 20:30:33 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
2021-08-19 22:46:50 +02:00
|
|
|
|
2021-08-30 15:28:01 +02:00
|
|
|
use figment::{
|
|
|
|
providers::{Env, Format, Yaml},
|
|
|
|
Figment,
|
|
|
|
};
|
2021-09-01 17:29:39 +02:00
|
|
|
use rocket::{
|
|
|
|
fairing::AdHoc,
|
|
|
|
http::Status,
|
|
|
|
serde::json::{json, Value},
|
2021-09-05 11:19:18 +02:00
|
|
|
Build, Request, Rocket, Orbit,
|
2021-09-01 17:29:39 +02:00
|
|
|
};
|
2021-08-29 20:30:33 +02:00
|
|
|
use rocket_sync_db_pools::database;
|
2021-08-30 15:28:01 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-08-29 20:30:33 +02:00
|
|
|
|
2021-08-29 20:41:03 +02:00
|
|
|
mod admin;
|
2021-08-29 20:30:33 +02:00
|
|
|
pub mod auth;
|
|
|
|
pub mod db;
|
|
|
|
pub mod errors;
|
|
|
|
pub mod guards;
|
|
|
|
pub(crate) mod schema;
|
2021-08-20 16:52:58 +02:00
|
|
|
|
2021-09-03 16:31:06 +02:00
|
|
|
#[global_allocator]
|
|
|
|
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
|
|
|
|
2021-08-19 22:46:50 +02:00
|
|
|
#[database("postgres_rb")]
|
|
|
|
pub struct RbDbConn(diesel::PgConnection);
|
|
|
|
|
2021-09-01 17:29:39 +02:00
|
|
|
#[catch(default)]
|
|
|
|
fn default_catcher(status: Status, _: &Request) -> Value
|
|
|
|
{
|
|
|
|
json!({"status": status.code, "message": ""})
|
|
|
|
}
|
|
|
|
|
2021-08-29 20:30:33 +02:00
|
|
|
embed_migrations!();
|
|
|
|
|
2021-08-22 16:45:01 +02:00
|
|
|
async fn run_db_migrations(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocket<Build>>
|
|
|
|
{
|
2021-08-19 22:46:50 +02:00
|
|
|
let conn = RbDbConn::get_one(&rocket)
|
|
|
|
.await
|
|
|
|
.expect("database connection");
|
|
|
|
conn.run(|c| match embedded_migrations::run(c) {
|
|
|
|
Ok(()) => Ok(rocket),
|
|
|
|
Err(_) => Err(rocket),
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2021-09-05 11:19:18 +02:00
|
|
|
async fn create_admin_user<'a>(rocket: &'a Rocket<Orbit>)
|
2021-08-22 16:45:01 +02:00
|
|
|
{
|
2021-09-05 11:19:18 +02:00
|
|
|
let config = rocket.state::<RbConfig>().expect("RbConfig instance");
|
|
|
|
let admin_user = config.admin_user.clone();
|
|
|
|
let admin_pass = config.admin_pass.clone();
|
2021-08-21 20:22:19 +02:00
|
|
|
|
2021-08-21 18:05:16 +02:00
|
|
|
let conn = RbDbConn::get_one(&rocket)
|
|
|
|
.await
|
|
|
|
.expect("database connection");
|
2021-08-21 21:42:36 +02:00
|
|
|
conn.run(move |c| {
|
2021-09-05 11:19:18 +02:00
|
|
|
admin::create_admin_user(c, &admin_user, &admin_pass)
|
2021-08-21 21:42:36 +02:00
|
|
|
.expect("failed to create admin user")
|
|
|
|
})
|
|
|
|
.await;
|
2021-08-21 18:05:16 +02:00
|
|
|
}
|
|
|
|
|
2021-08-30 15:28:01 +02:00
|
|
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
|
|
|
pub struct RbJwtConf
|
|
|
|
{
|
|
|
|
key: String,
|
|
|
|
refresh_token_size: usize,
|
|
|
|
refresh_token_expire: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
pub struct RbConfig
|
|
|
|
{
|
|
|
|
admin_user: String,
|
|
|
|
admin_pass: String,
|
|
|
|
jwt: RbJwtConf,
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:46:50 +02:00
|
|
|
#[launch]
|
2021-08-22 16:45:01 +02:00
|
|
|
fn rocket() -> _
|
|
|
|
{
|
2021-08-30 14:27:54 +02:00
|
|
|
let figment = Figment::from(rocket::config::Config::default())
|
|
|
|
.merge(Yaml::file("Rb.yaml").nested())
|
|
|
|
.merge(Env::prefixed("RB_").global());
|
|
|
|
|
|
|
|
rocket::custom(figment)
|
2021-08-19 22:46:50 +02:00
|
|
|
.attach(RbDbConn::fairing())
|
|
|
|
.attach(AdHoc::try_on_ignite(
|
|
|
|
"Run database migrations",
|
|
|
|
run_db_migrations,
|
|
|
|
))
|
2021-09-05 11:19:18 +02:00
|
|
|
// .attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))
|
2021-08-30 15:28:01 +02:00
|
|
|
.attach(AdHoc::config::<RbConfig>())
|
2021-09-01 17:29:39 +02:00
|
|
|
.register("/", catchers![default_catcher])
|
2021-08-29 20:30:33 +02:00
|
|
|
.mount(
|
|
|
|
"/api/auth",
|
|
|
|
routes![auth::already_logged_in, auth::login, auth::refresh_token,],
|
|
|
|
)
|
2021-08-29 20:41:03 +02:00
|
|
|
.mount(
|
|
|
|
"/api/admin",
|
|
|
|
routes![admin::get_users, admin::create_user, admin::get_user_info],
|
|
|
|
)
|
2021-08-18 22:06:37 +02:00
|
|
|
}
|