now with dynamic counter too

This commit is contained in:
2025-01-10 18:09:32 -05:00
parent a745239d16
commit 987baf1ad6
8 changed files with 103 additions and 5 deletions

1
count/Cargo.lock generated
View File

@@ -219,6 +219,7 @@ checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
name = "count"
version = "0.1.0"
dependencies = [
"http",
"poem",
"poem-openapi",
"tokio",

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
http = "1.2.0"
poem = "3"
poem-openapi = { version = "5", features = ["swagger-ui"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

View File

@@ -1,9 +1,10 @@
use std::sync::atomic::{AtomicU32, Ordering};
use poem_openapi::payload::PlainText;
use poem_openapi::payload::{Response, PlainText};
use poem_openapi::{OpenApi, OpenApiService};
use poem::{Route, Server};
use poem::{EndpointExt, Route, Server};
use poem::listener::TcpListener;
use poem::middleware::Cors;
pub struct CounterApi {
counter: AtomicU32,
@@ -18,6 +19,7 @@ impl CounterApi {
}
}
#[oai(path = "/next", method = "get")]
pub async fn next(&self) -> PlainText<String> {
let next = self.counter.fetch_add(1, Ordering::Relaxed);
@@ -31,6 +33,7 @@ impl CounterApi {
PlainText(format!("{{\"count\":{next}}}"))
}
}
#[tokio::main]
@@ -38,7 +41,7 @@ async fn main() {
let api_service =
OpenApiService::new(CounterApi::new(), "counter", "1.0").server("http://localhost:9000");
let ui = api_service.swagger_ui();
let app = Route::new().nest("/", api_service).nest("/docs", ui);
let app = Route::new().nest("/", api_service).nest("/docs", ui).with(Cors::new());
Server::new(TcpListener::bind("127.0.0.1:9000"))
.run(app)