feat(server): server state logic
Build Docker Image / build-nightly (push) Successful in 1m26s Details

This commit is contained in:
subcrip 2024-04-13 18:13:56 +08:00
parent 74bca308d1
commit 6039127365
Signed by: subcrip
SSH Key Fingerprint: SHA256:dFPFi68d8C87YkFkEBU4TkcrYRySWpekRR1hbnDWUCw
5 changed files with 72 additions and 23 deletions

View File

@ -1,4 +1,6 @@
use uuid::Uuid; use uuid::Uuid;
use crate::types;
use crate::status::Status;
/// Wrapper struct for Postgres database with APIs related to OAuth database operations. /// Wrapper struct for Postgres database with APIs related to OAuth database operations.
pub struct OAuthDatabase { pub struct OAuthDatabase {
@ -54,12 +56,25 @@ impl OAuthDatabase {
/// Register an application. /// Register an application.
/// Applicant: client /// Applicant: client
pub async fn trusted_register_application(&mut self, name: &String, description: &String) -> Result<crate::types::oauth_application::OAuthApplication, tokio_postgres::Error> { pub async fn trusted_register_application(&mut self, name: &String, description: &String) -> Result<(Status, types::oauth_application::OAuthApplication), tokio_postgres::Error> {
// TEST: functionality // TEST: functionality
let app = crate::types::oauth_application::OAuthApplication::create(name, description); let app = crate::types::oauth_application::OAuthApplication::create(name, description);
let app_hash = crate::types::oauth_application::OAuthHashedApplication::from(&app); 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?; 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<Status, tokio_postgres::Error> {
// 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. /// Register a resource.

View File

@ -7,6 +7,7 @@ mod db;
mod types; mod types;
mod misc; mod misc;
mod constants; mod constants;
mod status;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -17,12 +18,8 @@ async fn main() {
Ok(()) => tide::log::info!("Finished initializing."), Ok(()) => tide::log::info!("Finished initializing."),
Err(e) => tide::log::warn!("{}", e), Err(e) => tide::log::warn!("{}", e),
} }
break;
}
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
}
let serve = async { let serve = async {
match server::OAuthServer::serve(&format!("{}:{}", constants::HOST.as_str(), constants::PORT.as_str()).into()).await { match server::OAuthServer::serve(b, &format!("{}:{}", constants::HOST.as_str(), constants::PORT.as_str()).into()).await {
Ok(_) => (), Ok(_) => (),
Err(e) => tide::log::error!("{}", e), Err(e) => tide::log::error!("{}", e),
} }
@ -34,6 +31,10 @@ async fn main() {
}, },
Err(e) => tide::log::error!("{}", e) Err(e) => tide::log::error!("{}", e)
} }
break;
}
tokio::time::sleep(tokio::time::Duration::from_millis(1000)).await;
}
} }
#[tokio::test] #[tokio::test]

View File

@ -1,3 +1,15 @@
pub async fn hello(req: tide::Request<()>) -> tide::Result { 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()) Ok("Hello".into())
} }
}

View File

@ -1,14 +1,29 @@
use tide::Request;
use tide::prelude::*; use tide::prelude::*;
use std::sync::{Arc, Mutex};
use super::api; type Request = tide::Request<State>;
type BoundApi = super::api::BoundApi;
#[derive(Clone)]
struct State {
value: Arc<Mutex<BoundApi>>
}
impl State {
fn new(api: BoundApi) -> Self {
Self { value: Arc::new(Mutex::new(api)) }
}
}
pub struct OAuthServer; pub struct OAuthServer;
impl OAuthServer { impl OAuthServer {
pub async fn serve(bind_addr: &String) -> async_std::io::Result<()> { pub async fn serve(db: crate::db::OAuthDatabase, bind_addr: &String) -> async_std::io::Result<()> {
let mut server = tide::new(); let mut server = tide::with_state(State::new(BoundApi::new(db)));
server.at("/hello").get(api::hello); server.at("/hello").get(|req: Request| async move {
let api = req.state().value.lock().unwrap();
api.hello()
});
server.listen(bind_addr).await server.listen(bind_addr).await
} }
} }

6
src/status.rs Normal file
View File

@ -0,0 +1,6 @@
pub enum Status {
Success,
Connection,
Execution,
NotFound,
}