2021-11-09 09:42:16 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rocket;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_migrations;
|
|
|
|
|
|
|
|
use figment::{
|
|
|
|
providers::{Env, Format, Yaml},
|
|
|
|
Figment,
|
|
|
|
};
|
2021-11-25 09:19:01 +00:00
|
|
|
use rb::{auth::JwtConf, errors::RbError};
|
|
|
|
use rb_gw::db;
|
2021-11-09 09:42:16 +00:00
|
|
|
use rocket::{
|
|
|
|
fairing::AdHoc,
|
|
|
|
http::Status,
|
|
|
|
serde::json::{json, Value},
|
2021-11-25 13:14:33 +00:00
|
|
|
Build, Request, Rocket,
|
2021-11-09 09:42:16 +00:00
|
|
|
};
|
|
|
|
use rocket_sync_db_pools::database;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2021-11-25 13:14:33 +00:00
|
|
|
use proxy::ProxyServer;
|
2021-11-09 09:42:16 +00:00
|
|
|
|
2021-11-25 00:02:23 +00:00
|
|
|
pub mod v1;
|
2021-11-25 13:14:33 +00:00
|
|
|
mod proxy;
|
2021-11-09 09:42:16 +00: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 09:19:01 +00:00
|
|
|
async fn create_admin_user(rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocket<Build>>
|
|
|
|
{
|
|
|
|
let admin = rocket.state::<AdminConfig>().expect("admin config");
|
|
|
|
|
|
|
|
let conn = RbDbConn::get_one(&rocket)
|
|
|
|
.await
|
|
|
|
.expect("database connection");
|
|
|
|
|
|
|
|
let new_user = db::NewUser {
|
|
|
|
username: admin.username.clone(),
|
|
|
|
password: admin.password.clone(),
|
|
|
|
admin: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
match conn
|
|
|
|
.run(move |c| db::users::create_or_update(c, new_user))
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(_) => Ok(rocket),
|
|
|
|
Err(RbError::UMDuplicateUser) => Ok(rocket),
|
|
|
|
Err(_) => Err(rocket),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
pub struct AdminConfig
|
|
|
|
{
|
|
|
|
username: String,
|
|
|
|
password: String,
|
|
|
|
}
|
2021-11-09 09:42:16 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
pub struct RbConfig
|
|
|
|
{
|
2021-11-25 09:19:01 +00:00
|
|
|
admin: AdminConfig,
|
2021-11-25 00:02:23 +00:00
|
|
|
jwt: JwtConf,
|
2021-11-09 09:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[launch]
|
|
|
|
fn rocket() -> _
|
|
|
|
{
|
|
|
|
let figment = Figment::from(rocket::config::Config::default())
|
|
|
|
.merge(Yaml::file("Rb.yaml").nested())
|
|
|
|
.merge(Env::prefixed("RB_").global());
|
|
|
|
|
2021-11-25 09:19:01 +00:00
|
|
|
let rocket = rocket::custom(figment)
|
2021-11-09 09:42:16 +00:00
|
|
|
.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 00:02:23 +00:00
|
|
|
"/v1/auth",
|
|
|
|
routes![
|
|
|
|
v1::auth::already_logged_in,
|
|
|
|
v1::auth::login,
|
|
|
|
v1::auth::refresh_token,
|
|
|
|
],
|
2021-11-25 13:14:33 +00:00
|
|
|
).mount("/v1/posts", ProxyServer::from("http://localhost:8000/v1/posts"));
|
2021-11-25 09:19:01 +00:00
|
|
|
|
|
|
|
// This let's the guards properly access the JWT credentials when needed
|
|
|
|
let new_figment = rocket.figment();
|
|
|
|
let jwt_conf: JwtConf = new_figment.extract_inner("jwt").expect("jwt config");
|
|
|
|
|
|
|
|
// We do the same thing here so we can access the admin credentials for initially creating the
|
|
|
|
// admin user
|
|
|
|
let admin_conf: AdminConfig = new_figment.extract_inner("admin").expect("admin config");
|
|
|
|
|
|
|
|
rocket
|
|
|
|
.manage(jwt_conf)
|
|
|
|
.manage(admin_conf)
|
|
|
|
.attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))
|
2021-11-09 09:42:16 +00:00
|
|
|
}
|