refactor(api): promoted endpoints module to a directory
This commit is contained in:
parent
d9480a8727
commit
4bf8f83ba8
|
@ -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(),
|
||||
}),
|
||||
},
|
||||
}
|
||||
}
|
|
@ -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(),
|
||||
}),
|
||||
},
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
pub mod account;
|
|
@ -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))?;
|
||||
|
||||
|
|
Loading…
Reference in New Issue