rust backend
This commit is contained in:
47
count/src/main.rs
Normal file
47
count/src/main.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
use poem_openapi::payload::PlainText;
|
||||
use poem_openapi::{OpenApi, OpenApiService};
|
||||
use poem::{Route, Server};
|
||||
use poem::listener::TcpListener;
|
||||
|
||||
pub struct CounterApi {
|
||||
counter: AtomicU32,
|
||||
}
|
||||
|
||||
|
||||
#[OpenApi]
|
||||
impl CounterApi {
|
||||
pub fn new() -> Self {
|
||||
CounterApi {
|
||||
counter: AtomicU32::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
#[oai(path = "/next", method = "get")]
|
||||
pub async fn next(&self) -> PlainText<String> {
|
||||
let next = self.counter.fetch_add(1, Ordering::Relaxed);
|
||||
|
||||
PlainText(next.to_string())
|
||||
}
|
||||
|
||||
#[oai(path = "/next_json", method = "get")]
|
||||
pub async fn next_json(&self) -> PlainText<String> {
|
||||
let next = self.counter.fetch_add(1, Ordering::Relaxed);
|
||||
|
||||
PlainText(format!("{{\"count\":{next}}}"))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
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);
|
||||
|
||||
Server::new(TcpListener::bind("127.0.0.1:9000"))
|
||||
.run(app)
|
||||
.await;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user