feat: add route and tests
This commit is contained in:
parent
122c38a0da
commit
57704def31
5 changed files with 155 additions and 14 deletions
67
src/error.rs
Normal file
67
src/error.rs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
use axum::http::StatusCode;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use serde::Serialize;
|
||||
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, strum_macros::AsRefStr)]
|
||||
#[serde(tag = "type", content = "data")]
|
||||
pub enum Error {
|
||||
LoginFail,
|
||||
|
||||
// -- Auth errors.
|
||||
AuthFailNoAuthTokenCookie,
|
||||
AuthFailTokenWrongFormat,
|
||||
AuthFailCtxNotInRequestExt,
|
||||
|
||||
// -- Model errors.
|
||||
PropertyDeleteFailIdNotFound { id: u64 },
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn client_status_and_error(&self) -> (StatusCode, ClientError) {
|
||||
match self {
|
||||
// -- Login.
|
||||
Self::LoginFail => (StatusCode::UNAUTHORIZED, ClientError::LOGIN_FAIL),
|
||||
|
||||
// -- Auth.
|
||||
Self::AuthFailNoAuthTokenCookie
|
||||
| Self::AuthFailTokenWrongFormat
|
||||
| Self::AuthFailCtxNotInRequestExt => (StatusCode::FORBIDDEN, ClientError::NO_AUTH),
|
||||
|
||||
// -- Model.
|
||||
Self::PropertyDeleteFailIdNotFound { .. } => {
|
||||
(StatusCode::BAD_REQUEST, ClientError::INVALID_PARAMS)
|
||||
}
|
||||
|
||||
// -- Fallback.
|
||||
_ => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
ClientError::SERVICE_ERROR,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for Error {
|
||||
fn into_response(self) -> Response {
|
||||
println!("->> {:<12} - {self:?}", "INTO_RESPONSE");
|
||||
|
||||
// Create a placeholder Axum response.
|
||||
let mut response = StatusCode::INTERNAL_SERVER_ERROR.into_response();
|
||||
|
||||
// Insert the Error into the response.
|
||||
response.extensions_mut().insert(self);
|
||||
|
||||
response
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, strum_macros::AsRefStr)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum ClientError {
|
||||
LOGIN_FAIL,
|
||||
NO_AUTH,
|
||||
INVALID_PARAMS,
|
||||
SERVICE_ERROR,
|
||||
}
|
||||
16
src/lib.rs
16
src/lib.rs
|
|
@ -2,18 +2,28 @@
|
|||
use axum::extract::{Path, Query};
|
||||
use axum::http::{Method, Uri};
|
||||
use axum::response::{Html, IntoResponse, Response};
|
||||
use axum::routing::{get, get_service, IntoMakeService};
|
||||
use axum::routing::{get, get_service, post, IntoMakeService};
|
||||
use axum::Server;
|
||||
use axum::{middleware, Json, Router};
|
||||
use hyper::server::conn::AddrIncoming;
|
||||
use serde::Deserialize;
|
||||
use serde_json::{json, Value};
|
||||
use std::net::SocketAddr;
|
||||
use std::net::TcpListener;
|
||||
|
||||
pub type App = Server<AddrIncoming, IntoMakeService<Router>>;
|
||||
|
||||
/// API routes
|
||||
#[derive(Deserialize)]
|
||||
struct FormData {
|
||||
email: String,
|
||||
name: String,
|
||||
}
|
||||
|
||||
/// API routing
|
||||
fn app() -> Router {
|
||||
Router::new().route("/health_check", get(|| async {}))
|
||||
Router::new()
|
||||
.route("/health_check", get(|| async {}))
|
||||
.route("/subscriptions", post(|| async {}))
|
||||
}
|
||||
|
||||
/// Start the server
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue