feat(server): server state logic
Build Docker Image / build-nightly (push) Successful in 1m26s
Details
Build Docker Image / build-nightly (push) Successful in 1m26s
Details
This commit is contained in:
parent
74bca308d1
commit
6039127365
19
src/db.rs
19
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<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
|
||||
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<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.
|
||||
|
|
27
src/main.rs
27
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]
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,29 @@
|
|||
use tide::Request;
|
||||
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;
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
pub enum Status {
|
||||
Success,
|
||||
Connection,
|
||||
Execution,
|
||||
NotFound,
|
||||
}
|
Loading…
Reference in New Issue