Initial
This commit is contained in:
14
src/main.rs
Normal file
14
src/main.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use log::LevelFilter;
|
||||
|
||||
mod web;
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
env_logger::builder()
|
||||
.filter_module("fuck_microsoft_access", LevelFilter::Debug)
|
||||
.init();
|
||||
|
||||
web::start().await?;
|
||||
Ok(())
|
||||
}
|
||||
17
src/web/mod.rs
Normal file
17
src/web/mod.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use axum::{Router, routing::get};
|
||||
|
||||
pub mod pages;
|
||||
|
||||
|
||||
|
||||
|
||||
pub async fn start() -> anyhow::Result<()> {
|
||||
let addr = "0.0.0.0:3000";
|
||||
let app = Router::new()
|
||||
.route("/", get(pages::home::get_page));
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
||||
log::info!("Listening on http://{addr}");
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
Ok(())
|
||||
}
|
||||
31
src/web/pages/error.rs
Normal file
31
src/web/pages/error.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use askama::Template;
|
||||
use crate::web::pages::{BaseTemplate, BaseTemplateCtx};
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "error.html")]
|
||||
pub struct ErrorTemplate {
|
||||
pub ctx: BaseTemplateCtx,
|
||||
pub error: String
|
||||
}
|
||||
|
||||
impl BaseTemplate for ErrorTemplate {
|
||||
fn ctx(&self) -> &BaseTemplateCtx {
|
||||
&self.ctx
|
||||
}
|
||||
fn ctx_mut(&mut self) -> &mut BaseTemplateCtx {
|
||||
&mut self.ctx
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn get_error_page(e: String) -> String {
|
||||
let mut template = ErrorTemplate {
|
||||
ctx: Default::default(),
|
||||
error: e.to_string()
|
||||
};
|
||||
|
||||
template.set_title("Error");
|
||||
|
||||
template.render().unwrap()
|
||||
}
|
||||
50
src/web/pages/home.rs
Normal file
50
src/web/pages/home.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
|
||||
|
||||
use askama::Template;
|
||||
use axum::response::{Html, IntoResponse, Response};
|
||||
|
||||
use axum::{
|
||||
routing::{get, post},
|
||||
http::StatusCode,
|
||||
Json, Router,
|
||||
};
|
||||
|
||||
use crate::web::pages::{BaseTemplate, BaseTemplateCtx};
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "home.html")]
|
||||
pub struct HomeTemplate {
|
||||
pub ctx: BaseTemplateCtx,
|
||||
|
||||
}
|
||||
|
||||
impl BaseTemplate for HomeTemplate {
|
||||
fn ctx(&self) -> &BaseTemplateCtx {
|
||||
&self.ctx
|
||||
}
|
||||
fn ctx_mut(&mut self) -> &mut BaseTemplateCtx {
|
||||
&mut self.ctx
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
pub async fn get_page() -> Response {
|
||||
fn inner() -> anyhow::Result<(StatusCode, String)> {
|
||||
let mut template = HomeTemplate {
|
||||
ctx: Default::default()
|
||||
};
|
||||
|
||||
template.set_title("Home");
|
||||
|
||||
Ok((StatusCode::OK, template.render()?))
|
||||
}
|
||||
match inner() {
|
||||
Ok((status, s)) => (status, Html(s)).into_response(),
|
||||
Err(e) => {
|
||||
let s = crate::web::pages::error::get_error_page(e.to_string()).await;
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, Html(s)).into_response()
|
||||
}
|
||||
}
|
||||
}
|
||||
33
src/web/pages/mod.rs
Normal file
33
src/web/pages/mod.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
|
||||
pub mod home;
|
||||
pub mod error;
|
||||
|
||||
|
||||
pub struct BaseTemplateCtx {
|
||||
pub title: String
|
||||
}
|
||||
|
||||
impl Default for BaseTemplateCtx {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
title: String::from("Default Title")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait BaseTemplate {
|
||||
fn ctx(&self) -> &BaseTemplateCtx;
|
||||
fn ctx_mut(&mut self) -> &mut BaseTemplateCtx;
|
||||
|
||||
|
||||
fn set_title(&mut self, title: &str) {
|
||||
self.ctx_mut().title = title.to_string();
|
||||
}
|
||||
|
||||
|
||||
fn title(&self) -> &String {
|
||||
&self.ctx().title
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user