blossom/src/main.rs

112 lines
3.1 KiB
Rust
Raw Normal View History

2023-02-19 02:11:17 +00:00
use rocket::fairing::{self, AdHoc};
2023-01-15 21:43:21 +00:00
use rocket::fs::{relative, FileServer};
2023-01-16 05:49:03 +00:00
use rocket::http::Status;
use rocket::State;
2023-02-19 02:11:17 +00:00
use rocket::{Build, Request, Rocket};
use rocket_db_pools::sqlx::{self, database};
use rocket_db_pools::{Connection, Database};
2023-01-15 21:43:21 +00:00
use rocket_dyn_templates::{context, Template};
use std::borrow::Cow;
2023-01-15 21:43:21 +00:00
mod scrobbles;
mod skweets;
2023-01-15 21:43:21 +00:00
2023-02-19 02:11:17 +00:00
#[derive(Database)]
#[database("blossom")]
struct Blossom(sqlx::SqlitePool);
async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
match Blossom::fetch(&rocket) {
Some(db) => match sqlx::migrate!().run(&**db).await {
Ok(_) => Ok(rocket),
Err(e) => {
error!("failed to init blossom database: {}", e);
Err(rocket)
}
},
None => Err(rocket),
}
}
fn stage() -> AdHoc {
AdHoc::on_ignite("blossom database stage", |rocket| async {
rocket
.attach(Blossom::init())
.attach(AdHoc::try_on_ignite("database migrations", run_migrations))
})
}
struct Clients {
listenbrainz: listenbrainz::raw::Client,
skinnyverse: mastodon_async::Mastodon,
2023-01-16 05:18:41 +00:00
}
#[macro_use]
extern crate rocket;
2023-01-16 05:18:41 +00:00
2023-01-15 21:43:21 +00:00
#[get("/")]
async fn home(clients: &State<Clients>) -> Result<Template, BlossomError> {
Ok(Template::render(
2023-01-16 05:18:41 +00:00
"home",
2023-02-16 18:02:20 +00:00
context! {
is_live: false,
listenbrainz: scrobbles::get_now_playing(&clients.listenbrainz).await.unwrap_or_default(),
skweets: skweets::get_recents(&clients.skinnyverse).await.unwrap_or_default() },
))
2023-01-15 21:43:21 +00:00
}
#[get("/contact")]
async fn contact() -> Template {
Template::render("contact", context! {})
}
2023-01-17 02:31:48 +00:00
#[get("/plants")]
async fn plants() -> Result<Template, BlossomError> {
2023-02-16 18:02:20 +00:00
Err(BlossomError::Unimplemented(Status::NotImplemented))
2023-01-17 02:31:48 +00:00
}
2023-01-16 05:49:03 +00:00
#[catch(default)]
fn catcher(status: Status, req: &Request) -> Template {
2023-01-16 05:54:09 +00:00
let message;
if status.code == 404 {
message = "i either haven't built this page yet or it looks like you're a little lost";
} else if status.code == 500 {
message = "omg the server went kaputt!!";
} else if status.code == 501 {
message = "it looks like this is not yet here!!!";
} else {
2023-02-16 18:02:20 +00:00
message = "idk i got bored";
2023-01-16 05:54:09 +00:00
}
2023-01-16 05:49:03 +00:00
let status = format!("{}", status);
Template::render(
"error",
context! { status: status, req: req.uri(), message: message },
)
}
2023-01-15 21:43:21 +00:00
#[tokio::main]
async fn main() -> Result<(), rocket::Error> {
let mut skinny_data = mastodon_async::Data::default();
skinny_data.base = Cow::from("https://skinnyver.se");
2023-01-15 21:43:21 +00:00
let _rocket = rocket::build()
2023-02-19 02:11:17 +00:00
.attach(stage())
.manage(Clients {
listenbrainz: listenbrainz::raw::Client::new(),
skinnyverse: mastodon_async::Mastodon::from(skinny_data),
})
2023-01-17 02:31:48 +00:00
.attach(Template::custom(|engines| {
engines.tera.autoescape_on(vec![]);
}))
2023-02-16 18:02:20 +00:00
.mount("/", routes![home, contact, plants])
.register("/", catchers![catcher])
2023-01-15 21:43:21 +00:00
.mount("/", FileServer::from(relative!("static")))
.launch()
.await?;
Ok(())
}
mod error;
use error::BlossomError;