From 51c8548e5ccaa2dacc425613daf5e52c3490e28e Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Wed, 9 Oct 2024 21:32:47 +0200 Subject: [PATCH] basic testing --- Cargo.toml | 5 +++ src/main.rs | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e96cb1f..de46007 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] +rocket = "0.5.1" +twba-common.workspace = true +lazy_static = "1.5.0" +derive_more = { version = "1.0.0", features = ["full"] } + diff --git a/src/main.rs b/src/main.rs index e7a11a9..4426f85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,105 @@ -fn main() { - println!("Hello, world!"); +use derive_more::{FromStr, FromStrError}; +#[macro_use] +extern crate rocket; + +use rocket::request::FromParam; +use rocket::tokio::time::{sleep, Duration}; +use std::sync::OnceLock; +use twba_common::init_tracing; +use twba_common::prelude::twba_local_db; +use twba_common::prelude::twba_local_db::re_exports::sea_orm; +use twba_common::prelude::twba_local_db::re_exports::sea_orm::DatabaseConnection; +use twba_common::prelude::Conf; + +static CLIENT: OnceLock = OnceLock::new(); +static CONF: OnceLock = OnceLock::new(); + +#[get("/service//info")] +fn service_info(service: AvailableServices) -> String { + format!("Here is some info about the service: name: {service}") +} + +#[get("/delay/")] +async fn delay(seconds: u64) -> String { + sleep(Duration::from_secs(seconds)).await; + format!("Waited for {} seconds", seconds) +} +#[get("/")] +fn index() -> &'static str { + trace!("Called Index"); + "Hello, world!" +} +fn get_config<'a>() -> &'a Conf { + CONF.get_or_init(twba_common::get_config) +} +async fn get_client<'a>() -> Result<&'a DatabaseConnection, MainError> { + match CLIENT.get() { + Some(client) => Ok(client), + None => { + CLIENT + .set(get_new_client().await?) + .expect("Failed to set client after failing to get client"); + Ok(CLIENT.get().expect("we just initialized the client")) + } + } +} +async fn get_new_client<'a>() -> Result { + Ok(twba_local_db::open_database(Some(&get_config().db_url)).await?) +} +#[rocket::main] +async fn main() -> Result<(), MainError> { + let _guard = init_tracing("twba_uploader"); + info!("Hello world!"); + let _rocket = rocket::build() + .mount("/", routes![index, delay, service_info]) + .launch() + .await?; + + Ok(()) +} +#[derive(Debug, derive_more::Error, derive_more::Display, derive_more::From)] +pub enum MainError { + Rocket(#[from] rocket::Error), + Db(#[from] sea_orm::DbErr), + #[from(ignore)] + SetStatics { + problem_static: Statics, + }, + #[from(ignore)] + MissingStatic { + problem_static: Statics, + }, + Other { + reason: String, + }, +} +#[derive(Debug, derive_more::Display, derive_more::From, Clone, Copy)] +pub enum Statics { + DbClient, + Config, +} + +#[derive( + Debug, + derive_more::Display, + derive_more::From, + derive_more::FromStr, + Clone, + Copy, + Ord, + PartialOrd, + Eq, + PartialEq, + FromFormField, +)] +enum AvailableServices { + Uploader, + Downloader, + Splitter, +} +impl<'a> FromParam<'a> for AvailableServices { + type Error = FromStrError; + fn from_param(param: &'a str) -> Result { + AvailableServices::from_str(param) + } }