product-os-server 0.0.55

Product OS : Server provides a full functioning advanced server capable of acting as a web server, command and control distributed network, authentication server, crawling server and more. Fully featured with high level of flexibility.
Documentation
//! Basic HTTP Server Example
//!
//! This example demonstrates how to create a simple HTTP server
//! with a few routes.

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>> {
    // Initialize tracing
    tracing_subscriber::fmt::init();

    // Create configuration
    let mut config = ServerConfig::new();
    config.network.port = 3000;
    config.network.host = "localhost".to_string();

    println!("Creating server...");
    
    // Create server
    let mut server: ProductOSServer<(), TokioExecutor, _> = 
        ProductOSServer::new_with_config(config);

    // Add routes
    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");

    // Start server (non-blocking)
    server.start(false).await.map_err(|e| format!("Server error: {}", e))?;

    // Keep the main thread alive
    loop {
        tokio::time::sleep(std::time::Duration::from_secs(3600)).await;
    }
}