Move /commit/:hash/enqueue to /admin/queue/add

This commit is contained in:
Joscha 2023-08-13 15:33:11 +02:00
parent 08e240d7db
commit 087ecfd783
9 changed files with 113 additions and 40 deletions

View file

@ -1,12 +0,0 @@
{
"db_name": "SQLite",
"query": "INSERT OR IGNORE INTO queue (hash, date, priority) VALUES (?, ?, 1)",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "091ccc5f03da1f11d8efafe1a0082e62f7973dc2d35835693dd5bdd696759de0"
}

View file

@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT INTO queue (hash, date, priority) VALUES (?, ?, ?) ON CONFLICT (hash) DO UPDATE SET priority = excluded.priority WHERE priority < excluded.priority ",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "aaf84d58ad3670609cf7cb28243a242829f0b5a65e39e98070664533621bc4c2"
}

39
Cargo.lock generated
View file

@ -234,6 +234,31 @@ dependencies = [
"tower-service", "tower-service",
] ]
[[package]]
name = "axum-extra"
version = "0.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a93e433be9382c737320af3924f7d5fc6f89c155cf2bf88949d8f5126fab283f"
dependencies = [
"axum",
"axum-core",
"axum-macros",
"bytes",
"form_urlencoded",
"futures-util",
"http",
"http-body",
"mime",
"percent-encoding",
"pin-project-lite",
"serde",
"serde_html_form",
"tokio",
"tower",
"tower-layer",
"tower-service",
]
[[package]] [[package]]
name = "axum-macros" name = "axum-macros"
version = "0.3.8" version = "0.3.8"
@ -2678,6 +2703,19 @@ dependencies = [
"syn 2.0.28", "syn 2.0.28",
] ]
[[package]]
name = "serde_html_form"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cde65b75f2603066b78d6fa239b2c07b43e06ead09435f60554d3912962b4a3c"
dependencies = [
"form_urlencoded",
"indexmap 2.0.0",
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.104" version = "1.0.104"
@ -3130,6 +3168,7 @@ dependencies = [
"askama", "askama",
"askama_axum", "askama_axum",
"axum", "axum",
"axum-extra",
"clap", "clap",
"directories", "directories",
"flate2", "flate2",

View file

@ -8,6 +8,7 @@ anyhow = "1.0.72"
askama = { version = "0.12.0", features = ["with-axum"] } askama = { version = "0.12.0", features = ["with-axum"] }
askama_axum = "0.3.0" askama_axum = "0.3.0"
axum = { version = "0.6.19", features = ["macros", "headers"] } axum = { version = "0.6.19", features = ["macros", "headers"] }
axum-extra = { version = "0.7.7", features = ["typed-routing"] }
clap = { version = "4.3.19", features = ["derive", "deprecated"] } clap = { version = "4.3.19", features = ["derive", "deprecated"] }
directories = "5.0.1" directories = "5.0.1"
flate2 = "1.0.26" flate2 = "1.0.26"

View file

@ -1,3 +1,4 @@
mod admin;
mod api; mod api;
mod commit; mod commit;
mod index; mod index;
@ -6,13 +7,13 @@ mod queue;
mod r#static; mod r#static;
mod worker; mod worker;
use axum::{ use axum::{routing::get, Router};
routing::{get, post}, use axum_extra::routing::RouterExt;
Router,
};
use crate::{config::Config, somehow}; use crate::{config::Config, somehow};
use self::admin::queue::post_admin_queue_add;
use super::Server; use super::Server;
pub enum Tab { pub enum Tab {
@ -49,10 +50,10 @@ pub async fn run(server: Server) -> somehow::Result<()> {
let app = Router::new() let app = Router::new()
.route("/", get(index::get)) .route("/", get(index::get))
.route("/commit/:hash", get(commit::get)) .route("/commit/:hash", get(commit::get))
.route("/commit/:hash/enqueue", post(commit::post_enqueue))
.route("/queue/", get(queue::get)) .route("/queue/", get(queue::get))
.route("/queue/inner", get(queue::get_inner)) .route("/queue/inner", get(queue::get_inner))
.route("/worker/:name", get(worker::get)) .route("/worker/:name", get(worker::get))
.typed_post(post_admin_queue_add)
.merge(api::router(&server)) .merge(api::router(&server))
.fallback(get(r#static::static_handler)) .fallback(get(r#static::static_handler))
.with_state(server.clone()); .with_state(server.clone());

1
src/server/web/admin.rs Normal file
View file

@ -0,0 +1 @@
pub mod queue;

View file

@ -0,0 +1,46 @@
use axum::{
extract::State,
response::{IntoResponse, Redirect},
Form,
};
use axum_extra::routing::TypedPath;
use serde::Deserialize;
use sqlx::SqlitePool;
use time::OffsetDateTime;
use crate::{config::Config, somehow};
#[derive(Deserialize, TypedPath)]
#[typed_path("/admin/queue/add")]
pub struct PathAdminQueueAdd {}
#[derive(Deserialize)]
pub struct FormAdminQueueAdd {
hash: String,
#[serde(default)]
priority: i32,
}
pub async fn post_admin_queue_add(
_path: PathAdminQueueAdd,
State(config): State<&'static Config>,
State(db): State<SqlitePool>,
Form(form): Form<FormAdminQueueAdd>,
) -> somehow::Result<impl IntoResponse> {
let date = OffsetDateTime::now_utc();
sqlx::query!(
"\
INSERT INTO queue (hash, date, priority) VALUES (?, ?, ?) \
ON CONFLICT (hash) DO UPDATE \
SET priority = excluded.priority WHERE priority < excluded.priority \
",
form.hash,
date,
form.priority,
)
.execute(&db)
.await?;
// TODO Replace with typed link
Ok(Redirect::to(&format!("{}queue/", config.web_base)))
}

View file

@ -2,15 +2,14 @@ use askama::Template;
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, State},
http::StatusCode, http::StatusCode,
response::{IntoResponse, Redirect, Response}, response::{IntoResponse, Response},
}; };
use futures::TryStreamExt; use futures::TryStreamExt;
use sqlx::SqlitePool; use sqlx::SqlitePool;
use time::OffsetDateTime;
use crate::{config::Config, server::util, somehow}; use crate::{config::Config, server::util, somehow};
use super::{link::CommitLink, Base, Tab}; use super::{admin::queue::PathAdminQueueAdd, link::CommitLink, Base, Tab};
#[derive(Template)] #[derive(Template)]
#[template(path = "commit.html")] #[template(path = "commit.html")]
@ -26,6 +25,7 @@ struct CommitTemplate {
summary: String, summary: String,
message: String, message: String,
reachable: i64, reachable: i64,
link_admin_queue_add: PathAdminQueueAdd,
} }
pub async fn get( pub async fn get(
@ -96,24 +96,7 @@ pub async fn get(
summary: util::format_commit_summary(&commit.message), summary: util::format_commit_summary(&commit.message),
message: commit.message.trim_end().to_string(), message: commit.message.trim_end().to_string(),
reachable: commit.reachable, reachable: commit.reachable,
link_admin_queue_add: PathAdminQueueAdd {},
} }
.into_response()) .into_response())
} }
// TODO Move to /admin/queue/add
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)))
}

View file

@ -34,7 +34,9 @@
title="{% call util::commit_title(reachable) %}">{{ message }}</pre> title="{% call util::commit_title(reachable) %}">{{ message }}</pre>
</div> </div>
<form method="post" action="{{ base.root }}commit/{{ hash }}/enqueue"> <form method="post" action="{{ base.root }}{{ link_admin_queue_add }}">
<button>Enqueue</button> <input type="hidden" name="hash" value="{{ hash }}">
<label>Priority: <input type="number" name="priority" value="0" min="-2147483648" max="2147483647"></label>
<button>Add to queue</button>
</form> </form>
{% endblock %} {% endblock %}