|
|
|
@ -1,9 +1,23 @@
|
|
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
|
|
|
|
#![recursion_limit="1024"]
|
|
|
|
|
|
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
#[macro_use] extern crate rocket;
|
|
|
|
|
use rocket::{State, response::content};
|
|
|
|
|
#[macro_use]
|
|
|
|
|
extern crate gotham_derive;
|
|
|
|
|
|
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
use gotham::{
|
|
|
|
|
handler::assets::FileOptions,
|
|
|
|
|
helpers::http::response::create_response,
|
|
|
|
|
hyper::{Body, Response},
|
|
|
|
|
router::builder::{DefineSingleRoute, DrawRoutes},
|
|
|
|
|
middleware::state::StateMiddleware,
|
|
|
|
|
pipeline::single::single_pipeline,
|
|
|
|
|
pipeline::single_middleware,
|
|
|
|
|
router::builder::*,
|
|
|
|
|
state::{FromState, State},
|
|
|
|
|
};
|
|
|
|
|
use http::status::StatusCode;
|
|
|
|
|
use mime::TEXT_HTML;
|
|
|
|
|
|
|
|
|
|
use typed_html::{html, text, dom::DOMTree};
|
|
|
|
|
use diesel::{Connection, pg::PgConnection, prelude::*};
|
|
|
|
|
use chrono::{offset::Local, NaiveDate};
|
|
|
|
@ -50,9 +64,13 @@ fn group_by_day(es: &[Event]) -> Vec<DayEvents> {
|
|
|
|
|
results
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
|
fn index(db: State<Mutex<PgConnection>>) -> content::Html<String> {
|
|
|
|
|
let db = db.lock().unwrap();
|
|
|
|
|
#[derive(Clone, StateData)]
|
|
|
|
|
struct AppState {
|
|
|
|
|
db: Arc<Mutex<PgConnection>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render_index(app_state: &AppState) -> String {
|
|
|
|
|
let db = app_state.db.lock().unwrap();
|
|
|
|
|
let today = Local::today().naive_local().and_hms(0, 0, 0);
|
|
|
|
|
let es = events
|
|
|
|
|
.filter(schema::events::dtstart.ge(&today))
|
|
|
|
@ -65,7 +83,8 @@ fn index(db: State<Mutex<PgConnection>>) -> content::Html<String> {
|
|
|
|
|
let doc: DOMTree<String> = html!(
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<title>"Ticker"</title>
|
|
|
|
|
<title>"Ticker"</title>
|
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<h1>"Ticker"</h1>
|
|
|
|
@ -100,18 +119,40 @@ fn index(db: State<Mutex<PgConnection>>) -> content::Html<String> {
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
);
|
|
|
|
|
content::Html(doc.to_string())
|
|
|
|
|
format!("<DOCTYPE html>\n{}", doc.to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn index(state: State) -> (State, Response<Body>) {
|
|
|
|
|
let message = {
|
|
|
|
|
let app_state = AppState::borrow_from(&state);
|
|
|
|
|
render_index(app_state)
|
|
|
|
|
};
|
|
|
|
|
let res = create_response(&state, StatusCode::OK, TEXT_HTML, message);
|
|
|
|
|
(state, res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let config = Config::read_yaml_file("config.yaml");
|
|
|
|
|
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();
|
|
|
|
|
let state = AppState {
|
|
|
|
|
db: Arc::new(Mutex::new(db))
|
|
|
|
|
};
|
|
|
|
|
let (chain, pipelines) = single_pipeline(
|
|
|
|
|
single_middleware(
|
|
|
|
|
StateMiddleware::new(state)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
let router = build_router(chain, pipelines, |route| {
|
|
|
|
|
route.get("/").to(index);
|
|
|
|
|
route.get("static/*").to_dir(
|
|
|
|
|
FileOptions::new(&"static")
|
|
|
|
|
// TODO:
|
|
|
|
|
.with_cache_control("no-cache")
|
|
|
|
|
.with_gzip(true)
|
|
|
|
|
.build()
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
gotham::start("[::1]:8400", router)
|
|
|
|
|
}
|
|
|
|
|