From 0c4c9eef87c1cb49c303f3bf5f3af002d6ae0db6 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 4 Jan 2026 17:12:02 +0900 Subject: [PATCH] Add pkg version --- .../changepack_log_KcdLl0PBqSzwXJah8jSDB.json | 1 + Cargo.lock | 6 +-- README.md | 44 ++++++++++++++++++- crates/vespera_macro/src/lib.rs | 16 ++++--- examples/axum-example/openapi.json | 2 +- .../snapshots/integration_test__openapi.snap | 2 +- openapi.json | 2 +- 7 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 .changepacks/changepack_log_KcdLl0PBqSzwXJah8jSDB.json diff --git a/.changepacks/changepack_log_KcdLl0PBqSzwXJah8jSDB.json b/.changepacks/changepack_log_KcdLl0PBqSzwXJah8jSDB.json new file mode 100644 index 0000000..feb2932 --- /dev/null +++ b/.changepacks/changepack_log_KcdLl0PBqSzwXJah8jSDB.json @@ -0,0 +1 @@ +{"changes":{"crates/vespera_core/Cargo.toml":"Patch","crates/vespera_macro/Cargo.toml":"Patch","crates/vespera/Cargo.toml":"Patch"},"note":"Apply automatic sync to Cargo.toml package version","date":"2026-01-04T08:11:58.130989600Z"} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 132badd..e33a3d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1674,7 +1674,7 @@ checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" [[package]] name = "vespera" -version = "0.1.16" +version = "0.1.18" dependencies = [ "axum", "axum-extra", @@ -1684,7 +1684,7 @@ dependencies = [ [[package]] name = "vespera_core" -version = "0.1.16" +version = "0.1.18" dependencies = [ "rstest", "serde", @@ -1693,7 +1693,7 @@ dependencies = [ [[package]] name = "vespera_macro" -version = "0.1.16" +version = "0.1.18" dependencies = [ "anyhow", "insta", diff --git a/README.md b/README.md index d314615..6059263 100644 --- a/README.md +++ b/README.md @@ -248,7 +248,7 @@ let app = vespera!( dir = "routes", // Route folder name (default: "routes") openapi = "openapi.json", // OpenAPI JSON file path (optional) title = "My API", // API title (optional, default: "API") - version = "1.0.0", // API version (optional, default: "1.0.0") + version = "1.0.0", // API version (optional, default: Cargo.toml version) docs_url = "/docs" // Swagger UI documentation URL (optional) ); ``` @@ -268,13 +268,53 @@ let app = vespera!( - **`title`**: API title (optional, default: `"API"`) - Used in the `info.title` field of the OpenAPI document -- **`version`**: API version (optional, default: `"1.0.0"`) +- **`version`**: API version (optional, default: your `Cargo.toml` version) - Used in the `info.version` field of the OpenAPI document + - If not specified, automatically uses the version from your project's `Cargo.toml` (`CARGO_PKG_VERSION`) - **`docs_url`**: Swagger UI documentation URL (optional) - If specified, you can view the API documentation through Swagger UI at that path - Example: Setting `docs_url = "/docs"` allows viewing documentation at `http://localhost:3000/docs` +- **`redoc_url`**: ReDoc documentation URL (optional) + - If specified, you can view the API documentation through ReDoc at that path + - Example: Setting `redoc_url = "/redoc"` allows viewing documentation at `http://localhost:3000/redoc` + +#### Environment Variables + +All macro parameters can also be configured via environment variables. Environment variables are used as fallbacks when the corresponding macro parameter is not specified. + +| Macro Parameter | Environment Variable | Description | +|-----------------|---------------------|-------------| +| `dir` | `VESPERA_DIR` | Route folder name | +| `openapi` | `VESPERA_OPENAPI` | OpenAPI JSON file path | +| `title` | `VESPERA_TITLE` | API title | +| `version` | `VESPERA_VERSION` | API version | +| `docs_url` | `VESPERA_DOCS_URL` | Swagger UI documentation URL | +| `redoc_url` | `VESPERA_REDOC_URL` | ReDoc documentation URL | + +**Priority Order** (highest to lowest): +1. Macro parameter (e.g., `version = "1.0.0"`) +2. Environment variable (e.g., `VESPERA_VERSION`) +3. `CARGO_PKG_VERSION` (for `version` only) +4. Default value + +**Example:** + +```bash +# Set environment variables +export VESPERA_TITLE="My Production API" +export VESPERA_VERSION="2.0.0" +export VESPERA_DOCS_URL="/api-docs" +``` + +```rust +// These will use the environment variables as defaults +let app = vespera!( + openapi = "openapi.json" +); +``` + ### `#[route]` Attribute Macro Specify HTTP method and path for handler functions. diff --git a/crates/vespera_macro/src/lib.rs b/crates/vespera_macro/src/lib.rs index 324c7bd..4da5409 100644 --- a/crates/vespera_macro/src/lib.rs +++ b/crates/vespera_macro/src/lib.rs @@ -175,11 +175,17 @@ impl Parse for AutoRouterInput { .map(|f| LitStr::new(&f, Span::call_site())) .ok() }), - version: version.or_else(|| { - std::env::var("VESPERA_VERSION") - .map(|f| LitStr::new(&f, Span::call_site())) - .ok() - }), + version: version + .or_else(|| { + std::env::var("VESPERA_VERSION") + .map(|f| LitStr::new(&f, Span::call_site())) + .ok() + }) + .or_else(|| { + std::env::var("CARGO_PKG_VERSION") + .map(|f| LitStr::new(&f, Span::call_site())) + .ok() + }), docs_url: docs_url.or_else(|| { std::env::var("VESPERA_DOCS_URL") .map(|f| LitStr::new(&f, Span::call_site())) diff --git a/examples/axum-example/openapi.json b/examples/axum-example/openapi.json index e0e4fa8..aec3f80 100644 --- a/examples/axum-example/openapi.json +++ b/examples/axum-example/openapi.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "info": { "title": "API", - "version": "1.0.0" + "version": "0.1.0" }, "servers": [ { diff --git a/examples/axum-example/tests/snapshots/integration_test__openapi.snap b/examples/axum-example/tests/snapshots/integration_test__openapi.snap index 045be91..819d8db 100644 --- a/examples/axum-example/tests/snapshots/integration_test__openapi.snap +++ b/examples/axum-example/tests/snapshots/integration_test__openapi.snap @@ -6,7 +6,7 @@ expression: "std::fs::read_to_string(\"openapi.json\").unwrap()" "openapi": "3.1.0", "info": { "title": "API", - "version": "1.0.0" + "version": "0.1.0" }, "servers": [ { diff --git a/openapi.json b/openapi.json index e0e4fa8..aec3f80 100644 --- a/openapi.json +++ b/openapi.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "info": { "title": "API", - "version": "1.0.0" + "version": "0.1.0" }, "servers": [ {