Skip to content

Commit

Permalink
Chore update examples (#1121)
Browse files Browse the repository at this point in the history
This commit updates examples to use the latest available functionality
such as nesting and `utoipa-axum` in case of todo-axum.
  • Loading branch information
juhaku authored Oct 14, 2024
1 parent cbf08cd commit 2389d18
Show file tree
Hide file tree
Showing 21 changed files with 120 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ authors = [
actix-web = "4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
env_logger = "0.10.0"
env_logger = "0.11.0"
log = "0.4"
futures = "0.3"
utoipa = { path = "../../utoipa", features = ["actix_extras"] }
Expand Down
22 changes: 0 additions & 22 deletions examples/rocket-0_4-hello/Cargo.toml

This file was deleted.

20 changes: 0 additions & 20 deletions examples/rocket-0_4-hello/README.md

This file was deleted.

2 changes: 0 additions & 2 deletions examples/rocket-0_4-hello/rust-toolchain.toml

This file was deleted.

69 changes: 0 additions & 69 deletions examples/rocket-0_4-hello/src/main.rs

This file was deleted.

1 change: 0 additions & 1 deletion examples/rocket-todo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ utoipa-rapidoc = { path = "../../utoipa-rapidoc", features = ["rocket"] }
utoipa-scalar = { path = "../../utoipa-scalar", features = ["rocket"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
env_logger = "0.10.0"
log = "0.4"

[workspace]
29 changes: 9 additions & 20 deletions examples/rocket-todo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,10 @@ use crate::todo::TodoStore;

#[rocket::launch]
fn rocket() -> Rocket<Build> {
env_logger::init();

#[derive(OpenApi)]
#[openapi(
paths(
todo::get_tasks,
todo::create_todo,
todo::mark_done,
todo::delete_todo,
todo::search_todos,
),
components(
schemas(todo::Todo, todo::TodoError)
nest(
(path = "/api/todo", api = todo::TodoApi)
),
tags(
(name = "todo", description = "Todo management endpoints.")
Expand All @@ -49,7 +40,7 @@ fn rocket() -> Rocket<Build> {

rocket::build()
.manage(TodoStore::default())
.register("/todo", catchers![unauthorized])
.register("/api/todo", catchers![unauthorized])
.mount(
"/",
SwaggerUi::new("/swagger-ui/<_..>").url("/api-docs/openapi.json", ApiDoc::openapi()),
Expand All @@ -65,7 +56,7 @@ fn rocket() -> Rocket<Build> {
.mount("/", Redoc::with_url("/redoc", ApiDoc::openapi()))
.mount("/", Scalar::with_url("/scalar", ApiDoc::openapi()))
.mount(
"/todo",
"/api/todo",
routes![
todo::get_tasks,
todo::create_todo,
Expand Down Expand Up @@ -97,7 +88,11 @@ mod todo {
FromForm, Request, State,
};
use serde::{Deserialize, Serialize};
use utoipa::{IntoParams, ToSchema};
use utoipa::{IntoParams, OpenApi, ToSchema};

#[derive(OpenApi)]
#[openapi(paths(get_tasks, create_todo, mark_done, delete_todo, search_todos,))]
pub struct TodoApi;

pub(super) type TodoStore = Arc<Mutex<Vec<Todo>>>;

Expand Down Expand Up @@ -173,7 +168,6 @@ mod todo {

/// List all available todo items.
#[utoipa::path(
context_path = "/todo",
responses(
(status = 200, description = "Get all todos", body = [Todo])
)
Expand All @@ -187,8 +181,6 @@ mod todo {
///
/// Create new todo item and add it to the storage.
#[utoipa::path(
context_path = "/todo",
request_body = Todo,
responses(
(status = 201, description = "Todo item created successfully", body = Todo),
(status = 409, description = "Todo already exists", body = TodoError, example = json!(TodoError::Conflict(String::from("id = 1"))))
Expand Down Expand Up @@ -216,7 +208,6 @@ mod todo {
/// Tries to find todo item by given id and mark it done if found. Will return not found in case todo
/// item does not exists.
#[utoipa::path(
context_path = "/todo",
responses(
(status = 200, description = "Todo item marked done successfully"),
(status = 404, description = "Todo item not found from storage", body = TodoError, example = json!(TodoError::NotFound(String::from("id = 1"))))
Expand Down Expand Up @@ -252,7 +243,6 @@ mod todo {
///
/// Delete Todo from storage by Todo id if found.
#[utoipa::path(
context_path = "/todo",
responses(
(status = 200, description = "Todo deleted successfully"),
(status = 401, description = "Unauthorized to delete Todos", body = TodoError, example = json!(TodoError::Unauthorized(String::from("id = 1")))),
Expand Down Expand Up @@ -294,7 +284,6 @@ mod todo {
///
/// Search is performed in case sensitive manner from value of Todo.
#[utoipa::path(
context_path = "/todo",
params(
SearchParams
),
Expand Down
20 changes: 8 additions & 12 deletions examples/todo-actix/src/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ use utoipa::{IntoParams, OpenApi, ToSchema};
use crate::{LogApiKey, RequireApiKey};

#[derive(OpenApi)]
#[openapi(
paths(
get_todos,
create_todo,
delete_todo,
get_todo_by_id,
update_todo,
search_todos
),
components(schemas(Todo, TodoUpdateRequest, ErrorResponse))
)]
#[openapi(paths(
get_todos,
create_todo,
delete_todo,
get_todo_by_id,
update_todo,
search_todos
))]
pub(super) struct TodoApi;

#[derive(Default)]
Expand Down Expand Up @@ -202,7 +199,6 @@ async fn get_todo_by_id(id: Path<i32>, todo_store: Data<TodoStore>) -> impl Resp
/// updated according `TodoUpdateRequest` and updated `Todo` is returned with status 200.
/// If todo is not found then 404 not found is returned.
#[utoipa::path(
request_body = TodoUpdateRequest,
responses(
(status = 200, description = "Todo updated successfully", body = Todo),
(status = 404, description = "Todo not found by id", body = ErrorResponse, example = json!(ErrorResponse::NotFound(String::from("id = 1"))))
Expand Down
9 changes: 3 additions & 6 deletions examples/todo-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@ description = "Simple axum todo example api with utoipa and Swagger UI, Rapidoc,
version = "0.1.0"
edition = "2021"
license = "MIT"
authors = [
"Elli Example <example@example.com>"
]
authors = ["Elli Example <example@example.com>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = "0.7"
hyper = { version = "1.0.1", features = ["full"] }
tokio = { version = "1.17", features = ["full"] }
tower = "0.4"
tower = "0.5"
utoipa = { path = "../../utoipa", features = ["axum_extras"] }
utoipa-swagger-ui = { path = "../../utoipa-swagger-ui", features = ["axum"] }
utoipa-axum = { path = "../../utoipa-axum" }
utoipa-redoc = { path = "../../utoipa-redoc", features = ["axum"] }
utoipa-rapidoc = { path = "../../utoipa-rapidoc", features = ["axum"] }
utoipa-scalar = { path = "../../utoipa-scalar", features = ["axum"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
env_logger = "0.11"
log = "0.4"

[workspace]
6 changes: 0 additions & 6 deletions examples/todo-axum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,3 @@ Scalar can be reached on `http://localhost:8080/scalar`.
```bash
cargo run
```

If you want to see some logging, you may prepend the command with `RUST_LOG=debug` as shown below.

```bash
RUST_LOG=debug cargo run
```
Loading

0 comments on commit 2389d18

Please sign in to comment.