beacon/src/main.rs

52 lines
1.2 KiB
Rust

use std::sync::Arc;
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Router};
use data::Data;
use maud::Markup;
use tower_http::services::ServeDir;
pub mod clock;
pub mod data;
pub mod forecast;
pub mod templates;
pub struct AppState {
pub data: Data,
}
pub struct AppError(anyhow::Error);
impl IntoResponse for AppError {
fn into_response(self) -> axum::response::Response {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {}", self.0),
)
.into_response()
}
}
impl<E> From<E> for AppError
where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}
#[axum::debug_handler]
async fn main_handler(state: State<Arc<AppState>>) -> Result<Markup, AppError> {
Ok(templates::main_page(state))
}
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(main_handler))
.nest_service("/static", ServeDir::new("static"))
.with_state(Arc::new(AppState { data: Data::new() }));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
println!("Listening on http://localhost:3000!");
axum::serve(listener, app).await.unwrap();
}