use product_os_server::{ProductOSServer, StatusCode, Response, Json, Body};
use product_os_server::ServerConfig;
use product_os_async_executor::TokioExecutor;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct HelloResponse {
message: String,
}
async fn root_handler() -> Result<Response<Body>, StatusCode> {
Ok(Response::builder()
.status(StatusCode::OK)
.body(Body::from("Welcome to Product OS Server!"))
.unwrap())
}
async fn hello_handler() -> Result<Json<HelloResponse>, StatusCode> {
Ok(Json(HelloResponse {
message: "Hello from Product OS Server!".to_string(),
}))
}
async fn echo_handler(body: String) -> Result<String, StatusCode> {
Ok(format!("You said: {}", body))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let mut config = ServerConfig::new();
config.network.port = 3000;
config.network.host = "localhost".to_string();
println!("Creating server...");
let mut server: ProductOSServer<(), TokioExecutor, _> =
ProductOSServer::new_with_config(config);
server.add_get("/", root_handler);
server.add_get("/hello", hello_handler);
server.add_post("/echo", echo_handler);
println!("Server listening on http://localhost:3000");
println!("Try:");
println!(" curl http://localhost:3000/");
println!(" curl http://localhost:3000/hello");
println!(" curl -X POST -d 'test message' http://localhost:3000/echo");
server.start(false).await.map_err(|e| format!("Server error: {}", e))?;
loop {
tokio::time::sleep(std::time::Duration::from_secs(3600)).await;
}
}