mirror of https://github.com/knative/func.git
feat: Add proper example of configuring Rust functions. (#436)
This commit is contained in:
parent
435d1ac2a3
commit
7656c40972
1
go.sum
1
go.sum
|
|
@ -509,7 +509,6 @@ github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBt
|
|||
github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE=
|
||||
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
|
||||
github.com/hashicorp/consul/sdk v0.3.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
|
||||
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
|
||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
|
||||
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
|
||||
|
|
|
|||
|
|
@ -21,6 +21,20 @@ use actix_web::web;
|
|||
/// ) -> Result<Event, actix_web::Error> {
|
||||
/// Ok(Event::default())
|
||||
/// }
|
||||
pub fn configure(_cfg: &mut web::ServiceConfig) {
|
||||
pub fn configure(cfg: &mut web::ServiceConfig) {
|
||||
log::info!("Configuring service");
|
||||
cfg.data(HandlerConfig::default());
|
||||
}
|
||||
|
||||
/// An example of the function configuration structure.
|
||||
pub struct HandlerConfig {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl Default for HandlerConfig {
|
||||
fn default() -> HandlerConfig {
|
||||
HandlerConfig {
|
||||
name: String::from("world"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,21 @@
|
|||
use crate::config::HandlerConfig;
|
||||
use actix_web::web;
|
||||
use cloudevents::{event::Data, Event, EventBuilder, EventBuilderV10};
|
||||
use log::info;
|
||||
use serde_json::{from_slice, from_str, json};
|
||||
|
||||
// Implement your function's logic here
|
||||
pub async fn handle(event: Event) -> Result<Event, actix_web::Error> {
|
||||
pub async fn handle(
|
||||
event: Event,
|
||||
config: web::Data<HandlerConfig>,
|
||||
) -> Result<Event, actix_web::Error> {
|
||||
info!("event: {}", event);
|
||||
|
||||
let input = match event.data() {
|
||||
Some(Data::Binary(v)) => from_slice(v)?,
|
||||
Some(Data::String(v)) => from_str(v)?,
|
||||
Some(Data::Json(v)) => v.to_owned(),
|
||||
None => json!({ "name": "world" }),
|
||||
None => json!({ "name": config.name }),
|
||||
};
|
||||
|
||||
EventBuilderV10::from(event)
|
||||
|
|
@ -25,11 +30,15 @@ pub async fn handle(event: Event) -> Result<Event, actix_web::Error> {
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn config() -> web::Data<HandlerConfig> {
|
||||
web::Data::new(HandlerConfig::default())
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn valid_input() {
|
||||
let mut input = Event::default();
|
||||
input.set_data("application/json", json!({"name": "bootsy"}));
|
||||
let resp = handle(input).await;
|
||||
let resp = handle(input, config()).await;
|
||||
assert!(resp.is_ok());
|
||||
match resp.unwrap().data() {
|
||||
Some(Data::Json(output)) => assert_eq!("bootsy", output["hello"]),
|
||||
|
|
@ -39,7 +48,7 @@ mod tests {
|
|||
|
||||
#[actix_rt::test]
|
||||
async fn no_input() {
|
||||
let resp = handle(Event::default()).await;
|
||||
let resp = handle(Event::default(), config()).await;
|
||||
assert!(resp.is_ok());
|
||||
match resp.unwrap().data() {
|
||||
Some(Data::Json(output)) => assert_eq!("world", output["hello"]),
|
||||
|
|
|
|||
|
|
@ -21,6 +21,21 @@ use actix_web::web;
|
|||
/// ) -> HttpResponse {
|
||||
/// HttpResponse::NoContent()
|
||||
/// }
|
||||
pub fn configure(_cfg: &mut web::ServiceConfig) {
|
||||
pub fn configure(cfg: &mut web::ServiceConfig) {
|
||||
log::info!("Configuring service");
|
||||
cfg.data(HandlerConfig::default());
|
||||
}
|
||||
|
||||
/// An example of the function configuration structure.
|
||||
#[derive(Clone)]
|
||||
pub struct HandlerConfig {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl Default for HandlerConfig {
|
||||
fn default() -> HandlerConfig {
|
||||
HandlerConfig {
|
||||
name: String::from("world"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
use actix_web::{http::Method, HttpRequest, HttpResponse};
|
||||
use crate::config::HandlerConfig;
|
||||
use actix_web::{http::Method, web, HttpRequest, HttpResponse};
|
||||
use log::info;
|
||||
|
||||
// Implement your function's logic here
|
||||
pub async fn index(req: HttpRequest) -> HttpResponse {
|
||||
pub async fn index(req: HttpRequest, config: web::Data<HandlerConfig>) -> HttpResponse {
|
||||
info!("{:#?}", req);
|
||||
if req.method() == Method::GET {
|
||||
HttpResponse::Ok().body("Hello!\n")
|
||||
HttpResponse::Ok().body(format!("Hello {}!\n", config.name))
|
||||
} else {
|
||||
HttpResponse::Ok().body("Thanks!\n")
|
||||
HttpResponse::Ok().body(format!("Thanks {}!\n", config.name))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -16,19 +17,29 @@ mod tests {
|
|||
use super::*;
|
||||
use actix_web::{body::Body, http, test};
|
||||
|
||||
fn config() -> web::Data<HandlerConfig> {
|
||||
web::Data::new(HandlerConfig::default())
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn get() {
|
||||
let req = test::TestRequest::get().to_http_request();
|
||||
let resp = index(req).await;
|
||||
let resp = index(req, config()).await;
|
||||
assert_eq!(resp.status(), http::StatusCode::OK);
|
||||
assert_eq!(&Body::from("Hello!\n"), resp.body().as_ref().unwrap());
|
||||
assert_eq!(
|
||||
&Body::from(format!("Hello {}!\n", "world")),
|
||||
resp.body().as_ref().unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn post() {
|
||||
let req = test::TestRequest::post().to_http_request();
|
||||
let resp = index(req).await;
|
||||
let resp = index(req, config()).await;
|
||||
assert!(resp.status().is_success());
|
||||
assert_eq!(&Body::from("Thanks!\n"), resp.body().as_ref().unwrap());
|
||||
assert_eq!(
|
||||
&Body::from(format!("Thanks {}!\n", "world")),
|
||||
resp.body().as_ref().unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue