rust backend
This commit is contained in:
1
count/.gitignore
vendored
Normal file
1
count/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
target
|
||||||
1681
count/Cargo.lock
generated
Normal file
1681
count/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
9
count/Cargo.toml
Normal file
9
count/Cargo.toml
Normal 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
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