2021-11-23 09:32:08 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rocket;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate diesel_migrations;
|
|
|
|
|
|
|
|
use figment::{
|
|
|
|
providers::{Env, Format, Yaml},
|
|
|
|
Figment,
|
|
|
|
};
|
2021-11-23 20:31:54 +01:00
|
|
|
use rb::auth::JwtConf;
|
2021-11-23 09:32:08 +01:00
|
|
|
use rocket::{
|
|
|
|
fairing::AdHoc,
|
|
|
|
http::Status,
|
|
|
|
serde::json::{json, Value},
|
|
|
|
Build, Request, Rocket,
|
|
|
|
};
|
|
|
|
use rocket_sync_db_pools::database;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2021-11-24 17:51:52 +01:00
|
|
|
pub mod v1;
|
2021-11-23 09:32:08 +01:00
|
|
|
|
2021-11-24 18:52:02 +01:00
|
|
|
/// This function just returns a pre-built rocket header containing a valid JWT for the secret
|
|
|
|
/// "secret", valid until 2034 or something. It's easy for testing the API.
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn auth_header() -> rocket::http::Header<'static>
|
|
|
|
{
|
|
|
|
return rocket::http::Header::new("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjVjMjM2OTI0NjY4ZDQzZWFiNGNmNDczYjk1YWZiNzgzIiwidXNlcm5hbWUiOiJKb2huIERvZSIsImFkbWluIjp0cnVlLCJleHAiOjE1MTYyMzkwMjIwfQ.if939L9le8LP-dtXnQs-mHPkb-VieRAvAfSu20755jY");
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:32:08 +01:00
|
|
|
#[database("postgres_rb")]
|
|
|
|
pub struct RbDbConn(diesel::PgConnection);
|
|
|
|
|
|
|
|
#[catch(default)]
|
|
|
|
fn default_catcher(status: Status, _: &Request) -> Value
|
|
|
|
{
|
2021-11-23 20:21:29 +01:00
|
|
|
json!({"status": status.code, "message": ""})
|
2021-11-23 09:32:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
pub struct RbConfig
|
|
|
|
{
|
2021-11-23 17:49:31 +01:00
|
|
|
jwt: JwtConf,
|
2021-11-23 09:32:08 +01: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-23 18:00:10 +01:00
|
|
|
let rocket = rocket::custom(figment)
|
2021-11-23 09:32:08 +01: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))
|
2021-11-23 18:00:10 +01:00
|
|
|
// .attach(AdHoc::config::<JwtConf>())
|
2021-11-23 09:32:08 +01:00
|
|
|
.register("/", catchers![default_catcher])
|
2021-11-23 20:21:29 +01:00
|
|
|
.mount(
|
2021-11-24 17:51:52 +01:00
|
|
|
"/v1/sections",
|
2021-11-23 20:21:29 +01:00
|
|
|
routes![
|
2021-11-24 17:51:52 +01:00
|
|
|
v1::sections::get,
|
|
|
|
v1::sections::create,
|
|
|
|
v1::sections::find,
|
|
|
|
v1::sections::patch,
|
|
|
|
v1::sections::delete
|
2021-11-23 20:21:29 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
.mount(
|
2021-11-24 17:51:52 +01:00
|
|
|
"/v1/posts",
|
2021-11-23 20:21:29 +01:00
|
|
|
routes![
|
2021-11-24 17:51:52 +01:00
|
|
|
v1::posts::get,
|
|
|
|
v1::posts::create,
|
|
|
|
v1::posts::find,
|
|
|
|
v1::posts::patch,
|
|
|
|
v1::posts::delete
|
2021-11-23 20:21:29 +01:00
|
|
|
],
|
2021-11-23 20:31:54 +01:00
|
|
|
);
|
2021-11-23 18:00:10 +01:00
|
|
|
|
|
|
|
let new_figment = rocket.figment();
|
|
|
|
let jwt_conf: JwtConf = new_figment.extract_inner("jwt").expect("jwt config");
|
|
|
|
|
|
|
|
rocket.manage(jwt_conf)
|
2021-11-23 09:32:08 +01:00
|
|
|
}
|