rust backend

This commit is contained in:
2025-01-10 17:17:04 -05:00
parent 9ce0cb4ff6
commit a745239d16
4 changed files with 1738 additions and 0 deletions

47
count/src/main.rs Normal file
View 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;
}