serve: switch from rocket to gotham

This commit is contained in:
Astro 2020-10-26 16:20:11 +01:00
parent 650d41b39f
commit d1566f6c31
4 changed files with 1479 additions and 797 deletions

2193
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -3,4 +3,7 @@ members = [
"ticker-update", "ticker-update",
"ticker-serve", "ticker-serve",
"libticker", "libticker",
] ]
[patch.crates-io]
typed-html = { git = "https://github.com/bodil/typed-html", rev = "4c13ecca506887d07638cdf12d6ea6d51cd3b29a" }

View File

@ -5,7 +5,10 @@ authors = ["Astro <astro@spaceboyz.net>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
rocket = "0.4" gotham = "0.5"
gotham_derive = "0.5"
http = "0.2"
mime = "0.3"
typed-html = "0.2" typed-html = "0.2"
diesel = { version = "~1", features = ["postgres", "chrono"] } diesel = { version = "~1", features = ["postgres", "chrono"] }
chrono = "~0.4" chrono = "~0.4"

View File

@ -1,9 +1,23 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![recursion_limit="1024"] #![recursion_limit="1024"]
use std::sync::Mutex; #[macro_use]
#[macro_use] extern crate rocket; extern crate gotham_derive;
use rocket::{State, response::content};
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 typed_html::{html, text, dom::DOMTree};
use diesel::{Connection, pg::PgConnection, prelude::*}; use diesel::{Connection, pg::PgConnection, prelude::*};
use chrono::{offset::Local, NaiveDate}; use chrono::{offset::Local, NaiveDate};
@ -50,9 +64,13 @@ fn group_by_day(es: &[Event]) -> Vec<DayEvents> {
results results
} }
#[get("/")] #[derive(Clone, StateData)]
fn index(db: State<Mutex<PgConnection>>) -> content::Html<String> { struct AppState {
let db = db.lock().unwrap(); 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 today = Local::today().naive_local().and_hms(0, 0, 0);
let es = events let es = events
.filter(schema::events::dtstart.ge(&today)) .filter(schema::events::dtstart.ge(&today))
@ -65,7 +83,8 @@ fn index(db: State<Mutex<PgConnection>>) -> content::Html<String> {
let doc: DOMTree<String> = html!( let doc: DOMTree<String> = html!(
<html> <html>
<head> <head>
<title>"Ticker"</title> <title>"Ticker"</title>
<meta charset="utf-8" />
</head> </head>
<body> <body>
<h1>"Ticker"</h1> <h1>"Ticker"</h1>
@ -100,18 +119,40 @@ fn index(db: State<Mutex<PgConnection>>) -> content::Html<String> {
</body> </body>
</html> </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() { 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) let db = PgConnection::establish(&config.db_url)
.expect("DB"); .expect("DB");
rocket::ignite() let state = AppState {
.manage(Mutex::new(db)) db: Arc::new(Mutex::new(db))
.mount("/", routes![ };
index, let (chain, pipelines) = single_pipeline(
]) single_middleware(
.launch(); 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)
} }