refactor(api): promoted endpoints module to a directory

This commit is contained in:
antifallobst 2024-03-22 14:57:11 +01:00
parent d9480a8727
commit 4bf8f83ba8
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
4 changed files with 66 additions and 42 deletions

View File

@ -1,41 +0,0 @@
use crate::backend::{error::AccountRegisterError, Backend};
use actix_web::{post, web, HttpResponse, Responder};
use log::error;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize)]
struct AccountRegisterRequest {
token: String,
password: String,
}
#[derive(Debug, Serialize)]
struct AccountRegisterResponse {
uuid: String,
}
#[post("/account/register")]
pub async fn account_register(
backend: web::Data<Backend>,
body: web::Json<AccountRegisterRequest>,
) -> impl Responder {
let body = body.into_inner();
match backend.account_register(body.token, body.password).await {
Err(e) => {
error!("{e}");
HttpResponse::InternalServerError().finish()
}
Ok(res) => match res {
Err(e) => match e {
AccountRegisterError::InvalidToken => HttpResponse::Unauthorized().finish(),
AccountRegisterError::SqlError(e) => {
error!("{e}");
HttpResponse::InternalServerError().finish()
}
},
Ok(uuid) => HttpResponse::Ok().json(AccountRegisterResponse {
uuid: uuid.to_string(),
}),
},
}
}

View File

@ -0,0 +1,64 @@
use crate::backend::error::AccountRegisterError;
use crate::backend::Backend;
use actix_web::{post, web, HttpResponse, Responder};
use log::error;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize)]
struct RegisterRequest {
token: String,
password: String,
}
#[derive(Debug, Serialize)]
struct RegisterResponse {
uuid: String,
}
#[post("/account/register")]
pub async fn register(
backend: web::Data<Backend>,
body: web::Json<RegisterRequest>,
) -> impl Responder {
let body = body.into_inner();
match backend.account_register(body.token, body.password).await {
Err(e) => {
error!("{e}");
HttpResponse::InternalServerError().finish()
}
Ok(res) => match res {
Err(e) => match e {
AccountRegisterError::InvalidToken => HttpResponse::Unauthorized().finish(),
AccountRegisterError::SqlError(e) => {
error!("{e}");
HttpResponse::InternalServerError().finish()
}
},
Ok(uuid) => HttpResponse::Ok().json(RegisterResponse {
uuid: uuid.to_string(),
}),
},
}
}
#[post("/account/new_token")]
pub async fn new_token(backend: web::Data<Backend>) -> impl Responder {
match backend.account_register(body.token, body.password).await {
Err(e) => {
error!("{e}");
HttpResponse::InternalServerError().finish()
}
Ok(res) => match res {
Err(e) => match e {
AccountRegisterError::InvalidToken => HttpResponse::Unauthorized().finish(),
AccountRegisterError::SqlError(e) => {
error!("{e}");
HttpResponse::InternalServerError().finish()
}
},
Ok(uuid) => HttpResponse::Ok().json(RegisterResponse {
uuid: uuid.to_string(),
}),
},
}
}

1
src/api/endpoints/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod account;

View File

@ -10,7 +10,7 @@ pub async fn start(config: &Config, backend: Backend) -> Result<()> {
let server = HttpServer::new(move || {
App::new()
.app_data(web::Data::new(backend.clone()))
.service(endpoints::account_register)
.service(endpoints::account::register)
})
.bind((config.addr.as_str(), config.port))?;