Skip to main content

HTTP_LIB_RS

Constant HTTP_LIB_RS 

Source
pub const HTTP_LIB_RS: &str = "use spin_sdk::http::{IntoResponse, Response};\nuse spin_sdk::http_component;\n\n/// A simple Spin HTTP component.\n#[http_component]\nfn hello_world(req: http::Request<()>) -> anyhow::Result<impl IntoResponse> {\n    // Extract query parameters\n    let query = req.uri().query().unwrap_or(\"\");\n    let name = query\n        .split(\'&\')\n        .find_map(|pair| {\n            let mut parts = pair.split(\'=\');\n            if parts.next() == Some(\"name\") {\n                parts.next()\n            } else {\n                None\n            }\n        })\n        .unwrap_or(\"World\");\n\n    // Extract path from the URL\n    let path = req.uri().path();\n\n    // Get user agent\n    let user_agent = req\n        .headers()\n        .get(http::header::USER_AGENT)\n        .map(|h| h.to_str().unwrap_or(\"Unknown\"))\n        .unwrap_or(\"Unknown\");\n\n    // Build response with HTML\n    let html = format!(\n        r#\"<!DOCTYPE html>\n<html>\n<head>\n    <title>Faasta HTTP Example</title>\n    <style>\n        body {{ font-family: Arial, sans-serif; margin: 40px; line-height: 1.6; }}\n        h1 {{ color: #333; }}\n        .info {{ background-color: #f5f5f5; padding: 15px; border-radius: 5px; }}\n        .highlight {{ color: #0066cc; font-weight: bold; }}\n    </style>\n</head>\n<body>\n    <h1>Hello, {}!</h1>\n    <div class=\"info\">\n        <p>You accessed path: <span class=\"highlight\">{}</span></p>\n        <p>Your User-Agent: <span class=\"highlight\">{}</span></p>\n        <p>This function is running on Faasta with subdomain routing!</p>\n    </div>\n</body>\n</html>\"#,\n        name, path, user_agent\n    );\n\n    Ok(Response::builder()\n        .status(200)\n        .header(\"content-type\", \"text/html\")\n        .body(html)\n        .build())\n}\n";