aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: ed768b4b324f6435cc2a1bd3d678c36c69e66b2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
mod error;
mod live;
mod posts;
mod scrobbles;
mod skweets;

use std::borrow::Cow;
use std::collections::HashSet;
use std::time::Duration;

use atom_syndication::Feed;
use rocket::fs::{relative, FileServer};
use rocket::http::{ContentType, 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,
    reqwest: reqwest::Client,
}

#[macro_use]
extern crate rocket;

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

#[get("/blog/<blogpost>")]
async fn blogpost(blogpost: &str) -> Result<Template> {
    let mut blogpost = posts::get_blogpost(blogpost).await?;
    blogpost.render().await?;
    Ok(Template::render(
        "blogpost",
        context! {
            blogpost,
        },
    ))
}

#[get("/blog?<filter>")]
async fn blog(filter: Vec<String>) -> Result<Template> {
    let mut blogposts = posts::get_blogposts().await?;
    let tags: Vec<String> = posts::get_tags(&blogposts)
        .into_iter()
        .map(|tag| tag.to_owned())
        .collect();
    let mut filter_hashset: HashSet<String> = HashSet::new();
    if !filter.is_empty() {
        filter_hashset.extend(filter.into_iter());
        blogposts = posts::filter_by_tags(blogposts, &filter_hashset);
    }
    for blogpost in &mut blogposts {
        blogpost.render().await?;
    }
    let reverse = true;
    Ok(Template::render(
        "blog",
        context! {
            reverse,
            tags,
            filter_hashset,
            blogposts,
        },
    ))
}

#[get("/feed")]
async fn feed() -> Result<(Status, (ContentType, String))> {
    let posts = posts::get_blogposts().await?;
    let feed = posts::syndication::atom(posts).await;
    let feed: String = String::from_utf8(feed.write_to(Vec::new())?)?;
    Ok((
        Status::new(200),
        (ContentType::new("application", "atom+xml"), feed),
    ))
}

#[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),
            reqwest: reqwest::Client::builder()
                .connect_timeout(Duration::from_secs(1))
                .build()
                .unwrap(),
        })
        .attach(Template::custom(|engines| {
            engines.tera.autoescape_on(vec![]);
        }))
        .mount("/", routes![home, contact, blog, blogpost, feed, plants])
        .register("/", catchers![catcher])
        .mount("/", FileServer::from("./static"))
        .launch()
        .await?;

    Ok(())
}