add small frontend stack w/ tailwind & askama
This commit is contained in:
@@ -4,6 +4,7 @@ use tokio::net::TcpListener;
|
||||
mod discordbot;
|
||||
mod router;
|
||||
mod setup;
|
||||
mod website;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
use axum::{Router, http::StatusCode, routing::get};
|
||||
use chrono::{NaiveDate, Utc};
|
||||
use tower::service_fn;
|
||||
|
||||
use crate::router::redirects::redirects;
|
||||
use crate::{router::redirects::redirects, website::website_service};
|
||||
|
||||
mod redirects;
|
||||
|
||||
pub fn init() -> Router {
|
||||
Router::new().merge(redirects()).nest("/api/", api())
|
||||
Router::new()
|
||||
.merge(redirects())
|
||||
.nest("/api/", api())
|
||||
.fallback_service(service_fn(website_service))
|
||||
}
|
||||
|
||||
fn api() -> Router {
|
||||
|
||||
23
src/website/mod.rs
Normal file
23
src/website/mod.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use std::convert::Infallible;
|
||||
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{Request, StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
|
||||
use crate::website::pages::index::page_index;
|
||||
|
||||
mod pages;
|
||||
|
||||
const STYLES_CSS: &str = include_str!("../../web/styles.css");
|
||||
|
||||
pub async fn website_service(req: Request<Body>) -> Result<Response, Infallible> {
|
||||
let path = req.uri().path().trim_start_matches("/");
|
||||
|
||||
Ok(match path {
|
||||
"" | "index" | "index.html" | "index.htm" => page_index().await,
|
||||
"styles.css" => ([(header::CONTENT_TYPE, "text/css")], STYLES_CSS).into_response(),
|
||||
_ => StatusCode::NOT_FOUND.into_response(),
|
||||
})
|
||||
}
|
||||
19
src/website/pages/index.rs
Normal file
19
src/website/pages/index.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use askama::Template;
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse, Response},
|
||||
};
|
||||
|
||||
use crate::website::pages::INTERNAL_SERVER_ERROR_MSG;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct PageIndex;
|
||||
|
||||
pub async fn page_index() -> Response {
|
||||
let a = PageIndex;
|
||||
match a.render() {
|
||||
Ok(res) => (StatusCode::OK, Html(res)).into_response(),
|
||||
Err(_e) => (StatusCode::INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MSG).into_response(),
|
||||
}
|
||||
}
|
||||
4
src/website/pages/mod.rs
Normal file
4
src/website/pages/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod index;
|
||||
|
||||
const INTERNAL_SERVER_ERROR_MSG: &str =
|
||||
"An internal server error occured while rendering your HTML. Sorry!";
|
||||
Reference in New Issue
Block a user