Move server code into its own module

This commit is contained in:
Joscha 2023-08-07 14:23:47 +02:00
parent ad0c1a69cb
commit 45abda2b6d
12 changed files with 15 additions and 4 deletions

18
src/server/web/commit.rs Normal file
View file

@ -0,0 +1,18 @@
use askama::Template;
use axum::{extract::State, response::IntoResponse};
use crate::{config::Config, somehow};
use super::{Base, Tab};
#[derive(Template)]
#[template(path = "commit.html")]
struct CommitTemplate {
base: Base,
}
pub async fn get(State(config): State<&'static Config>) -> somehow::Result<impl IntoResponse> {
Ok(CommitTemplate {
base: Base::new(config, Tab::Commit),
})
}

View file

@ -0,0 +1,114 @@
use askama::Template;
use axum::{
extract::{Path, State},
http::StatusCode,
response::{IntoResponse, Response},
};
use futures::TryStreamExt;
use sqlx::SqlitePool;
use crate::{config::Config, somehow, util};
use super::{Base, Tab};
struct Commit {
hash: String,
short: String,
reachable: i64,
}
impl Commit {
fn new(hash: String, message: &str, reachable: i64) -> Self {
Self {
short: util::format_commit_short(&hash, message),
hash,
reachable,
}
}
}
#[derive(Template)]
#[template(path = "commit_hash.html")]
struct CommitHashTemplate {
base: Base,
hash: String,
author: String,
author_date: String,
commit: String,
commit_date: String,
parents: Vec<Commit>,
children: Vec<Commit>,
summary: String,
message: String,
reachable: i64,
}
pub async fn get(
Path(hash): Path<String>,
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
) -> somehow::Result<Response> {
let Some(commit) = sqlx::query!(
"\
SELECT \
hash, \
author, \
author_date AS \"author_date: time::OffsetDateTime\", \
committer, \
committer_date AS \"committer_date: time::OffsetDateTime\", \
message, \
reachable \
FROM commits \
WHERE hash = ? \
",
hash
)
.fetch_optional(&db)
.await?
else {
return Ok(StatusCode::NOT_FOUND.into_response());
};
let parents = sqlx::query!(
"\
SELECT hash, message, reachable FROM commits \
JOIN commit_links ON hash = parent \
WHERE child = ? \
ORDER BY reachable DESC, unixepoch(committer_date) ASC \
",
hash
)
.fetch(&db)
.map_ok(|r| Commit::new(r.hash, &r.message, r.reachable))
.try_collect::<Vec<_>>()
.await?;
let children = sqlx::query!(
"\
SELECT hash, message, reachable FROM commits \
JOIN commit_links ON hash = child \
WHERE parent = ? \
ORDER BY reachable DESC, unixepoch(committer_date) ASC \
",
hash
)
.fetch(&db)
.map_ok(|r| Commit::new(r.hash, &r.message, r.reachable))
.try_collect::<Vec<_>>()
.await?;
Ok(CommitHashTemplate {
base: Base::new(config, Tab::Commit),
hash: commit.hash,
author: commit.author,
author_date: util::format_time(commit.author_date),
commit: commit.committer,
commit_date: util::format_time(commit.committer_date),
parents,
children,
summary: util::format_commit_summary(&commit.message),
message: commit.message.trim_end().to_string(),
reachable: commit.reachable,
}
.into_response())
}

64
src/server/web/index.rs Normal file
View file

@ -0,0 +1,64 @@
use askama::Template;
use axum::{extract::State, response::IntoResponse};
use futures::TryStreamExt;
use sqlx::SqlitePool;
use crate::{config::Config, somehow, util};
use super::{Base, Tab};
struct Ref {
name: String,
hash: String,
short: String,
reachable: i64,
tracked: bool,
}
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {
base: Base,
tracked_refs: Vec<Ref>,
untracked_refs: Vec<Ref>,
}
pub async fn get(
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
) -> somehow::Result<impl IntoResponse> {
let refs = sqlx::query!(
"\
SELECT name, hash, message, reachable, tracked \
FROM refs \
JOIN commits USING (hash) \
ORDER BY name ASC \
"
)
.fetch(&db)
.map_ok(|r| Ref {
short: util::format_commit_short(&r.hash, &r.message),
name: r.name,
hash: r.hash,
reachable: r.reachable,
tracked: r.tracked != 0,
})
.try_collect::<Vec<_>>()
.await?;
let mut tracked_refs = vec![];
let mut untracked_refs = vec![];
for reference in refs {
if reference.tracked {
tracked_refs.push(reference);
} else {
untracked_refs.push(reference);
}
}
Ok(IndexTemplate {
base: Base::new(config, Tab::Index),
tracked_refs,
untracked_refs,
})
}

