ticker/ticker-serve/src/main.rs

50 lines
1.2 KiB
Rust
Raw Normal View History

2019-10-26 02:19:33 +02:00
#![recursion_limit="1024"]
2019-10-26 01:14:49 +02:00
2020-10-26 16:20:11 +01:00
#[macro_use]
extern crate gotham_derive;
use std::sync::{Arc, Mutex};
use gotham::{
handler::assets::FileOptions,
router::builder::{DefineSingleRoute, DrawRoutes},
middleware::state::StateMiddleware,
pipeline::single::single_pipeline,
pipeline::single_middleware,
router::builder::*,
};
2020-10-26 16:51:25 +01:00
use diesel::{Connection, pg::PgConnection};
2020-10-26 16:20:11 +01:00
2020-10-26 16:51:25 +01:00
use libticker::config::Config;
mod index;
2019-10-26 02:19:33 +02:00
2020-10-26 16:20:11 +01:00
#[derive(Clone, StateData)]
2020-10-26 16:51:25 +01:00
pub struct AppState {
pub db: Arc<Mutex<PgConnection>>,
2019-10-26 01:14:49 +02:00
}
fn main() {
2020-10-26 16:20:11 +01:00
let config = Config::read_yaml_file("../config.yaml");
2019-10-26 01:14:49 +02:00
let db = PgConnection::establish(&config.db_url)
.expect("DB");
2020-10-26 16:20:11 +01:00
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| {
2020-10-26 16:51:25 +01:00
route.get("/").to(index::index);
2020-10-26 16:20:11 +01:00
route.get("static/*").to_dir(
FileOptions::new(&"static")
// TODO:
.with_cache_control("no-cache")
.with_gzip(true)
.build()
);
});
gotham::start("[::1]:8400", router)
2019-10-26 01:14:49 +02:00
}