Add "Enqueue" button to commits

This commit is contained in:
Joscha 2023-08-11 16:50:32 +02:00
parent 5e0b8e3c8c
commit 48693d3f1c
4 changed files with 41 additions and 3 deletions

View file

@ -6,7 +6,10 @@ mod queue;
mod r#static;
mod worker;
use axum::{routing::get, Router};
use axum::{
routing::{get, post},
Router,
};
use crate::{config::Config, somehow};
@ -46,9 +49,10 @@ pub async fn run(server: Server) -> somehow::Result<()> {
let app = Router::new()
.route("/", get(index::get))
.route("/commit/:hash", get(commit::get))
.route("/worker/:name", get(worker::get))
.route("/commit/:hash/enqueue", post(commit::post_enqueue))
.route("/queue/", get(queue::get))
.route("/queue/inner", get(queue::get_inner))
.route("/worker/:name", get(worker::get))
.merge(api::router(&server))
.fallback(get(r#static::static_handler))
.with_state(server.clone());

View file

@ -2,10 +2,11 @@ use askama::Template;
use axum::{
extract::{Path, State},
http::StatusCode,
response::{IntoResponse, Response},
response::{IntoResponse, Redirect, Response},
};
use futures::TryStreamExt;
use sqlx::SqlitePool;
use time::OffsetDateTime;
use crate::{config::Config, server::util, somehow};
@ -98,3 +99,20 @@ pub async fn get(
}
.into_response())
}
pub async fn post_enqueue(
Path(hash): Path<String>,
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
) -> somehow::Result<impl IntoResponse> {
let date = OffsetDateTime::now_utc();
sqlx::query!(
"INSERT OR IGNORE INTO queue (hash, date, priority) VALUES (?, ?, 1)",
hash,
date,
)
.execute(&db)
.await?;
Ok(Redirect::to(&format!("{}queue/", config.web_base)))
}