feat(api): brought the api into a working state

This commit is contained in:
antifallobst 2023-08-12 12:38:11 +02:00
parent a726004a6c
commit 25934dff07
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
3 changed files with 65 additions and 27 deletions

View File

@ -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<Result<()>>)>,
}
#[post("/authenticate")] #[post("/authenticate")]
async fn hello() -> impl Responder { async fn authenticate(data: web::Data<ApiState>) -> impl Responder {
HttpResponse::Ok().body("Hello world!") 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")] #[post("/register")]
async fn echo(req_body: String) -> impl Responder { async fn register(data: web::Data<ApiState>) -> impl Responder {
HttpResponse::Ok().body(req_body) 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] pub async fn start_worker(
async fn main() -> std::io::Result<()> { port: u16,
HttpServer::new(|| App::new().service(hello).service(echo)) tx: mpsc::Sender<(Call, oneshot::Sender<Result<()>>)>,
.bind(("127.0.0.1", 8080))? ) -> Result<()> {
.run() tokio::task::spawn(
.await 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(())
} }

View File

@ -1,30 +1,29 @@
use tokio::sync::mpsc; use anyhow::{anyhow, Result};
use anyhow::Result; use tokio::sync::{mpsc, oneshot};
#[derive(Debug)] #[derive(Debug)]
pub enum RawCall { pub enum Call {
Register, Register,
Authenticate, Authenticate,
Delete, Delete,
} }
#[derive(Debug)] async fn worker(mut rx: mpsc::Receiver<(Call, oneshot::Sender<Result<()>>)>) -> Result<()> {
pub struct Call {
call: RawCall,
tx: mpsc::Sender<Result<()>>,
}
async fn worker(mut rx: mpsc::Receiver<Call>) {
loop { loop {
let call = rx.recv().await.unwrap(); if let Some((call, tx)) = rx.recv().await {
println!("{:#?}", call);
println!("Received: {:#?}", call); tx.send(Err(anyhow::Error::msg("test"))).unwrap();
};
} }
} }
pub async fn start_worker() -> Result<mpsc::Sender<Call>> { pub async fn start_worker() -> Result<mpsc::Sender<(Call, oneshot::Sender<Result<()>>)>> {
let (tx, rx) = mpsc::channel(128); 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) Ok(tx)
} }

View File

@ -6,6 +6,10 @@ use anyhow::Result;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
println!("Starting BaseAuth server v0.1"); println!("Starting BaseAuth server v0.1");
let tx = call::start_worker().await?; let tx = call::start_worker().await?;
api::start_worker(8080, tx).await?;
loop {}
Ok(()) Ok(())
} }