diff --git a/src/api.rs b/src/api.rs index 9426e49..2dc9644 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,19 +1,54 @@ -use actix_web::{post, App, HttpResponse, HttpServer, Responder}; +use actix_web::{post, web, App, HttpResponse, HttpServer, Responder}; +use anyhow::Result; +use tokio::sync::{mpsc, oneshot}; + +use crate::call::Call; + +struct ApiState { + tx: mpsc::Sender<(Call, oneshot::Sender>)>, +} #[post("/authenticate")] -async fn hello() -> impl Responder { - HttpResponse::Ok().body("Hello world!") +async fn authenticate(data: web::Data) -> impl Responder { + let (tx, rx) = oneshot::channel(); + + if let Err(_) = data.tx.send((Call::Authenticate, tx)).await { + return HttpResponse::InternalServerError().finish(); + } + + match rx.await.unwrap() { + Ok(_) => HttpResponse::Ok().finish(), + Err(_) => HttpResponse::Unauthorized().finish(), + } } #[post("/register")] -async fn echo(req_body: String) -> impl Responder { - HttpResponse::Ok().body(req_body) +async fn register(data: web::Data) -> impl Responder { + let (tx, rx) = oneshot::channel(); + + if let Err(_) = data.tx.send((Call::Register, tx)).await { + return HttpResponse::InternalServerError().finish(); + } + + match rx.await.unwrap() { + Ok(_) => HttpResponse::Ok().finish(), + Err(_) => HttpResponse::Unauthorized().finish(), + } } -#[actix_web::main] -async fn main() -> std::io::Result<()> { - HttpServer::new(|| App::new().service(hello).service(echo)) - .bind(("127.0.0.1", 8080))? - .run() - .await +pub async fn start_worker( + port: u16, + tx: mpsc::Sender<(Call, oneshot::Sender>)>, +) -> Result<()> { + tokio::task::spawn( + HttpServer::new(move || { + App::new() + .service(authenticate) + .service(register) + .app_data(web::Data::new(ApiState { tx: tx.clone() })) + }) + .bind(("127.0.0.1", port))? + .run(), + ); + Ok(()) } diff --git a/src/call.rs b/src/call.rs index f9a026c..c629791 100644 --- a/src/call.rs +++ b/src/call.rs @@ -1,30 +1,29 @@ -use tokio::sync::mpsc; -use anyhow::Result; +use anyhow::{anyhow, Result}; +use tokio::sync::{mpsc, oneshot}; #[derive(Debug)] -pub enum RawCall { +pub enum Call { Register, Authenticate, Delete, } -#[derive(Debug)] -pub struct Call { - call: RawCall, - tx: mpsc::Sender>, -} - -async fn worker(mut rx: mpsc::Receiver) { +async fn worker(mut rx: mpsc::Receiver<(Call, oneshot::Sender>)>) -> Result<()> { loop { - let call = rx.recv().await.unwrap(); - - println!("Received: {:#?}", call); + if let Some((call, tx)) = rx.recv().await { + println!("{:#?}", call); + tx.send(Err(anyhow::Error::msg("test"))).unwrap(); + }; } } -pub async fn start_worker() -> Result> { +pub async fn start_worker() -> Result>)>> { let (tx, rx) = mpsc::channel(128); - tokio::task::spawn(worker(rx)); - + tokio::task::spawn(async { + if let Err(e) = worker(rx).await { + panic!("{e}"); + }; + }); + Ok(tx) } diff --git a/src/main.rs b/src/main.rs index 00a64ff..c77fd0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,10 @@ use anyhow::Result; #[tokio::main] async fn main() -> Result<()> { println!("Starting BaseAuth server v0.1"); + let tx = call::start_worker().await?; + api::start_worker(8080, tx).await?; + + loop {} Ok(()) }