Use typed paths in worker

This commit is contained in:
Joscha 2023-08-13 16:13:59 +02:00
parent 88d9a1f818
commit 3d5a277234
5 changed files with 25 additions and 14 deletions

View file

@ -195,10 +195,11 @@ impl ConfigFile {
.servers .servers
.iter() .iter()
.map(|(name, server)| { .map(|(name, server)| {
let mut url = server.url.clone(); let url = server
if !url.ends_with('/') { .url
url.push('/'); .strip_suffix('/')
} .unwrap_or(&server.url)
.to_string();
let token = server.token.to_string(); let token = server.token.to_string();
(name.to_string(), WorkerServerConfig { url, token }) (name.to_string(), WorkerServerConfig { url, token })
}) })
@ -209,7 +210,7 @@ impl ConfigFile {
// TODO Url functions // TODO Url functions
#[derive(Clone)] #[derive(Clone)]
pub struct WorkerServerConfig { pub struct WorkerServerConfig {
/// Always ends with a `/`. /// Never ends with a `/`.
pub url: String, pub url: String,
pub token: String, pub token: String,
} }

View file

@ -1,6 +1,6 @@
mod recurring; mod recurring;
mod util; mod util;
mod web; pub mod web;
mod workers; mod workers;
use std::{ use std::{

View file

@ -1,5 +1,5 @@
mod admin; mod admin;
mod api; pub mod api;
mod commit; mod commit;
mod index; mod index;
mod link; mod link;

View file

@ -202,7 +202,7 @@ fn stream_response(repo: Arc<ThreadSafeRepository>, id: ObjectId) -> impl IntoRe
#[derive(Deserialize, TypedPath)] #[derive(Deserialize, TypedPath)]
#[typed_path("/api/worker/repo/:hash/tree.tar.gz")] #[typed_path("/api/worker/repo/:hash/tree.tar.gz")]
pub struct PathApiWorkerRepoByHashTreeTarGz { pub struct PathApiWorkerRepoByHashTreeTarGz {
hash: String, pub hash: String,
} }
pub async fn get_api_worker_repo_by_hash_tree_tar_gz( pub async fn get_api_worker_repo_by_hash_tree_tar_gz(
@ -227,7 +227,7 @@ pub async fn get_api_worker_repo_by_hash_tree_tar_gz(
#[derive(Deserialize, TypedPath)] #[derive(Deserialize, TypedPath)]
#[typed_path("/api/worker/bench_repo/:hash/tree.tar.gz")] #[typed_path("/api/worker/bench_repo/:hash/tree.tar.gz")]
pub struct PathApiWorkerBenchRepoByHashTreeTarGz { pub struct PathApiWorkerBenchRepoByHashTreeTarGz {
hash: String, pub hash: String,
} }
pub async fn get_api_worker_bench_repo_by_hash_tree_tar_gz( pub async fn get_api_worker_bench_repo_by_hash_tree_tar_gz(

View file

@ -7,6 +7,10 @@ use tracing::{debug, warn};
use crate::{ use crate::{
config::{Config, WorkerServerConfig}, config::{Config, WorkerServerConfig},
server::web::api::worker::{
PathApiWorkerBenchRepoByHashTreeTarGz, PathApiWorkerRepoByHashTreeTarGz,
PathApiWorkerStatus,
},
shared::{FinishedRun, ServerResponse, WorkerRequest, WorkerStatus}, shared::{FinishedRun, ServerResponse, WorkerRequest, WorkerStatus},
somehow, somehow,
worker::tree, worker::tree,
@ -47,7 +51,7 @@ impl Server {
request_run: bool, request_run: bool,
submit_run: Option<FinishedRun>, submit_run: Option<FinishedRun>,
) -> somehow::Result<ServerResponse> { ) -> somehow::Result<ServerResponse> {
let url = format!("{}api/worker/status", self.server_config.url); let url = format!("{}{}", self.server_config.url, PathApiWorkerStatus {});
let status = match &*self.current_run.lock().unwrap() { let status = match &*self.current_run.lock().unwrap() {
Some(run) if run.is_for_server(&self.name) => { Some(run) if run.is_for_server(&self.name) => {
@ -80,8 +84,11 @@ impl Server {
pub async fn download_repo(&self, hash: &str) -> somehow::Result<TempDir> { pub async fn download_repo(&self, hash: &str) -> somehow::Result<TempDir> {
let url = format!( let url = format!(
"{}api/worker/repo/{hash}/tree.tar.gz", "{}{}",
self.server_config.url self.server_config.url,
PathApiWorkerRepoByHashTreeTarGz {
hash: hash.to_string()
},
); );
let response = self let response = self
@ -96,8 +103,11 @@ impl Server {
pub async fn download_bench_repo(&self, hash: &str) -> somehow::Result<TempDir> { pub async fn download_bench_repo(&self, hash: &str) -> somehow::Result<TempDir> {
let url = format!( let url = format!(
"{}api/worker/bench_repo/{hash}/tree.tar.gz", "{}{}",
self.server_config.url self.server_config.url,
PathApiWorkerBenchRepoByHashTreeTarGz {
hash: hash.to_string()
},
); );
let response = self let response = self