mi/backend/src/main.rs

98 lines
3.0 KiB
Rust
Raw Normal View History

2020-11-02 18:06:50 +00:00
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
#[macro_use]
extern crate tracing;
2020-09-13 16:07:06 +00:00
2020-09-13 15:23:53 +00:00
use color_eyre::eyre::Result;
use rocket::{fairing::AdHoc, http::Method};
2020-11-02 18:06:50 +00:00
use rocket_contrib::helmet::SpaceHelmet;
2020-11-04 21:02:16 +00:00
use rocket_cors::{AllowedHeaders, AllowedOrigins};
2020-11-02 18:06:50 +00:00
use rocket_prometheus::PrometheusMetrics;
2020-09-13 15:23:53 +00:00
2020-11-10 22:11:05 +00:00
use ::mi::{api, frontend, paseto, rocket_trace::*, web::*, MainDatabase, APPLICATION_NAME};
2020-11-02 18:06:50 +00:00
2020-11-03 15:26:51 +00:00
#[get("/.within/botinfo")]
fn botinfo() -> &'static str {
2020-11-16 02:52:38 +00:00
include_str!("./botinfo.txt")
2020-11-03 15:26:51 +00:00
}
2020-11-02 18:06:50 +00:00
fn main() -> Result<()> {
2020-09-13 15:23:53 +00:00
color_eyre::install()?;
2020-11-02 18:06:50 +00:00
tracing_subscriber::fmt::init();
info!("{} starting up", APPLICATION_NAME);
let allowed_origins = AllowedOrigins::some_exact(&[
"https://mi.within.website",
"http://localhost:8000",
"http://mi.akua",
]);
2020-11-04 21:02:16 +00:00
// You can also deserialize this
let cors = rocket_cors::CorsOptions {
allowed_origins,
allowed_methods: vec![Method::Get, Method::Post]
.into_iter()
.map(From::from)
.collect(),
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
allow_credentials: true,
..Default::default()
}
.to_cors()?;
2020-11-02 18:06:50 +00:00
let prometheus = PrometheusMetrics::with_registry(prometheus::default_registry().clone());
rocket::ignite()
.attach(prometheus.clone())
2020-11-04 21:02:16 +00:00
.attach(cors)
2020-11-02 18:06:50 +00:00
.attach(SpaceHelmet::default())
2020-11-10 22:11:05 +00:00
.attach(static_files())
.attach(frontend::fairing())
.attach(MainDatabase::fairing())
2020-11-05 22:52:32 +00:00
.attach(RequestId {})
2020-11-03 19:55:03 +00:00
.attach(paseto::ed25519_keypair())
2020-11-04 17:36:12 +00:00
.attach(DiscordWebhook::fairing())
.attach(Mastodon::fairing())
.attach(PluralKit::fairing())
.attach(SwitchCounter::fairing())
.attach(Twitter::fairing())
.attach(AdHoc::on_launch("systemd readiness", |_| {
if let Ok(ref mut n) = sdnotify::SdNotify::from_env() {
let _ = n
.notify_ready()
.map_err(|why| error!("can't signal readiness to systemd: {}", why));
}
}))
2020-11-02 18:06:50 +00:00
.mount("/metrics", prometheus)
2020-11-03 19:55:03 +00:00
.mount("/", routes![botinfo])
2020-11-02 18:06:50 +00:00
.mount(
2020-11-03 19:55:03 +00:00
"/api",
2020-11-02 18:06:50 +00:00
routes![
api::package_tracking::orangeconnex::list,
api::package_tracking::orangeconnex::recieved,
api::package_tracking::orangeconnex::status,
api::package_tracking::orangeconnex::track,
2020-11-04 20:21:12 +00:00
api::posse::notify,
2020-11-04 18:41:46 +00:00
api::posse::refresh_blog,
api::switch::current_front,
2020-11-04 20:21:12 +00:00
api::switch::current_front_text,
api::switch::get,
api::switch::list,
api::switch::switch,
2020-11-04 17:18:17 +00:00
api::webmention::accept,
api::webmention::get,
api::webmention::lookup_target,
2020-11-04 18:41:46 +00:00
api::webmention::list,
2020-11-02 18:06:50 +00:00
api::get_members,
2020-11-04 15:24:26 +00:00
api::token_info,
api::tweet,
2020-11-04 15:54:21 +00:00
api::toot,
2020-11-02 18:06:50 +00:00
],
)
.launch();
2020-09-13 15:23:53 +00:00
Ok(())
}