Set up base template

This commit is contained in:
Joscha 2023-08-05 14:22:31 +02:00
parent feb73c96c4
commit e17483b4d6
6 changed files with 73 additions and 44 deletions

View file

@ -8,13 +8,23 @@ use tracing::{debug, info};
mod default {
use std::time::Duration;
pub fn repo_name() -> String {
"Local repo".to_string()
}
pub fn repo_update_delay() -> Duration {
Duration::from_secs(60)
}
pub fn web_base() -> String {
"".to_string()
}
}
#[derive(Debug, Deserialize)]
pub struct Repo {
#[serde(default = "default::repo_name")]
pub name: String,
#[serde(default = "default::repo_update_delay", with = "humantime_serde")]
pub update_delay: Duration,
}
@ -22,14 +32,45 @@ pub struct Repo {
impl Default for Repo {
fn default() -> Self {
Self {
name: default::repo_name(),
update_delay: default::repo_update_delay(),
}
}
}
impl Repo {
pub fn name(&self) -> String {
self.name.clone()
}
}
#[derive(Debug, Deserialize)]
pub struct Web {
#[serde(default = "default::web_base")]
pub base: String,
}
impl Default for Web {
fn default() -> Self {
Self {
base: default::web_base(),
}
}
}
impl Web {
pub fn base(&self) -> String {
self.base
.strip_suffix('/')
.unwrap_or(&self.base)
.to_string()
}
}
#[derive(Debug, Default, Deserialize)]
pub struct Config {
pub repo: Repo,
pub web: Web,
}
impl Config {

View file

@ -1,18 +1,18 @@
use askama::Template;
use axum::{extract::State, response::IntoResponse};
use sqlx::SqlitePool;
use crate::config::Config;
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {
number: i32,
base: String,
repo_name: String,
}
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 })
pub async fn get(State(config): State<&'static Config>) -> super::Result<impl IntoResponse> {
Ok(IndexTemplate {
base: config.web.base(),
repo_name: config.repo.name(),
})
}