2021-11-09 10:42:16 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rocket;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_migrations;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel;
|
|
|
|
|
|
|
|
use figment::{
|
|
|
|
providers::{Env, Format, Yaml},
|
|
|
|
Figment,
|
|
|
|
};
|
2021-11-25 01:02:23 +01:00
|
|
|
use rb::auth::JwtConf;
|
2021-11-09 10:42:16 +01:00
|
|
|
use rocket::{
|
|
|
|
fairing::AdHoc,
|
|
|
|
http::Status,
|
|
|
|
serde::json::{json, Value},
|
|
|
|
Build, Orbit, Request, Rocket,
|
|
|
|
};
|
|
|
|
use rocket_sync_db_pools::database;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
pub mod auth;
|
2021-11-25 01:02:23 +01:00
|
|
|
pub mod v1;
|
2021-11-09 10:42:16 +01:00
|
|
|
|
|
|
|
#[database("postgres_rb")]
|
|
|
|
pub struct RbDbConn(diesel::PgConnection);
|
|
|
|
|
|
|
|
#[catch(default)]
|
|
|
|
fn default_catcher(status: Status, _: &Request) -> Value
|
|
|
|
{
|
|
|
|
json!({"status": status.code, "message": ""})
|
|
|
|
}
|
|
|
|
|
|
|
|
embed_migrations!();
|
|
|
|
|
|
|
|
async fn run_db_migrations(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocket<Build>>
|
|
|
|
{
|
|
|
|
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-11-25 01:02:23 +01:00
|
|
|
// async fn create_admin_user<'a>(rocket: &'a Rocket<Orbit>)
|
|
|
|
// {
|
|
|
|
// let config = rocket.state::<RbConfig>().expect("RbConfig instance");
|
|
|
|
// let admin_user = config.admin_user.clone();
|
|
|
|
// let admin_pass = config.admin_pass.clone();
|
2021-11-09 10:42:16 +01:00
|
|
|
|
2021-11-25 01:02:23 +01:00
|
|
|
// let conn = RbDbConn::get_one(&rocket)
|
|
|
|
// .await
|
|
|
|
// .expect("database connection");
|
|
|
|
// conn.run(move |c| {
|
|
|
|
// admin::create_admin_user(c, &admin_user, &admin_pass).expect("failed to create admin user")
|
|
|
|
// })
|
|
|
|
// .await;
|
|
|
|
// }
|
2021-11-09 10:42:16 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
pub struct RbConfig
|
|
|
|
{
|
|
|
|
admin_user: String,
|
|
|
|
admin_pass: String,
|
2021-11-25 01:02:23 +01:00
|
|
|
jwt: JwtConf,
|
2021-11-09 10:42:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[launch]
|
|
|
|
fn rocket() -> _
|
|
|
|
{
|
|
|
|
let figment = Figment::from(rocket::config::Config::default())
|
|
|
|
.merge(Yaml::file("Rb.yaml").nested())
|
|
|
|
.merge(Env::prefixed("RB_").global());
|
|
|
|
|
|
|
|
rocket::custom(figment)
|
|
|
|
.attach(RbDbConn::fairing())
|
|
|
|
.attach(AdHoc::try_on_ignite(
|
|
|
|
"Run database migrations",
|
|
|
|
run_db_migrations,
|
|
|
|
))
|
|
|
|
// .attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))
|
|
|
|
.attach(AdHoc::config::<RbConfig>())
|
|
|
|
.register("/", catchers![default_catcher])
|
|
|
|
.mount(
|
2021-11-25 01:02:23 +01:00
|
|
|
"/v1/auth",
|
|
|
|
routes![
|
|
|
|
v1::auth::already_logged_in,
|
|
|
|
v1::auth::login,
|
|
|
|
v1::auth::refresh_token,
|
|
|
|
],
|
2021-11-09 10:42:16 +01:00
|
|
|
)
|
|
|
|
}
|