Skip to content

Instantly share code, notes, and snippets.

@robertohuertasm
Created December 23, 2018 02:23
Show Gist options
  • Save robertohuertasm/6b42cb867154768810b9817ce53f8345 to your computer and use it in GitHub Desktop.
Save robertohuertasm/6b42cb867154768810b9817ce53f8345 to your computer and use it in GitHub Desktop.
azure-function-rust
use azure_functions::func;
use azure_functions::bindings::{HttpRequest, HttpResponse};
use serde_derive::Deserialize;
// Struct that will hold information about the body of the request.
#[derive(Deserialize)]
pub struct Body {
name: String,
}
// The func attribute marks this fn as the function to be used by Azure Functions.
#[func]
// See https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings#supported-bindings
// See also https://github.com/peterhuene/azure-functions-rs/blob/master/README.md#azure-functions-bindings
#[binding(name="request", auth_level="anonymous")]
// The function will just check for a name parameter in the querystring
// or for a JSON Body structure in the body of the request.
pub fn hello(request: &HttpRequest) -> HttpResponse {
// Logs the request on the Azure Functions Host.
info!("Request: {:?}", request);
// checking the query string
if let Some(name) = request.query_params().get("name") {
return format!("Hello from Rust, my dear {}! (qs)", name).into();
}
// checking the body
if let Ok(body) = request.body().as_json::<Body>() {
return format!("Hello from Rust, my dear {}! (body)", body.name).into();
}
"Hello from Rust, my dear default user with no params!".into()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment