forked from torrust/torrust-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): [torrust#183] Axum API, user context, registration
- Loading branch information
1 parent
9f8832b
commit 79682a5
Showing
21 changed files
with
266 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct RegistrationForm { | ||
pub username: String, | ||
pub email: Option<String>, | ||
pub password: String, | ||
pub confirm_password: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//! API handlers for the the [`user`](crate::web::api::v1::contexts::user) API | ||
//! context. | ||
use std::sync::Arc; | ||
|
||
use axum::extract::{self, Host, State}; | ||
use axum::Json; | ||
|
||
use super::forms::RegistrationForm; | ||
use super::responses::{self, NewUser}; | ||
use crate::common::AppData; | ||
use crate::errors::ServiceError; | ||
use crate::web::api::v1::responses::OkResponse; | ||
|
||
/// It handles the registration of a new user. | ||
/// | ||
/// # Errors | ||
/// | ||
/// It returns an error if the user could not be registered. | ||
#[allow(clippy::unused_async)] | ||
pub async fn registration_handler( | ||
State(app_data): State<Arc<AppData>>, | ||
Host(host_from_header): Host, | ||
extract::Json(registration_form): extract::Json<RegistrationForm>, | ||
) -> Result<Json<OkResponse<NewUser>>, ServiceError> { | ||
let api_base_url = app_data | ||
.cfg | ||
.get_api_base_url() | ||
.await | ||
.unwrap_or(api_base_url(&host_from_header)); | ||
|
||
match app_data | ||
.registration_service | ||
.register_user(®istration_form, &api_base_url) | ||
.await | ||
{ | ||
Ok(user_id) => Ok(responses::added_user(user_id)), | ||
Err(error) => Err(error), | ||
} | ||
} | ||
|
||
/// It returns the base API URL without the port. For example: `http://localhost`. | ||
fn api_base_url(host: &str) -> String { | ||
// HTTPS is not supported yet. | ||
// See https://github.com/torrust/torrust-index-backend/issues/131 | ||
format!("http://{host}") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use axum::Json; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::models::user::UserId; | ||
use crate::web::api::v1::responses::OkResponse; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct NewUser { | ||
pub user_id: UserId, | ||
} | ||
|
||
/// Response after successfully creating a new user. | ||
pub fn added_user(user_id: i64) -> Json<OkResponse<NewUser>> { | ||
Json(OkResponse { | ||
data: NewUser { user_id }, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//! API routes for the [`user`](crate::web::api::v1::contexts::user) API context. | ||
//! | ||
//! Refer to the [API endpoint documentation](crate::web::api::v1::contexts::user). | ||
use std::sync::Arc; | ||
|
||
use axum::routing::post; | ||
use axum::Router; | ||
|
||
use super::handlers::registration_handler; | ||
use crate::common::AppData; | ||
|
||
/// Routes for the [`user`](crate::web::api::v1::contexts::user) API context. | ||
pub fn router(app_data: Arc<AppData>) -> Router { | ||
Router::new().route("/register", post(registration_handler).with_state(app_data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ | |
//! information. | ||
pub mod auth; | ||
pub mod contexts; | ||
pub mod responses; | ||
pub mod routes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! Generic responses for the API. | ||
use axum::response::{IntoResponse, Response}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::databases::database; | ||
use crate::errors::{http_status_code_for_service_error, map_database_error_to_service_error, ServiceError}; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct OkResponse<T> { | ||
pub data: T, | ||
} | ||
|
||
impl IntoResponse for database::Error { | ||
fn into_response(self) -> Response { | ||
let service_error = map_database_error_to_service_error(&self); | ||
|
||
(http_status_code_for_service_error(&service_error), service_error.to_string()).into_response() | ||
} | ||
} | ||
|
||
impl IntoResponse for ServiceError { | ||
fn into_response(self) -> Response { | ||
(http_status_code_for_service_error(&self), self.to_string()).into_response() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use crate::common::asserts::assert_json_ok; | ||
use crate::common::contexts::user::responses::AddedUserResponse; | ||
use crate::common::responses::TextResponse; | ||
|
||
pub fn assert_added_user_response(response: &TextResponse) { | ||
let _added_user_response: AddedUserResponse = serde_json::from_str(&response.body) | ||
.unwrap_or_else(|_| panic!("response {:#?} should be a AddedUserResponse", response.body)); | ||
assert_json_ok(response); | ||
} |
Oops, something went wrong.