|
| 1 | +--- |
| 2 | +name: vespera-api-development |
| 3 | +description: Guide for developing fully automated OpenAPI/Axum APIs using the Vespera framework. |
| 4 | +--- |
| 5 | + |
| 6 | +# Vespera API Development |
| 7 | + |
| 8 | +Vespera is a fully automated OpenAPI engine for Axum with zero-config route and schema discovery. |
| 9 | +It allows you to write Axum APIs and automatically generate OpenAPI 3.1 specifications. |
| 10 | + |
| 11 | +## Instructions |
| 12 | + |
| 13 | +### 1. Project Setup |
| 14 | +Add `vespera` to your dependencies: |
| 15 | +```toml |
| 16 | +[dependencies] |
| 17 | +vespera = "0.1.0" |
| 18 | +axum = "0.8" |
| 19 | +tokio = { version = "1", features = ["full"] } |
| 20 | +serde = { version = "1", features = ["derive"] } |
| 21 | +``` |
| 22 | + |
| 23 | +### 2. Main Application |
| 24 | +Use the `vespera!` macro in `main.rs` to initialize the app. This macro automatically scans your routes. |
| 25 | + |
| 26 | +```rust |
| 27 | +use vespera::vespera; |
| 28 | + |
| 29 | +#[tokio::main] |
| 30 | +async fn main() { |
| 31 | + // Basic initialization |
| 32 | + let app = vespera!( |
| 33 | + openapi = "openapi.json", // Optional: Generate OpenAPI spec |
| 34 | + title = "My API", // Optional: API Title |
| 35 | + version = "1.0.0", // Optional: API Version |
| 36 | + docs_url = "/docs" // Optional: Serve Swagger UI |
| 37 | + ); |
| 38 | + |
| 39 | + let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); |
| 40 | + axum::serve(listener, app).await.unwrap(); |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +### 3. Route Handlers |
| 45 | +Define routes in modules (e.g., `src/routes/`). Use the `#[vespera::route]` attribute. |
| 46 | + |
| 47 | +```rust |
| 48 | +use axum::{Json, Path, State}; |
| 49 | +use serde::{Deserialize, Serialize}; |
| 50 | + |
| 51 | +#[derive(Serialize, Deserialize)] |
| 52 | +pub struct User { |
| 53 | + pub id: u32, |
| 54 | + pub name: String, |
| 55 | +} |
| 56 | + |
| 57 | +// GET /users/{id} |
| 58 | +#[vespera::route(path = "/{id}")] // Defaults to GET if not specified |
| 59 | +pub async fn get_user(Path(id): Path<u32>) -> Json<User> { |
| 60 | + Json(User { id, name: "Alice".into() }) |
| 61 | +} |
| 62 | + |
| 63 | +// POST /users |
| 64 | +#[vespera::route(post)] |
| 65 | +pub async fn create_user(Json(user): Json<User>) -> Json<User> { |
| 66 | + Json(user) |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### 5. Error Handling |
| 71 | +Vespera supports `Result<T, E>` return types. It automatically documents both the success capability (200 OK) and the error responses in the OpenAPI spec. |
| 72 | + |
| 73 | +```rust |
| 74 | +use axum::http::StatusCode; |
| 75 | + |
| 76 | +#[derive(Serialize, Deserialize, Schema)] |
| 77 | +pub struct ErrorResponse { |
| 78 | + pub message: String, |
| 79 | +} |
| 80 | + |
| 81 | +// Result<Json<Success>, Json<Error>> |
| 82 | +#[vespera::route(get, path = "/may-fail")] |
| 83 | +pub async fn may_fail() -> Result<Json<User>, Json<ErrorResponse>> { |
| 84 | + Err(Json(ErrorResponse { message: "Something went wrong".to_string() })) |
| 85 | +} |
| 86 | + |
| 87 | +// Result<Json<Success>, (StatusCode, Json<Error>)> |
| 88 | +#[vespera::route(get, path = "/not-found")] |
| 89 | +pub async fn not_found_example() -> Result<Json<User>, (StatusCode, Json<ErrorResponse>)> { |
| 90 | + Err(( |
| 91 | + StatusCode::NOT_FOUND, |
| 92 | + Json(ErrorResponse { message: "User not found".to_string() }) |
| 93 | + )) |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### 4. File Structure |
| 98 | +The file structure in `src/routes` maps to URL paths: |
| 99 | +- `src/routes/users.rs` -> `/users` |
| 100 | +- `src/routes/admin/stats.rs` -> `/admin/stats` |
| 101 | + |
| 102 | +## Guidelines |
| 103 | +- Always use `vespera!` macro instead of manually building `Router::new()`. |
| 104 | +- Use `#[vespera::route]` for all handlers to ensure they are picked up by the OpenAPI generator. |
| 105 | +- `dir` parameter in `vespera!` defaults to `"routes"`. |
| 106 | +- Handlers and schemas are automatically imported. |
0 commit comments