From 101af42d8cad820d148c9cbc6ca21778b6b962d1 Mon Sep 17 00:00:00 2001 From: Gaurav Saini Date: Mon, 2 Aug 2021 18:57:22 +0530 Subject: [PATCH] Added fn-fdk-runtime header to response --- Cargo.toml | 6 +++++- images/init/template/src/main.rs | 4 ++-- src/build.rs | 3 +++ src/utils.rs | 18 ++++++++++++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 src/build.rs diff --git a/Cargo.toml b/Cargo.toml index 0396692..3ba5523 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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.10", features = ["net"] } futures = "0.3" object-pool = "0.5" lazy_static = "1" @@ -27,3 +28,6 @@ serde_plain = "0.3" serde_urlencoded = "0.7" clap = "2" thiserror = "1" + +[build-dependencies] +built = "0.5.1" diff --git a/images/init/template/src/main.rs b/images/init/template/src/main.rs index d9abcd2..e100634 100644 --- a/images/init/template/src/main.rs +++ b/images/init/template/src/main.rs @@ -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 {}!", diff --git a/src/build.rs b/src/build.rs new file mode 100644 index 0000000..d8f91cb --- /dev/null +++ b/src/build.rs @@ -0,0 +1,3 @@ +fn main() { + built::write_built_file().expect("Failed to acquire build-time information"); +} diff --git a/src/utils.rs b/src/utils.rs index 174e422..8f28423 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,8 +3,22 @@ use hyper::{ header::{HeaderName, HeaderValue}, Body, HeaderMap, Response, StatusCode, }; +use lazy_static::lazy_static; use std::str::FromStr; +pub mod built_info { + include!(concat!(env!("OUT_DIR"), "/built.rs")); +} + +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); @@ -19,6 +33,10 @@ fn generic_response(status: StatusCode, body: Option, headers: HeaderMap) "Fn-Fdk-Version", HeaderValue::from_str(&format!("fdk-rust/{}", crate_version!())).unwrap(), ); + headers.insert( + "Fn-Fdk-Runtime", + HeaderValue::from_str(&format!("rustc/{}", *COMPILER_VERSION)).unwrap(), + ); let resp_headers = builder.headers_mut().unwrap(); *resp_headers = headers; }