This commit is contained in:
kristof 2025-04-03 13:59:40 +02:00
parent 1364db3f94
commit 4a65f8255a
2 changed files with 93 additions and 1 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
target/
server
*.wasm
herovm_build/

View File

@ -0,0 +1,92 @@
You're a Rust developer assistant.
Please generate a complete Rust web server using `poem` and `poem-openapi`. It should:
1. Create a basic `#[OpenApi]` interface.
2. Automatically generate and serve an OpenAPI 3.0 spec at `/api/openapi.json`.
3. Serve a Swagger UI interface at `/docs`.
4. Expose a function I define in another module (e.g., `my_logic::do_something()`).
5. The endpoint should be `/do` (HTTP GET) and return a JSON response.
Use these crates:
- `poem`
- `poem-openapi`
- `tokio`
- `uuid` (optional, if needed)
We want to create crud and also other methods of herodb/src/zaz/models
To see how we call this model logic see herodb/src/zaz/cmd/examples.rs
And use it inside the API handler.
The project should have:
- `main.rs` (entry point)
- `sale.rs` (logic per file e.g. sale, product, ...)
- Clear type annotations and minimal boilerplate.
```
---
## 🧪 Example Output from AI Based on That Prompt
### `Cargo.toml`
```toml
[package]
name = "poem-api-server"
version = "0.1.0"
edition = "2021"
[dependencies]
poem = "1"
poem-openapi = "3"
tokio = { version = "1", features = ["full"] }
```
### `src/main.rs`
```rust
use poem::{listener::TcpListener, Route, Server};
use poem_openapi::{payload::Json, OpenApi, OpenApiService};
mod my_logic;
struct Api;
#[OpenApi]
impl Api {
#[oai(path = "/do", method = "get")]
async fn do_action(&self) -> Json<String> {
Json(my_logic::do_something())
}
}
#[tokio::main]
async fn main() {
let api_service =
OpenApiService::new(Api, "My API", "1.0").server("http://localhost:3000/api");
let ui = api_service.swagger_ui();
let app = Route::new()
.nest("/api", api_service)
.nest("/docs", ui);
Server::new(TcpListener::bind("127.0.0.1:3000"))
.run(app)
.await
.unwrap();
}
```
---
### ✅ Result
- Open `/api/do` → Calls your logic and returns a JSON response.
- Open `/docs` → Interactive Swagger UI
- Open `/api/openapi.json` → Full OpenAPI spec
---