Add context reqruied later for authentication

This commit is contained in:
Simon Goller 2024-05-03 19:01:26 +02:00
parent 9a367c9260
commit 20828fb4a1
14 changed files with 463 additions and 330 deletions

View file

@ -1,14 +0,0 @@
use axum::{extract::State, response::Response};
use crate::{error_handler, RestStateDef, RoString};
use service::HelloService;
pub async fn hello<RestState: RestStateDef>(State(rest_state): State<RestState>) -> Response {
error_handler(
(async {
let string = rest_state.hello_service().hello().await?;
Ok(RoString::from(string).into())
})
.await,
)
}

View file

@ -1,13 +1,15 @@
use std::{convert::Infallible, sync::Arc};
mod hello;
mod permission;
mod slot;
use axum::{body::Body, response::Response, routing::get, Router};
use axum::{body::Body, response::Response, Router};
use thiserror::Error;
use uuid::Uuid;
// TODO: In prod, it must be a different type than in dev mode.
type Context = ();
pub struct RoString(Arc<str>, bool);
impl http_body::Body for RoString {
type Data = bytes::Bytes;
@ -125,18 +127,15 @@ fn error_handler(result: Result<Response, RestError>) -> Response {
}
pub trait RestStateDef: Clone + Send + Sync + 'static {
type HelloService: service::HelloService + Send + Sync + 'static;
type PermissionService: service::PermissionService + Send + Sync + 'static;
type SlotService: service::slot::SlotService + Send + Sync + 'static;
type PermissionService: service::PermissionService<Context = Context> + Send + Sync + 'static;
type SlotService: service::slot::SlotService<Context = Context> + Send + Sync + 'static;
fn hello_service(&self) -> Arc<Self::HelloService>;
fn permission_service(&self) -> Arc<Self::PermissionService>;
fn slot_service(&self) -> Arc<Self::SlotService>;
}
pub async fn start_server<RestState: RestStateDef>(rest_state: RestState) {
let app = Router::new()
.route("/", get(hello::hello::<RestState>))
.nest("/permission", permission::generate_route())
.nest("/slot", slot::generate_route())
.with_state(rest_state);

View file

@ -87,7 +87,7 @@ pub async fn add_user<RestState: RestStateDef>(
(async {
rest_state
.permission_service()
.create_user(user.name.as_str())
.create_user(user.name.as_str(), ())
.await?;
Ok(Response::builder()
.status(201)
@ -105,7 +105,10 @@ pub async fn remove_user<RestState: RestStateDef>(
println!("Removing user: {:?}", user);
error_handler(
(async {
rest_state.permission_service().delete_user(&user).await?;
rest_state
.permission_service()
.delete_user(&user, ())
.await?;
Ok(Response::builder()
.status(200)
.body(Body::from(""))
@ -123,7 +126,7 @@ pub async fn add_role<RestState: RestStateDef>(
(async {
rest_state
.permission_service()
.create_role(role.name.as_str())
.create_role(role.name.as_str(), ())
.await?;
Ok(Response::builder()
.status(200)
@ -142,7 +145,7 @@ pub async fn delete_role<RestState: RestStateDef>(
(async {
rest_state
.permission_service()
.delete_role(role.as_str())
.delete_role(role.as_str(), ())
.await?;
Ok(Response::builder()
.status(200)
@ -161,7 +164,7 @@ pub async fn add_user_role<RestState: RestStateDef>(
(async {
rest_state
.permission_service()
.add_user_role(user_role.user.as_str(), user_role.role.as_str())
.add_user_role(user_role.user.as_str(), user_role.role.as_str(), ())
.await?;
Ok(Response::builder()
.status(201)
@ -180,7 +183,7 @@ pub async fn remove_user_role<RestState: RestStateDef>(
(async {
rest_state
.permission_service()
.delete_user_role(user_role.user.as_str(), user_role.role.as_str())
.delete_user_role(user_role.user.as_str(), user_role.role.as_str(), ())
.await?;
Ok(Response::builder()
.status(200)
@ -202,6 +205,7 @@ pub async fn add_role_privilege<RestState: RestStateDef>(
.add_role_privilege(
role_privilege.role.as_str(),
role_privilege.privilege.as_str(),
(),
)
.await?;
Ok(Response::builder()
@ -224,6 +228,7 @@ pub async fn remove_role_privilege<RestState: RestStateDef>(
.delete_role_privilege(
role_privilege.role.as_str(),
role_privilege.privilege.as_str(),
(),
)
.await?;
Ok(Response::builder()
@ -240,7 +245,7 @@ pub async fn get_all_users<RestState: RestStateDef>(rest_state: State<RestState>
(async {
let users: Arc<[UserTO]> = rest_state
.permission_service()
.get_all_users()
.get_all_users(())
.await?
.iter()
.map(UserTO::from)
@ -259,7 +264,7 @@ pub async fn get_all_roles<RestState: RestStateDef>(rest_state: State<RestState>
(async {
let roles: Arc<[RoleTO]> = rest_state
.permission_service()
.get_all_roles()
.get_all_roles(())
.await?
.iter()
.map(RoleTO::from)
@ -278,7 +283,7 @@ pub async fn get_all_privileges<RestState: RestStateDef>(rest_state: State<RestS
(async {
let privileges: Arc<[PrivilegeTO]> = rest_state
.permission_service()
.get_all_privileges()
.get_all_privileges(())
.await?
.iter()
.map(PrivilegeTO::from)

View file

@ -107,7 +107,7 @@ pub async fn get_all_slots<RestState: RestStateDef>(rest_state: State<RestState>
(async {
let slots: Arc<[SlotTO]> = rest_state
.slot_service()
.get_slots()
.get_slots(())
.await?
.iter()
.map(SlotTO::from)
@ -127,7 +127,7 @@ pub async fn get_slot<RestState: RestStateDef>(
) -> Response {
error_handler(
(async {
let slot = SlotTO::from(&rest_state.slot_service().get_slot(&slot_id).await?.into());
let slot = SlotTO::from(&rest_state.slot_service().get_slot(&slot_id, ()).await?);
Ok(Response::builder()
.status(200)
.body(Body::new(serde_json::to_string(&slot).unwrap()))
@ -146,7 +146,7 @@ pub async fn create_slot<RestState: RestStateDef>(
let slot = SlotTO::from(
&rest_state
.slot_service()
.create_slot(&(&slot).into())
.create_slot(&(&slot).into(), ())
.await?,
);
Ok(Response::builder()
@ -170,7 +170,7 @@ pub async fn update_slot<RestState: RestStateDef>(
}
rest_state
.slot_service()
.update_slot(&(&slot).into())
.update_slot(&(&slot).into(), ())
.await?;
Ok(Response::builder()
.status(200)