aboutsummaryrefslogblamecommitdiffstats
path: root/src/main.rs
blob: ab65f8e18e9c5e9caa94d1f958547afe9709bdb0 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
          
          

              
 
                     
 



                                              
 


                                                      
 


                                            

 

                    
 
           






                                                             
                        
               

                       


                     
      






                                            
                 
                                       
                                                            

 
                 
                                                       







                                                                                               
                                    
     
                                       





                                                                      
              
                                                           


                                                          
                                 



                                                                     


                                               
                                                   
                                          





                                                          
mod error;
mod posts;
mod scrobbles;
mod skweets;

use std::borrow::Cow;

use rocket::fs::{relative, FileServer};
use rocket::http::Status;
use rocket::{Request, State};
use rocket_dyn_templates::{context, Template};

use error::BlossomError;

type Result<T> = std::result::Result<T, BlossomError>;

struct Clients {
    listenbrainz: listenbrainz::raw::Client,
    skinnyverse: mastodon_async::Mastodon,
}

#[macro_use]
extern crate rocket;

#[get("/")]
async fn home(clients: &State<Clients>) -> Result<Template> {
    let (listenbrainz, skweets) = tokio::join!(
        scrobbles::get_now_playing(&clients.listenbrainz),
        skweets::get_recents(&clients.skinnyverse)
    );
    let listenbrainz = listenbrainz.unwrap_or_default();
    let skweets = skweets.unwrap_or_default();
    Ok(Template::render(
        "home",
        context! {
        is_live: false,
        listenbrainz,
        skweets,
        },
    ))
}

#[get("/contact")]
async fn contact() -> Template {
    Template::render("contact", context! {})
}

#[get("/plants")]
async fn plants() -> Result<Template> {
    Err(BlossomError::Unimplemented(Status::NotImplemented))
}

#[catch(default)]
fn catcher(status: Status, req: &Request) -> Template {
    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 {
        message = "idk i got bored";
    }
    let status = format!("{}", status);
    Template::render(
        "error",
        context! { status: status, req: req.uri(), message: message },
    )
}

#[tokio::main]
async fn main() -> std::result::Result<(), rocket::Error> {
    let mut skinny_data = mastodon_async::Data::default();
    skinny_data.base = Cow::from("https://skinnyver.se");

    let _rocket = rocket::build()
        .manage(Clients {
            listenbrainz: listenbrainz::raw::Client::new(),
            skinnyverse: mastodon_async::Mastodon::from(skinny_data),
        })
        .attach(Template::custom(|engines| {
            engines.tera.autoescape_on(vec![]);
        }))
        .mount("/", routes![home, contact, plants])
        .register("/", catchers![catcher])
        .mount("/", FileServer::from(relative!("static")))
        .launch()
        .await?;

    Ok(())
}