init
This commit is contained in:
commit
c5731665a3
25 changed files with 1110 additions and 0 deletions
3
src/routes/index.rs
Normal file
3
src/routes/index.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub async fn route() -> &'static str {
|
||||
"Hello world"
|
||||
}
|
||||
57
src/routes/login.rs
Normal file
57
src/routes/login.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use axum::{
|
||||
extract::{FromRequest, Form, Json, Query},
|
||||
http::{self, Request, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
RequestExt,
|
||||
};
|
||||
use axum_typed_multipart::{TryFromMultipart, TypedMultipart};
|
||||
|
||||
#[derive(Debug, TryFromMultipart, Deserialize)]
|
||||
pub struct LoginForm {
|
||||
username: String,
|
||||
#[allow(dead_code)]
|
||||
password: String,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S, B> FromRequest<S, B> for LoginForm
|
||||
where
|
||||
Json<LoginForm>: FromRequest<(), B>,
|
||||
Form<LoginForm>: FromRequest<(), B>,
|
||||
TypedMultipart<LoginForm>: FromRequest<S, B>,
|
||||
B::Data: Into<axum::body::Bytes>,
|
||||
B::Error: Into<axum::BoxError> + Send + std::error::Error,
|
||||
B: Send + 'static + axum::body::HttpBody,
|
||||
S: Send
|
||||
{
|
||||
type Rejection = Response;
|
||||
|
||||
async fn from_request(req: Request<B>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
let headers = req.headers();
|
||||
|
||||
if let Some(mime) = headers.get(http::header::CONTENT_TYPE).and_then(|v| v.to_str().ok()) {
|
||||
if mime.starts_with("application/json") {
|
||||
let Json(login_form): Json<LoginForm> = req.extract().await.map_err(IntoResponse::into_response)?;
|
||||
return Ok(login_form);
|
||||
}
|
||||
|
||||
if mime.starts_with("application/x-www-form-urlencoded") {
|
||||
let Form(login_form) = req.extract().await.map_err(IntoResponse::into_response)?;
|
||||
return Ok(login_form);
|
||||
}
|
||||
|
||||
if mime.starts_with("multipart/form-data") {
|
||||
let TypedMultipart(login_form): TypedMultipart<LoginForm> = req.extract().await.map_err(IntoResponse::into_response)?;
|
||||
return Ok(login_form);
|
||||
}
|
||||
Err(StatusCode::UNSUPPORTED_MEDIA_TYPE.into_response())
|
||||
} else {
|
||||
Err("No POST Content-Type".into_response())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn route(form: LoginForm) -> String {
|
||||
format!("Welcome {}", form.username)
|
||||
}
|
||||
24
src/routes/mod.rs
Normal file
24
src/routes/mod.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use axum::{
|
||||
extract::State,
|
||||
routing::{get, post},
|
||||
Router,
|
||||
};
|
||||
|
||||
use crate::state::RoutableAppState;
|
||||
|
||||
mod index;
|
||||
mod login;
|
||||
|
||||
/// Build a router for the application, in a specific subpath eg `/yunohost/sso/`
|
||||
pub fn router(subpath: Option<String>, state: RoutableAppState) -> Router {
|
||||
let app = Router::new()
|
||||
.route("/", get(index::route))
|
||||
.route("/login/", get(login::route))
|
||||
.with_state(state);
|
||||
if let Some(p) = subpath {
|
||||
Router::new()
|
||||
.nest(&p, app)
|
||||
} else {
|
||||
app
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue