Skip to content

Commit

Permalink
added command function scafholdings
Browse files Browse the repository at this point in the history
  • Loading branch information
worldofjoni committed Jun 21, 2024
1 parent e29fcc8 commit 95c2670
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 20 deletions.
14 changes: 14 additions & 0 deletions backend/src/interface/api_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ pub trait Command: Send + Sync {

/// command to add a rating to a meal.
async fn set_meal_rating(&self, meal_id: Uuid, rating: u32, client_id: Uuid) -> Result<()>;

/// Marks an image as verified and deletes all its reports. // todo do we want this?
async fn verify_image(&self, image_id: Uuid) -> Result<()>;

/// Deletes an image. // todo only hide?
async fn delete_image(&self, image_id: Uuid) -> Result<()>;
}

#[async_trait]
Expand Down Expand Up @@ -105,6 +111,14 @@ impl<C: Command> Command for Arc<C> {
.set_meal_rating(meal_id, rating, client_id)
.await
}

async fn verify_image(&self, image_id: Uuid) -> Result<()> {
Self::as_ref(self).verify_image(image_id).await
}

async fn delete_image(&self, image_id: Uuid) -> Result<()> {
Self::as_ref(self).delete_image(image_id).await
}
}

/// Enum describing the possible ways, a command can fail.
Expand Down
8 changes: 8 additions & 0 deletions backend/src/layer/logic/api_command/command_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ where
.await?;
Ok(())
}

async fn delete_image(&self, _image_id: Uuid) -> Result<()> {
todo!()
}

async fn verify_image(&self, _image_id: Uuid) -> Result<()> {
todo!()
}
}

#[cfg(test)]
Expand Down
58 changes: 48 additions & 10 deletions backend/src/layer/trigger/api/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,71 @@ use std::sync::Arc;

use axum::{
debug_handler,
extract::State,
extract::{Path, State},
headers::{authorization::Basic, Authorization},
http::HeaderValue,
middleware::Next,
middleware::{self, Next},
response::IntoResponse,
routing::method_routing::get,
Router, TypedHeader,
};
use hyper::{header::WWW_AUTHENTICATE, HeaderMap, Request, StatusCode};

use crate::interface::api_command::Command;
use tracing::warn;

use crate::{
interface::api_command::{Command, CommandError},
util::Uuid,
};

#[derive(Clone)]
pub(super) struct AdminKey(pub String);
pub(super) struct AdminKey(String);

pub(super) type ArcCommand = Arc<dyn Command + Send + Sync>;

pub(super) fn admin_router() -> Router<ArcCommand> {
Router::new().route("/test", get(test_command))
pub(super) fn admin_router(admin_key: String, command: ArcCommand) -> Router<()> {
let admin_auth = middleware::from_fn_with_state(AdminKey(admin_key), admin_auth_middleware);
// let router = Router::new()
// .route("/version", get(version))
// .route("/report/delete_image/:image_id", get(delete_image))
// .route("/report/verify_image/:image_id", get(verify_image))
// .layer(HandleErrorLayer::new(handle_error));

Router::new()
.route("/version", get(version))
.route("/report/delete_image/:image_id", get(delete_image))
.route("/report/verify_image/:image_id", get(verify_image))
.layer(admin_auth)
.with_state(command)
}

impl IntoResponse for CommandError {
fn into_response(self) -> axum::response::Response {
let error = self.to_string();
warn!("On Admin API request: {error}");
(StatusCode::INTERNAL_SERVER_ERROR, error).into_response()
}
}

#[debug_handler]
async fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}

#[debug_handler]
async fn verify_image(
State(command): State<ArcCommand>,
Path(image_id): Path<Uuid>,
) -> Result<(), CommandError> {
command.verify_image(image_id).await
}

#[debug_handler]
async fn test_command(
async fn delete_image(
State(command): State<ArcCommand>,
body: Request<axum::body::Body>,
) -> &'static str {
"working" // todo
Path(image_id): Path<Uuid>,
) -> Result<(), CommandError> {
command.delete_image(image_id).await
}

const ADMIN_USER: &str = "admin";
Expand Down
8 changes: 8 additions & 0 deletions backend/src/layer/trigger/api/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@ impl Command for CommandMock {
) -> CommandResult<()> {
Ok(())
}

async fn delete_image(&self, _image_id: Uuid) -> CommandResult<()> {
Ok(())
}

async fn verify_image(&self, _image_id: Uuid) -> CommandResult<()> {
Ok(())
}
}

pub struct AuthDataMock;
Expand Down
14 changes: 4 additions & 10 deletions backend/src/layer/trigger/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,18 @@ impl ApiServer {
Duration::from_secs(1),
));

let admin_auth = middleware::from_fn_with_state(
AdminKey(self.server_info.admin_key.clone()),
admin_auth_middleware,
let admin_router = admin_router(
self.server_info.admin_key.clone(),
self.command_copy.clone() as ArcCommand,
);
let admin_router = admin_router();

let app = Router::new()
.route(
"/",
get(graphql_playground).post(graphql_handler.layer(auth)),
)
.layer(Extension(self.schema.clone()))
.nest(
"/admin",
admin_router
.layer(admin_auth)
.with_state(self.command_copy.clone() as ArcCommand),
)
.nest("/admin", admin_router)
.nest_service(IMAGE_BASE_PATH, ServeDir::new(&self.server_info.image_dir))
.layer(rate_limit)
.layer(DefaultBodyLimit::max(
Expand Down

0 comments on commit 95c2670

Please sign in to comment.