ticker/ticker-serve/src/main.rs

41 lines
998 B
Rust
Raw Normal View History

use std::sync::{Arc, Mutex};
use axum::{
Router,
routing::get, Extension,
2020-10-26 16:20:11 +01:00
};
use tower_http::services::ServeDir;
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;
mod export;
2019-10-26 02:19:33 +02:00
#[derive(Clone)]
2020-10-26 16:51:25 +01:00
pub struct AppState {
pub db: Arc<Mutex<PgConnection>>,
pub config: Arc<Config>,
2019-10-26 01:14:49 +02:00
}
#[tokio::main]
async fn main() {
2020-10-27 19:20:34 +01:00
let config = Config::read_yaml_file("config.yaml");
let listener = tokio::net::TcpListener::bind(&config.http_bind)
.await
.unwrap();
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 {
2020-10-26 19:48:17 +01:00
db: Arc::new(Mutex::new(db)),
config: Arc::new(config),
2020-10-26 16:20:11 +01:00
};
let app = Router::new()
.route("/", get(index::index))
.route("/export.ics", get(export::ics))
.layer(Extension(state))
.nest_service("/static", ServeDir::new("static"));
axum::serve(listener, app.into_make_service())
.await
.unwrap();
2019-10-26 01:14:49 +02:00
}