From 603912736513630a1355cf990095d79bcfea6747 Mon Sep 17 00:00:00 2001 From: subcrip Date: Sat, 13 Apr 2024 18:13:56 +0800 Subject: [PATCH] feat(server): server state logic --- src/db.rs | 19 +++++++++++++++++-- src/main.rs | 27 ++++++++++++++------------- src/server/api.rs | 16 ++++++++++++++-- src/server/server.rs | 27 +++++++++++++++++++++------ src/status.rs | 6 ++++++ 5 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 src/status.rs diff --git a/src/db.rs b/src/db.rs index aa8ae60..814163c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,4 +1,6 @@ use uuid::Uuid; +use crate::types; +use crate::status::Status; /// Wrapper struct for Postgres database with APIs related to OAuth database operations. pub struct OAuthDatabase { @@ -54,12 +56,25 @@ impl OAuthDatabase { /// Register an application. /// Applicant: client - pub async fn trusted_register_application(&mut self, name: &String, description: &String) -> Result { + pub async fn trusted_register_application(&mut self, name: &String, description: &String) -> Result<(Status, types::oauth_application::OAuthApplication), tokio_postgres::Error> { // TEST: functionality let app = crate::types::oauth_application::OAuthApplication::create(name, description); let app_hash = crate::types::oauth_application::OAuthHashedApplication::from(&app); self.psql_execute("insert into Applications (client_id, client_secret, client_name, client_desc) values ($1, $2, $3, $4)", &[&app_hash.client_id, &app_hash.client_secret, &app_hash.client_name, &app_hash.client_desc]).await?; - Ok(app) + Ok((Status::Success, app)) + } + + /// Delete an application. + /// Applicant: client + pub async fn trusted_drop_application(&mut self, client_id: &Uuid) -> Result { + // TEST: functionality + let r = self.psql_query("select client_id from Applications where client_id = $1", &[client_id]).await?; + if r.len() == 0 { + Ok(Status::NotFound) + } else { + self.psql_execute("delete from Applications where client_id = $1", &[client_id]).await?; + Ok(Status::Success) + } } /// Register a resource. diff --git a/src/main.rs b/src/main.rs index e9e2ec5..8f57ff3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod db; mod types; mod misc; mod constants; +mod status; #[tokio::main] async fn main() { @@ -17,23 +18,23 @@ async fn main() { Ok(()) => tide::log::info!("Finished initializing."), Err(e) => tide::log::warn!("{}", e), } + let serve = async { + match server::OAuthServer::serve(b, &format!("{}:{}", constants::HOST.as_str(), constants::PORT.as_str()).into()).await { + Ok(_) => (), + Err(e) => tide::log::error!("{}", e), + } + }; + match async_ctrlc::CtrlC::new() { + Ok(ctrlc) => { + ctrlc.race(serve).await; + tide::log::info!("Server stopped."); + }, + Err(e) => tide::log::error!("{}", e) + } break; } tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await; } - let serve = async { - match server::OAuthServer::serve(&format!("{}:{}", constants::HOST.as_str(), constants::PORT.as_str()).into()).await { - Ok(_) => (), - Err(e) => tide::log::error!("{}", e), - } - }; - match async_ctrlc::CtrlC::new() { - Ok(ctrlc) => { - ctrlc.race(serve).await; - tide::log::info!("Server stopped."); - }, - Err(e) => tide::log::error!("{}", e) - } } #[tokio::test] diff --git a/src/server/api.rs b/src/server/api.rs index edd41b4..3774864 100644 --- a/src/server/api.rs +++ b/src/server/api.rs @@ -1,3 +1,15 @@ -pub async fn hello(req: tide::Request<()>) -> tide::Result { - Ok("Hello".into()) +type Request = tide::Request<()>; +type Result = tide::Result; + +pub struct BoundApi { + db: crate::db::OAuthDatabase, +} + +impl BoundApi { + pub fn new(db: crate::db::OAuthDatabase) -> Self { + Self { db } + } + pub fn hello(&self) -> Result { + Ok("Hello".into()) + } } diff --git a/src/server/server.rs b/src/server/server.rs index a87e0c7..88f8512 100644 --- a/src/server/server.rs +++ b/src/server/server.rs @@ -1,14 +1,29 @@ -use tide::Request; use tide::prelude::*; +use std::sync::{Arc, Mutex}; + +type Request = tide::Request; +type BoundApi = super::api::BoundApi; + +#[derive(Clone)] +struct State { + value: Arc> +} + +impl State { + fn new(api: BoundApi) -> Self { + Self { value: Arc::new(Mutex::new(api)) } + } +} -use super::api; - pub struct OAuthServer; impl OAuthServer { - pub async fn serve(bind_addr: &String) -> async_std::io::Result<()> { - let mut server = tide::new(); - server.at("/hello").get(api::hello); + pub async fn serve(db: crate::db::OAuthDatabase, bind_addr: &String) -> async_std::io::Result<()> { + let mut server = tide::with_state(State::new(BoundApi::new(db))); + server.at("/hello").get(|req: Request| async move { + let api = req.state().value.lock().unwrap(); + api.hello() + }); server.listen(bind_addr).await } } diff --git a/src/status.rs b/src/status.rs new file mode 100644 index 0000000..2c94d7f --- /dev/null +++ b/src/status.rs @@ -0,0 +1,6 @@ +pub enum Status { + Success, + Connection, + Execution, + NotFound, +}