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

1
count/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
target

1681
count/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

9
count/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "count"
version = "0.1.0"
edition = "2021"
[dependencies]
poem = "3"
poem-openapi = { version = "5", features = ["swagger-ui"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

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;
}