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")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
async fn authenticate(data: web::Data<ApiState>) -> 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<ApiState>) -> 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<()>>)>,
) -> 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(())
}

View File

@ -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<Result<()>>,
}
async fn worker(mut rx: mpsc::Receiver<Call>) {
async fn worker(mut rx: mpsc::Receiver<(Call, oneshot::Sender<Result<()>>)>) -> 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<mpsc::Sender<Call>> {
pub async fn start_worker() -> Result<mpsc::Sender<(Call, oneshot::Sender<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)
}

View File

@ -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(())
}