{{#if (eq cloud_provider "aws")}}
use lambda_runtime::{service_fn, Error, LambdaEvent};
use serde::{Deserialize, Serialize};
use tracing::{info, Level};
use tracing_subscriber::FmtSubscriber;
/// Event input structure for the Lambda function
#[derive(Deserialize)]
struct Request {
#[serde(default)]
name: String,
}
/// Response structure for the Lambda function
#[derive(Serialize)]
struct Response {
message: String,
request_id: String,
}
/// Main Lambda handler function
async fn function_handler(event: LambdaEvent<Request>) -> Result<Response, Error> {
let (event, context) = event.into_parts();
let request_id = context.request_id;
info!(
message = "Processing serverless function request",
request_id = %request_id,
name = %event.name
);
// Create a response with a greeting using the name from the request or a default
let name = if event.name.is_empty() {
"World".to_string()
} else {
event.name
};
let message = format!("Hello, {}! Welcome to your FerrisUp serverless function.", name);
// Return the formatted response
Ok(Response {
message,
request_id,
})
}
#[tokio::main]
async fn main() -> Result<(), Error> {
// Initialize the tracing subscriber for logging
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber)?;
info!("FerrisUp AWS Lambda function starting");
// Start the Lambda runtime and register the handler
lambda_runtime::run(service_fn(function_handler)).await?;
Ok(())
}
{{/if}}
{{#if (eq cloud_provider "gcp")}}
use http::{Request, Response, StatusCode};
use hyper::{
service::{make_service_fn, service_fn},
Body, Server,
};
use serde::{Deserialize, Serialize};
use std::{convert::Infallible, net::SocketAddr};
use tracing::{info, Level};
use tracing_subscriber::FmtSubscriber;
use uuid::Uuid;
/// Event input structure for the GCP Cloud Function
#[derive(Deserialize)]
struct RequestBody {
#[serde(default)]
name: String,
}
/// Response structure for the GCP Cloud Function
#[derive(Serialize)]
struct ResponseBody {
message: String,
request_id: String,
}
async fn function_handler(req: Request<Body>) -> Result<Response<Body>, Infallible> {
// Parse the request body
let body_bytes = hyper::body::to_bytes(req.into_body()).await.unwrap_or_default();
let request_body: RequestBody = serde_json::from_slice(&body_bytes)
.unwrap_or(RequestBody { name: "".to_string() });
// Create a response with a greeting using the name from the request or a default
let name = if request_body.name.is_empty() {
"World".to_string()
} else {
request_body.name
};
let message = format!("Hello, {}! Welcome to your FerrisUp serverless function.", name);
// Return the formatted response
let response_body = ResponseBody {
message,
request_id: Uuid::new_v4().to_string(),
};
let json = serde_json::to_string(&response_body).unwrap();
let response = Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "application/json")
.body(Body::from(json))
.unwrap();
Ok(response)
}
#[tokio::main]
async fn main() {
// Initialize the tracing subscriber for logging
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber).unwrap();
info!("FerrisUp GCP Cloud Function starting");
// Get the port from the environment or use a default
let port = std::env::var("PORT")
.unwrap_or_else(|_| "8080".to_string())
.parse()
.unwrap_or(8080);
let addr = SocketAddr::from(([0, 0, 0, 0], port));
// Create a service to handle each incoming connection
let make_svc = make_service_fn(|_conn| {
async { Ok::<_, Infallible>(service_fn(function_handler)) }
});
// Create and run the server
let server = Server::bind(&addr).serve(make_svc);
info!("Server listening on {}", addr);
// Run the server
if let Err(e) = server.await {
eprintln!("Server error: {}", e);
}
}
{{/if}}
{{#if (eq cloud_provider "azure")}}
use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};
use serde::{Deserialize, Serialize};
use std::convert::Infallible;
use std::net::SocketAddr;
use tracing::{info, Level};
use tracing_subscriber::FmtSubscriber;
use uuid::Uuid;
/// Event input structure for the Azure Function
#[derive(Deserialize)]
struct RequestBody {
#[serde(default)]
name: String,
}
/// Response structure for the Azure Function
#[derive(Serialize)]
struct ResponseBody {
message: String,
request_id: String,
}
/// Handler function for HTTP requests
async fn function_handler(req: Request<Body>) -> Result<Response<Body>, Infallible> {
// Generate a unique request ID
let request_id = Uuid::new_v4().to_string();
info!("Processing request {}", request_id);
// Parse the request body
let body_bytes = hyper::body::to_bytes(req.into_body()).await.unwrap_or_default();
let request_body: RequestBody = serde_json::from_slice(&body_bytes)
.unwrap_or(RequestBody { name: "".to_string() });
// Create a response with a greeting using the name from the request or a default
let name = if request_body.name.is_empty() {
"World".to_string()
} else {
request_body.name
};
let message = format!("Hello, {}! Welcome to your FerrisUp Azure Function.", name);
// Return the formatted response
let response_body = ResponseBody {
message,
request_id,
};
let response_json = serde_json::to_string(&response_body).unwrap();
Ok(Response::builder()
.status(200)
.header("Content-Type", "application/json")
.body(Body::from(response_json))
.unwrap())
}
#[tokio::main]
async fn main() {
// Initialize the tracing subscriber for logging
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber).unwrap();
info!("FerrisUp Azure Function starting");
// Get the port from the environment variable or use a default
let port = std::env::var("FUNCTIONS_CUSTOMHANDLER_PORT")
.unwrap_or_else(|_| "3000".to_string())
.parse::<u16>()
.unwrap_or(3000);
let addr = SocketAddr::from(([0, 0, 0, 0], port));
// Create a service to handle each incoming connection
let make_svc = make_service_fn(|_conn| {
async { Ok::<_, Infallible>(service_fn(function_handler)) }
});
let server = Server::bind(&addr).serve(make_svc);
info!("Listening on http://{}", addr);
// Run the server
if let Err(e) = server.await {
eprintln!("server error: {}", e);
}
}
{{/if}}
{{#if (eq cloud_provider "vercel")}}
use serde::{Deserialize, Serialize};
use tracing::{info, Level};
use tracing_subscriber::FmtSubscriber;
use vercel_runtime::{run, Body, Error, Request, Response, StatusCode};
/// Event input structure for the Vercel Function
#[derive(Deserialize)]
struct RequestBody {
#[serde(default)]
name: String,
}
/// Response structure for the Vercel Function
#[derive(Serialize)]
struct ResponseBody {
message: String,
request_id: String,
}
#[tokio::main]
async fn main() -> Result<(), Error> {
run(handler).await
}
async fn handler(req: Request) -> Result<Response<Body>, Error> {
// Initialize the tracing subscriber for logging
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber)?;
info!("FerrisUp Vercel Function starting");
// Parse the request body
let request_body = match req.body() {
Body::Text(text) => serde_json::from_str::<RequestBody>(text).unwrap_or(RequestBody { name: "".to_string() }),
_ => RequestBody { name: "".to_string() },
};
// Create a response with a greeting using the name from the request or a default
let name = if request_body.name.is_empty() {
"World".to_string()
} else {
request_body.name
};
let message = format!("Hello, {}! Welcome to your FerrisUp serverless function.", name);
// Return the formatted response
let response_body = ResponseBody {
message,
request_id: req.headers().get("x-vercel-id").map_or("unknown", |v| v.to_str().unwrap_or("unknown")).to_string(),
};
Ok(Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "application/json")
.body(Body::from(serde_json::to_string(&response_body)?))?)
}
{{/if}}
{{#if (eq cloud_provider "netlify")}}
use netlify_lambda_http::{
lambda::{self, Context},
IntoResponse, Request, Response
};
use aws_lambda_events::encodings::Body;
use serde::{Deserialize, Serialize};
use tracing::{info, Level};
use tracing_subscriber::FmtSubscriber;
/// Event input structure for the Netlify Function
#[derive(Deserialize)]
struct RequestBody {
#[serde(default)]
name: String,
}
/// Response structure for the Netlify Function
#[derive(Serialize)]
struct ResponseBody {
message: String,
request_id: String,
}
#[lambda::lambda(http)]
#[tokio::main]
async fn main(req: Request, ctx: Context) -> Result<impl IntoResponse, Box<dyn std::error::Error + Send + Sync>> {
// Initialize the tracing subscriber for logging
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber)?;
info!("FerrisUp Netlify Function starting");
// Parse the request body
let body = req.body();
let request_body = match body {
Body::Text(text) => {
serde_json::from_str::<RequestBody>(text).unwrap_or(RequestBody { name: "".to_string() })
},
Body::Binary(binary) => {
serde_json::from_slice::<RequestBody>(binary).unwrap_or(RequestBody { name: "".to_string() })
},
_ => RequestBody { name: "".to_string() },
};
// Create a response with a greeting using the name from the request or a default
let name = if request_body.name.is_empty() {
"World".to_string()
} else {
request_body.name
};
let message = format!("Hello, {}! Welcome to your FerrisUp serverless function.", name);
// Return the formatted response
let response_body = ResponseBody {
message,
request_id: ctx.request_id,
};
Ok(Response::builder()
.status(200)
.header("Content-Type", "application/json")
.body(serde_json::to_string(&response_body)?)?)
}
{{/if}}