Move web code to web module

This commit is contained in:
Joscha 2023-08-05 12:58:38 +02:00
parent 3bfeb89686
commit 6d93e3bd70
4 changed files with 73 additions and 32 deletions

View file

@ -1,16 +1,12 @@
mod config;
mod recurring;
mod state;
mod r#static;
mod web;
use std::{io, path::PathBuf};
use askama::Template;
use askama_axum::{IntoResponse, Response};
use axum::{extract::State, http::StatusCode, routing::get, Router};
use clap::Parser;
use directories::ProjectDirs;
use sqlx::SqlitePool;
use state::AppState;
use tokio::{select, signal::unix::SignalKind};
use tracing::{debug, info, Level};
@ -93,22 +89,6 @@ async fn wait_for_signal() -> io::Result<()> {
Ok(())
}
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {
number: i32,
}
async fn index(State(db): State<SqlitePool>) -> Result<Response, Response> {
let result = sqlx::query!("SELECT column1 AS number FROM (VALUES (1))")
.fetch_one(&db)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")).into_response())?;
let number = result.number;
Ok(IndexTemplate { number }.into_response())
}
async fn run() -> anyhow::Result<()> {
let args = Args::parse();
@ -118,20 +98,10 @@ async fn run() -> anyhow::Result<()> {
let config = load_config(args.config)?;
let state = AppState::new(config, &args.db, &args.repo).await?;
let app = Router::new()
.route("/", get(index))
.fallback(get(r#static::static_handler))
.with_state(state.clone())
.into_make_service();
// TODO Add text body to body-less status codes
// TODO Add anyhow-like error type for endpoints
let server = axum::Server::bind(&"0.0.0.0:8000".parse().unwrap());
info!("Startup complete, running");
select! {
_ = wait_for_signal() => {},
_ = server.serve(app) => {},
_ = web::run(state.clone()) => {},
_ = recurring::run(state.clone()) => {},
}

53
src/web.rs Normal file
View file

@ -0,0 +1,53 @@
mod index;
mod r#static;
use std::{error, result};
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
routing::get,
Router, Server,
};
use crate::state::AppState;
/// Anyhow-like error that also implements [`IntoResponse`].
pub struct Error(anyhow::Error);
impl<E> From<E> for Error
where
E: error::Error + Send + Sync + 'static,
{
fn from(value: E) -> Self {
Self(anyhow::Error::from(value))
}
}
impl IntoResponse for Error {
fn into_response(self) -> Response {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("500 Internal Server Error\n\n{}", self.0),
)
.into_response()
}
}
/// Anyhow-like result that also implements [`IntoResponse`].
pub type Result<T> = result::Result<T, Error>;
pub async fn run(state: AppState) -> anyhow::Result<()> {
// TODO Add text body to body-less status codes
let app = Router::new()
.route("/", get(index::get))
.fallback(get(r#static::static_handler))
.with_state(state.clone());
Server::bind(&"0.0.0.0:8000".parse().unwrap())
.serve(app.into_make_service())
.await?;
Ok(())
}

18
src/web/index.rs Normal file
View file

@ -0,0 +1,18 @@
use askama::Template;
use axum::{extract::State, response::IntoResponse};
use sqlx::SqlitePool;
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {
number: i32,
}
pub async fn get(State(db): State<SqlitePool>) -> super::Result<impl IntoResponse> {
let result = sqlx::query!("SELECT column1 AS number FROM (VALUES (1))")
.fetch_one(&db)
.await?;
let number = result.number;
Ok(IndexTemplate { number })
}