85
src/server/web/queue.rs Normal file
View file

@ -0,0 +1,85 @@
use askama::Template;
use axum::{extract::State, response::IntoResponse};
use futures::TryStreamExt;
use sqlx::SqlitePool;
use crate::{config::Config, somehow, util};
use super::{Base, Tab};
struct Task {
id: String,
short: String,
reachable: i64,
since: String,
priority: i64,
odd: bool,
}
async fn get_queue(db: &SqlitePool) -> somehow::Result<Vec<Task>> {
let mut tasks = sqlx::query!(
"\
SELECT \
id, \
hash, \
message, \
reachable, \
date AS \"date: time::OffsetDateTime\", \
priority \
FROM queue \
JOIN commits USING (hash) \
ORDER BY priority DESC, unixepoch(date) DESC, hash ASC \
"
)
.fetch(db)
.map_ok(|r| Task {
id: r.id,
short: util::format_commit_short(&r.hash, &r.message),
reachable: r.reachable,
since: util::format_delta_from_now(r.date),
priority: r.priority,
odd: false,
})
.try_collect::<Vec<_>>()
.await?;
let mut last_priority = None;
let mut odd = false;
for task in tasks.iter_mut().rev() {
if last_priority.is_some() && last_priority != Some(task.priority) {
odd = !odd;
}
task.odd = odd;
last_priority = Some(task.priority);
}
Ok(tasks)
}
#[derive(Template)]
#[template(path = "queue_table.html")]
struct QueueTableTemplate {
tasks: Vec<Task>,
}
pub async fn get_table(State(db): State<SqlitePool>) -> somehow::Result<impl IntoResponse> {
let tasks = get_queue(&db).await?;
Ok(QueueTableTemplate { tasks })
}
#[derive(Template)]
#[template(path = "queue.html")]
struct QueueTemplate {
base: Base,
table: QueueTableTemplate,
}
pub async fn get(
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
) -> somehow::Result<impl IntoResponse> {
let tasks = get_queue(&db).await?;
Ok(QueueTemplate {
base: Base::new(config, Tab::Queue),
table: QueueTableTemplate { tasks },
})
}

View file

@ -0,0 +1,65 @@
use askama::Template;
use axum::{
extract::{Path, State},
http::StatusCode,
response::{IntoResponse, Response},
};
use sqlx::SqlitePool;
use crate::{config::Config, somehow, util};
use super::{Base, Tab};
#[derive(Template)]
#[template(path = "queue_id.html")]
struct QueueIdTemplate {
base: Base,
// Task
id: String,
hash: String,
date: String,
priority: i64,
// Commit
summary: String,
short: String,
reachable: i64,
}
pub async fn get(
Path(id): Path<String>,
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
) -> somehow::Result<Response> {
let Some(task) = sqlx::query!(
"\
SELECT \
id, \
hash, \
date AS \"date: time::OffsetDateTime\", \
priority, \
message, \
reachable \
FROM queue \
JOIN commits USING (hash) \
WHERE id = ? \
",
id
)
.fetch_optional(&db)
.await?
else {
return Ok(StatusCode::NOT_FOUND.into_response());
};
Ok(QueueIdTemplate {
base: Base::new(config, Tab::Queue),
date: util::format_time(task.date),
id: task.id,
priority: task.priority,
summary: util::format_commit_summary(&task.message),
short: util::format_commit_short(&task.hash, &task.message),
hash: task.hash,
reachable: task.reachable,
}
.into_response())
}

34
src/server/web/static.rs Normal file
View file

@ -0,0 +1,34 @@
//! Static files embedded in the binary.
use axum::{
http::{header, StatusCode, Uri},
response::IntoResponse,
};
use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[folder = "$OUT_DIR/static"]
pub struct StaticFiles;
pub struct StaticFile<T>(T);
impl<T> IntoResponse for StaticFile<T>
where
T: AsRef<str>,
{
fn into_response(self) -> axum::response::Response {
let path = self.0.as_ref();
match StaticFiles::get(path) {
Some(file) => {
let mime = mime_guess::from_path(path).first_or_octet_stream();
([(header::CONTENT_TYPE, mime.as_ref())], file.data).into_response()
}
None => StatusCode::NOT_FOUND.into_response(),
}
}
}
pub async fn static_handler(uri: Uri) -> impl IntoResponse {
let path = uri.path().trim_start_matches('/').to_string();
StaticFile(path)
}