now with dynamic counter too
This commit is contained in:
1
count/Cargo.lock
generated
1
count/Cargo.lock
generated
@@ -219,6 +219,7 @@ checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
|
||||
name = "count"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"http",
|
||||
"poem",
|
||||
"poem-openapi",
|
||||
"tokio",
|
||||
|
||||
@@ -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"] }
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user