Cleanup Rust cloudevents example (#1799)

By:

- Using a more opinionated release profile to optimize binary
- Removing not required dependencies
- Updating the rest of the dependencies
- Switch to Rust edition 2021
- Cleanup imports by importing the concrete types
- Avoid unnecessary type assertions
- Do not unwrap port

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
This commit is contained in:
Sascha Grunert 2023-06-10 05:32:59 +02:00 committed by GitHub
parent 890d440037
commit 529957e837
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 8001 additions and 7951 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,21 +1,20 @@
[package]
name = "function"
version = "0.1.0"
edition = "2018"
edition = "2021"
[profile.release]
codegen-units = 1
debug = 0
incremental = true
lto = true
opt-level = "z"
strip = "symbols"
[dependencies]
cloudevents-sdk = { version = "0.7.0", features = ["actix"] }
actix-web = "4.3.1"
actix-rt = "2.8.0"
serde_json = "1"
log = "0.4.0"
actix-web = "4.3.1"
cloudevents-sdk = { version = "0.7.0", features = ["actix"] }
env_logger = "0.10.0"
regex = "1.5.5"
tokio = "1.8.4"
actix-http = "3.3.1"
[dev-dependencies]
regex = "1.5.5"
tokio = "1.8.4"
actix-http = "3.3.1"
log = "0.4.18"
serde_json = "1.0.96"

View File

@ -1,4 +1,5 @@
use actix_web::{web::{self, Data}};
use actix_web::web::{Data, ServiceConfig};
use log::info;
/// Run custom configuration as part of the application building
/// process.
@ -6,7 +7,7 @@ use actix_web::{web::{self, Data}};
/// This function should contain all custom configuration for your function application.
///
/// ```rust
/// fn configure(cfg: &mut web::ServiceConfig) {
/// fn configure(cfg: &mut ServiceConfig) {
/// let db_driver = my_db();
/// cfg.data(db_driver.clone());
/// }
@ -17,12 +18,12 @@ use actix_web::{web::{self, Data}};
/// ```rust
/// pub async fn handle(
/// event: Event,
/// driver: web::Data<DbDriver>,
/// driver: Data<DbDriver>,
/// ) -> Result<Event, actix_web::Error> {
/// Ok(Event::default())
/// }
pub fn configure(cfg: &mut web::ServiceConfig) {
log::info!("Configuring service");
pub fn configure(cfg: &mut ServiceConfig) {
info!("Configuring service");
cfg.app_data(Data::new(HandlerConfig::default()));
}
@ -34,7 +35,7 @@ pub struct HandlerConfig {
impl Default for HandlerConfig {
fn default() -> HandlerConfig {
HandlerConfig {
name: String::from("world"),
name: "world".into(),
}
}
}

View File

@ -1,5 +1,5 @@
use crate::config::HandlerConfig;
use actix_web::web;
use actix_web::{error::ErrorInternalServerError, web};
use cloudevents::{event::Data, Event, EventBuilder, EventBuilderV10};
use log::info;
use serde_json::{from_slice, from_str, json};
@ -23,7 +23,7 @@ pub async fn handle(
.ty("func.example")
.data("application/json", json!({ "hello": input["name"] }))
.build()
.map_err(actix_web::error::ErrorInternalServerError)
.map_err(ErrorInternalServerError)
}
#[cfg(test)]

View File

@ -1,15 +1,22 @@
use actix_web::{web, App, HttpResponse, HttpServer};
use env_logger as elog;
use env_logger::{Builder, Env};
use std::{
env,
io::{Error, ErrorKind, Result},
num::ParseIntError,
};
mod config;
mod handler;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
elog::Builder::from_env(elog::Env::default().default_filter_or("info,actix_web=warn")).init();
async fn main() -> Result<()> {
Builder::from_env(Env::default().default_filter_or("info,actix_web=warn")).init();
let port: u16 = match std::env::var("PORT") {
Ok(v) => v.parse().unwrap(),
let port = match env::var("PORT") {
Ok(v) => v
.parse()
.map_err(|e: ParseIntError| Error::new(ErrorKind::Other, e.to_string()))?,
Err(_) => 8080,
};