Skip to content

Commit

Permalink
Added fn-fdk-runtime header to response
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav Saini authored and metamemelord committed Aug 12, 2021
1 parent 7b7d5cd commit d3f008f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 32 deletions.
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ repository = "https://github.com/fnproject/fdk-rust"
keywords = ["Fn", "serverless", "FaaS"]
categories = ["web-programming", "development-tools"]
license = "Apache-2.0"
build = "src/build.rs"

[badges]
maintenance = { status = "experimental" }

[dependencies]
hyper = { version = "0.14", features = ["full"] }
tokio = { version = "1.6", features = ["net"] }
tokio = { version = "1.8", features = ["net"] }
futures = "0.3"
object-pool = "0.5"
lazy_static = "1"
Expand All @@ -27,3 +28,9 @@ serde_plain = "0.3"
serde_urlencoded = "0.7"
clap = "2"
thiserror = "1"

[build-dependencies]
built = "0.5.1"
# rustc_version = "0.2.3"
# rustc_version_runtime = "0.2.0"
# semver = "0.9"
4 changes: 2 additions & 2 deletions images/init/template/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use fdk::{Function, FunctionError, RuntimeContext};
use fdk::{Function, FunctionError, Result, RuntimeContext};
use tokio;

#[tokio::main]
async fn main() -> Result<(), FunctionError> {
async fn main() -> Result<()> {
if let Err(e) = Function::run(|_: &mut RuntimeContext, i: String| {
Ok(format!(
"Hello {}!",
Expand Down
3 changes: 3 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
built::write_built_file().expect("Failed to acquire build-time information");
}
6 changes: 4 additions & 2 deletions src/coercions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use serde::{Deserialize, Serialize};

/// ContentType represents the supported content types in the FDK.
#[derive(Clone, Debug)]
pub enum ContentType {
JSON,
pub
enum ContentType
{
JSON,
YAML,
XML,
Plain,
Expand Down
8 changes: 4 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ pub struct RuntimeContext {
response_status_code: Option<StatusCode>,
}

fn resolve_content_type(v: Option<&hyper::header::HeaderValue>) -> ContentType {
match v {
fn resolve_content_type(v: Option<&hyper::header::HeaderValue>) -> ContentType{
match v {
Some(value) => ContentType::from_str(value.to_str().unwrap_or("")),
None => ContentType::JSON,
}
}

fn get_accept_header_value(headers: &hyper::HeaderMap) -> Option<&HeaderValue> {
if headers.get("Fn-Http-H-Accept").is_some() {
fn get_accept_header_value(headers: &hyper::HeaderMap) -> Option<&HeaderValue>{
if headers.get("Fn-Http-H-Accept").is_some() {
headers.get("Fn-Http-H-Accept")
} else if headers.get(hyper::header::ACCEPT).is_some() {
headers.get(hyper::header::ACCEPT)
Expand Down
64 changes: 41 additions & 23 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,53 @@ use hyper::{
header::{HeaderName, HeaderValue},
Body, HeaderMap, Response, StatusCode,
};
use lazy_static::lazy_static;
use std::str::FromStr;

pub fn make_header_map_with_single_value(key: HeaderName, value: HeaderValue) -> HeaderMap {
let mut header_map = HeaderMap::new();
header_map.insert(key, value);
header_map
pub mod built_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}

fn generic_response(status: StatusCode, body: Option<Body>, headers: HeaderMap) -> Response<Body> {
let mut builder = Response::builder().status(status);
{
let mut headers = headers;
headers.insert(
lazy_static! {
static ref COMPILER_VERSION: String = built_info::RUSTC_VERSION
.split_ascii_whitespace()
.skip(1)
.next()
.map(|s| s.to_owned())
.unwrap_or_else(|| String::from("UNKNOWN"));
}

pub fn make_header_map_with_single_value(key: HeaderName, value: HeaderValue) -> HeaderMap{
let mut header_map = HeaderMap::new();
header_map.insert(key, value);
header_map
}

fn generic_response(status: StatusCode, body: Option<Body>, headers: HeaderMap) -> Response<Body>{
let mut builder = Response::builder().status(status);
{
let mut headers = headers;
headers.insert(
"Fn-Fdk-Version",
HeaderValue::from_str(&format!("fdk-rust/{}", crate_version!())).unwrap(),
);
let resp_headers = builder.headers_mut().unwrap();
*resp_headers = headers;
}
headers.insert(
"Fn-Fdk-Runtime",
HeaderValue::from_str(&format!("rustc/{}", *COMPILER_VERSION)).unwrap(),
);
let resp_headers = builder.headers_mut().unwrap();
*resp_headers = headers;
}

let mut response_body = Body::empty();
if let Some(body) = body {
let mut response_body = Body::empty();
if let Some(body) = body {
response_body = body;
}
builder.body(response_body).unwrap()
builder.body(response_body).unwrap()
}

fn add_status_header(header: Option<HeaderMap>, status: StatusCode) -> HeaderMap {
header
fn add_status_header(header: Option<HeaderMap>, status: StatusCode) -> HeaderMap{
header
.map(|mut hdrs| {
hdrs.insert(
HeaderName::from_str("Fn-Http-Status").unwrap(),
Expand All @@ -51,16 +69,16 @@ pub fn success_or_recoverable_error(
status: StatusCode,
body: Option<Body>,
headers: Option<HeaderMap>,
) -> Response<Body> {
let response_headers = add_status_header(headers, status);
generic_response(StatusCode::OK, body, response_headers)
) -> Response<Body>{
let response_headers = add_status_header(headers, status);
generic_response(StatusCode::OK, body, response_headers)
}

pub fn unrecoverable_error(
status: StatusCode,
body: Option<Body>,
headers: Option<HeaderMap>,
) -> Response<Body> {
let response_headers = add_status_header(headers, status);
generic_response(StatusCode::BAD_GATEWAY, body, response_headers)
) -> Response<Body>{
let response_headers = add_status_header(headers, status);
generic_response(StatusCode::BAD_GATEWAY, body, response_headers)
}

0 comments on commit d3f008f

Please sign in to comment.