ticker/ticker-serve/src/main.rs

84 lines
2.2 KiB
Rust

#![feature(proc_macro_hygiene, decl_macro)]
#![recursion_limit="512"]
use std::fs::read_to_string;
use std::sync::Mutex;
#[macro_use] extern crate rocket;
use rocket::{State, response::content};
use typed_html::{html, text, dom::DOMTree};
use diesel::{Connection, pg::PgConnection, prelude::*};
use libticker::{
config::{Config, CalendarOptions},
schema::{self, events::dsl::events},
model::{Calendar, Event},
ics::{Object, Timestamp, GetValue},
};
fn fix_url(s: &str) -> std::borrow::Cow<str> {
if s.starts_with("http:") || s.starts_with("https:") {
s.into()
} else {
format!("http://{}", s).into()
}
}
#[get("/")]
fn index(db: State<Mutex<PgConnection>>) -> content::Html<String> {
let db = db.lock().unwrap();
let es = events
.order_by(schema::events::dtstart.asc())
.then_order_by(schema::events::dtend.desc())
.load::<Event>(&*db)
.unwrap();
let doc: DOMTree<String> = html!(
<html>
<head>
<title>"Ticker"</title>
</head>
<body>
<h1>"Ticker"</h1>
{ es.iter().map(|e| html!(
<article class="event">
{ match &e.url {
None => html!(
<h2>{ text!("{}", &e.summary) }</h2>
),
Some(url) => html!(
<h2>
<a href={ fix_url(url) }>
{ text!("{}", &e.summary) }
</a>
</h2>
),
} }
<p class="dtstart">{ text!("{}", &e.dtstart) }</p>
{ e.location.as_ref().map(|location| html!(
<p>
{ text!("{}", location) }
</p>
)) }
</article>
)) }
</body>
</html>
);
content::Html(doc.to_string())
}
fn main() {
let config = Config::read_yaml_file("config.yaml");
let db = PgConnection::establish(&config.db_url)
.expect("DB");
rocket::ignite()
.manage(Mutex::new(db))
.mount("/", routes![
index,
])
.launch();
}