Browse Source

serve: switch from rocket to gotham

master
Astro 12 months ago
parent
commit
d1566f6c31
  1. 2163
      Cargo.lock
  2. 5
      Cargo.toml
  3. 5
      ticker-serve/Cargo.toml
  4. 73
      ticker-serve/src/main.rs

2163
Cargo.lock

File diff suppressed because it is too large

5
Cargo.toml

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

5
ticker-serve/Cargo.toml

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

73
ticker-serve/src/main.rs

@ -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)
}

Loading…
Cancel
Save