Skip to content

Commit

Permalink
refactor!: upgrade utoipa to v5 (#65)
Browse files Browse the repository at this point in the history
* chore(deps): bump utoipa to v5

* feat(openapi): scalar frontend

* chore(api_admin): fix typo

* refactor(backend): use utoipa-axum

* refactor(api): use utoipa-axum

* refactor: move openapi routes to backend

* chore(api): remove unused deps

* refactor(api): use utoipa-axum

* refactor(api_mastodon): use utoipa-axum

* refactor(well_known): use utoipa-axum

* refactor(nodeinfo): use utoipa-axum

* refactor(api_apub): use utoipa-axum

* refactor(openapi): move specific options

* refactor: remove `hatsu_openapi` crate
  • Loading branch information
kwaa authored Sep 14, 2024
1 parent 0034188 commit 1c9c0ce
Show file tree
Hide file tree
Showing 49 changed files with 369 additions and 413 deletions.
61 changes: 38 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ members = [
"crates/db_schema",
"crates/feed",
"crates/nodeinfo",
"crates/openapi",
"crates/tracing",
"crates/utils",
"crates/well_known",
Expand All @@ -79,7 +78,6 @@ hatsu_db_migration = { path = "./crates/db_migration" }
hatsu_db_schema = { path = "./crates/db_schema" }
hatsu_feed = { path = "./crates/feed" }
hatsu_nodeinfo = { path = "./crates/nodeinfo" }
hatsu_openapi = { path = "./crates/openapi" }
hatsu_tracing = { path = "./crates/tracing" }
hatsu_utils = { path = "./crates/utils" }
hatsu_well_known = { path = "./crates/well_known" }
Expand Down Expand Up @@ -132,8 +130,9 @@ tracing = { version = "0.1" }
tracing-error = "0.2"
url = { version = "2.4", features = ["serde"] }
urlencoding = "2"
utoipa = { version = "4", features = ["axum_extras", "url", "uuid"] }
# utoipa-swagger-ui = { version = "7", features = ["axum"] }
utoipa = { version = "5.0.0-beta", features = ["axum_extras", "url", "uuid"] }
utoipa-axum = "0.1.0-beta"
utoipa-scalar = { version = "0.2.0-beta", features = ["axum"] }
uuid = { version = "1.8", features = [
"v4",
"v7",
Expand All @@ -145,7 +144,6 @@ uuid = { version = "1.8", features = [
hatsu_api_apub = { workspace = true }
hatsu_apub = { workspace = true }
hatsu_backend = { workspace = true }
# hatsu_cron = { workspace = true }
hatsu_db_migration = { workspace = true }
hatsu_db_schema = { workspace = true }
hatsu_tracing = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ path = "src/lib.rs"

[dependencies]
axum = { workspace = true }
url = { workspace = true }
utoipa = { workspace = true }
utoipa-axum = { workspace = true }
3 changes: 1 addition & 2 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// pub mod entities;
pub mod routes;

pub use routes::routes;
pub use routes::{routes, TAG};
4 changes: 3 additions & 1 deletion crates/api/src/routes/generate_204.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use axum::{debug_handler, http::StatusCode};

use crate::TAG;

/// Generate 204 Response
#[utoipa::path(
get,
tag = "hatsu",
tag = TAG,
path = "/api/v0/generate_204",
responses(
(status = NO_CONTENT, description = "NO_CONTENT"),
Expand Down
15 changes: 10 additions & 5 deletions crates/api/src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use axum::{routing::get, Router};
use utoipa::OpenApi;
use utoipa_axum::{router::OpenApiRouter, routes};

pub mod generate_204;
mod generate_204;

use generate_204::generate_204;
pub const TAG: &str = "hatsu";

pub fn routes() -> Router {
Router::new().route("/api/v0/generate_204", get(generate_204))
#[derive(OpenApi)]
#[openapi(tags((name = TAG, description = "Hatsu API (/api/v0/)")))]
pub struct HatsuApi;

pub fn routes() -> OpenApiRouter {
OpenApiRouter::with_openapi(HatsuApi::openapi()).routes(routes!(generate_204::generate_204))
}
1 change: 1 addition & 0 deletions crates/api_admin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ axum = { workspace = true }
sea-orm = { workspace = true }
serde = { workspace = true }
utoipa = { workspace = true }
utoipa-axum = { workspace = true }
2 changes: 1 addition & 1 deletion crates/api_admin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod entities;
pub mod routes;

pub use routes::routes;
pub use routes::{routes, TAG};
9 changes: 6 additions & 3 deletions crates/api_admin/src/routes/create_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ use hatsu_db_schema::prelude::User;
use hatsu_utils::{AppData, AppError};
use sea_orm::{ActiveModelTrait, EntityTrait, IntoActiveModel};

use crate::entities::{CreateRemoveAccount, CreateRemoveAccountResult};
use crate::{
entities::{CreateRemoveAccount, CreateRemoveAccountResult},
TAG,
};

/// Create Account
#[utoipa::path(
post,
tag = "hatsu::admin",
tag = TAG,
path = "/api/v0/admin/create-account",
responses(
(status = CREATED, description = "create succesfully", body = CreateRemoveAccountResult),
(status = CREATED, description = "create successfully", body = CreateRemoveAccountResult),
(status = BAD_REQUEST, description = "error", body = AppError)
),
security(("api_key" = ["token"]))
Expand Down
48 changes: 38 additions & 10 deletions crates/api_admin/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,49 @@ use axum::{
http::{Request, StatusCode},
middleware::{self, Next},
response::Response,
routing::post,
Router,
};
use hatsu_utils::AppData;
use utoipa::{
openapi::security::{ApiKey, ApiKeyValue, SecurityScheme},
Modify,
OpenApi,
};
use utoipa_axum::{router::OpenApiRouter, routes};

use crate::entities::{CreateRemoveAccount, CreateRemoveAccountResult};

mod create_account;
mod remove_account;

pub const TAG: &str = "hatsu::admin";

pub mod create_account;
pub mod remove_account;
#[derive(OpenApi)]
#[openapi(
components(schemas(CreateRemoveAccount, CreateRemoveAccountResult)),
modifiers(&SecurityAddon),
tags(
(name = TAG, description = "Hatsu Admin API (/api/v0/admin/)"),
)
)]
pub struct HatsuAdminApi;

use create_account::create_account;
use remove_account::remove_account;
pub struct SecurityAddon;

impl Modify for SecurityAddon {
fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
if let Some(components) = openapi.components.as_mut() {
components.add_security_scheme(
"api_key",
SecurityScheme::ApiKey(ApiKey::Query(ApiKeyValue::new("token"))),
);
}
}
}

pub fn routes() -> Router {
Router::new()
.route("/api/v0/admin/create-account", post(create_account))
.route("/api/v0/admin/remove-account", post(remove_account))
pub fn routes() -> OpenApiRouter {
OpenApiRouter::with_openapi(HatsuAdminApi::openapi())
.routes(routes!(create_account::create_account))
.routes(routes!(remove_account::remove_account))
.layer(middleware::from_fn(auth))
}

Expand Down
9 changes: 6 additions & 3 deletions crates/api_admin/src/routes/remove_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ use hatsu_db_schema::prelude::User;
use hatsu_utils::{AppData, AppError};
use sea_orm::EntityTrait;

use crate::entities::{CreateRemoveAccount, CreateRemoveAccountResult};
use crate::{
entities::{CreateRemoveAccount, CreateRemoveAccountResult},
TAG,
};

/// Remove Account
#[utoipa::path(
post,
tag = "hatsu::admin",
tag = TAG,
path = "/api/v0/admin/remove-account",
responses(
// (status = OK, description = "remove succesfully", body = CreateRemoveAccountResult),
// (status = OK, description = "remove successfully", body = CreateRemoveAccountResult),
(status = METHOD_NOT_ALLOWED, description = "not implemented", body = CreateRemoveAccountResult),
(status = BAD_REQUEST, description = "error", body = AppError)
),
Expand Down
1 change: 1 addition & 0 deletions crates/api_apub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ serde_json = { workspace = true }
tracing = { workspace = true }
url = { workspace = true }
utoipa = { workspace = true }
utoipa-axum = { workspace = true }
22 changes: 3 additions & 19 deletions crates/api_apub/src/activities/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,18 @@ use activitypub_federation::{
protocol::context::WithContext,
};
use axum::{debug_handler, extract::Path, response::Redirect};
// use axum_extra::routing::TypedPath;
use hatsu_apub::activities::ApubActivity;
use hatsu_db_schema::prelude::Activity;
use hatsu_utils::{AppData, AppError};
use sea_orm::EntityTrait;
// use serde::Deserialize;
use serde_json::Value;

// #[derive(TypedPath, Deserialize)]
// #[typed_path("/a/*activity_id")]
// pub struct Activities {
// activity_id: String
// }

// #[derive(TypedPath, Deserialize)]
// #[typed_path("/activities/*activity_id")]
// pub struct ActivitiesRedirect {
// activity_id: String
// }
use crate::TAG;

/// Get activity
#[utoipa::path(
get,
tag = "apub",
tag = TAG,
path = "/activities/{activity}",
responses(
(status = OK, description = "Activity", body = Value),
Expand All @@ -39,7 +27,6 @@ use serde_json::Value;
)]
#[debug_handler]
pub async fn activity(
// Activities { activity_id }: Activities,
Path(activity_id): Path<String>,
data: Data<AppData>,
) -> Result<FederationJson<WithContext<Value>>, AppError> {
Expand All @@ -63,9 +50,6 @@ pub async fn activity(
}

#[debug_handler]
pub async fn redirect(
// ActivitiesRedirect { activity_id }: ActivitiesRedirect,
Path(activity_id): Path<String>,
) -> Redirect {
pub async fn redirect(Path(activity_id): Path<String>) -> Redirect {
Redirect::permanent(&format!("/activities/{activity_id}"))
}
Loading

0 comments on commit 1c9c0ce

Please sign in to comment.