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
//! JSON API Example
//!
//! This example shows how to build a JSON API with Product OS Server.

use product_os_server::{ProductOSServer, StatusCode, Json};
use product_os_server::ServerConfig;
use product_os_async_executor::TokioExecutor;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone)]
struct User {
    id: u32,
    name: String,
    email: String,
}

#[derive(Deserialize)]
struct CreateUserRequest {
    name: String,
    email: String,
}

async fn list_users() -> Result<Json<Vec<User>>, StatusCode> {
    // In a real app, this would query a database
    let users = vec![
        User {
            id: 1,
            name: "Alice".to_string(),
            email: "alice@example.com".to_string(),
        },
        User {
            id: 2,
            name: "Bob".to_string(),
            email: "bob@example.com".to_string(),
        },
    ];
    
    Ok(Json(users))
}

async fn get_user() -> Result<Json<User>, StatusCode> {
    // In a real app, this would extract the ID from the path
    // and query the database
    Ok(Json(User {
        id: 1,
        name: "Alice".to_string(),
        email: "alice@example.com".to_string(),
    }))
}

async fn create_user(Json(req): Json<CreateUserRequest>) -> Result<Json<User>, StatusCode> {
    // In a real app, this would insert into a database
    let new_user = User {
        id: 3,
        name: req.name,
        email: req.email,
    };
    
    Ok(Json(new_user))
}

async fn update_user(Json(user): Json<User>) -> Result<Json<User>, StatusCode> {
    // In a real app, this would update the database
    Ok(Json(user))
}

async fn delete_user() -> Result<StatusCode, StatusCode> {
    // In a real app, this would delete from database
    Ok(StatusCode::NO_CONTENT)
}

#[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;

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

    // Add routes
    server.add_get("/api/users", list_users);
    server.add_get("/api/users/1", get_user);
    server.add_post("/api/users", create_user);
    server.add_post("/api/users/1", update_user);
    
    use product_os_server::Method;
    server.add_handler("/api/users/1", Method::DELETE, delete_user);

    println!("JSON API listening on http://localhost:3000");
    println!("Try:");
    println!("  curl http://localhost:3000/api/users");
    println!("  curl http://localhost:3000/api/users/1");
    println!("  curl -X POST -H 'Content-Type: application/json' -d '{{\"name\":\"Charlie\",\"email\":\"charlie@example.com\"}}' http://localhost:3000/api/users");

    // Start server
    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;
    }
}