diff --git a/inngest/examples/axum/main.rs b/inngest/examples/axum/main.rs index e30398e..0714e53 100644 --- a/inngest/examples/axum/main.rs +++ b/inngest/examples/axum/main.rs @@ -3,7 +3,7 @@ use axum::{ Router, }; use inngest::{ - function::{create_function, FunctionOps, Input, ServableFunction, Trigger}, + function::{create_function, FunctionOps, Input, ServableFn, Trigger}, router::{axum as inngest_axum, Handler}, }; use serde::{Deserialize, Serialize}; @@ -38,17 +38,17 @@ struct TestData { data: u8, } -fn dummy_fn() -> Box { +fn dummy_fn() -> ServableFn { create_function( FunctionOps { - name: "Dummy func".to_string(), + id: "Dummy func".to_string(), ..Default::default() }, Trigger::EventTrigger { event: "test/event".to_string(), expression: None, }, - Box::new(|input: Input| { + |input: Input| { println!("In dummy function"); let evt = input.event; @@ -56,21 +56,21 @@ fn dummy_fn() -> Box { // println!("Data: {:?}", evt.data); Ok(Box::new("test result".to_string())) - }), + }, ) } -fn hello_fn() -> Box { +fn hello_fn() -> ServableFn { create_function( FunctionOps { - name: "Hello func".to_string(), + id: "Hello func".to_string(), ..Default::default() }, Trigger::EventTrigger { event: "test/hello".to_string(), expression: None, }, - Box::new(|input: Input| { + |input: Input| { println!("In hello function"); let evt = input.event; @@ -78,6 +78,6 @@ fn hello_fn() -> Box { // println!("Data: {:?}", evt.data()); Ok(Box::new("test hello".to_string())) - }), + }, ) } diff --git a/inngest/src/function.rs b/inngest/src/function.rs index ac830cf..00a436e 100644 --- a/inngest/src/function.rs +++ b/inngest/src/function.rs @@ -3,22 +3,6 @@ use serde::{Deserialize, Serialize}; use slug::slugify; use std::{any::Any, collections::HashMap, fmt::Debug}; -pub trait ServableFunction { - fn slug(&self) -> String; - fn name(&self) -> String; - fn trigger(&self) -> Trigger; -} - -impl Debug for dyn ServableFunction + Send + Sync { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("ServableFn") - .field("name", &self.name()) - .field("slug", &self.slug()) - .field("trigger", &self.trigger()) - .finish() - } -} - #[derive(Deserialize)] pub struct Input where @@ -38,16 +22,16 @@ pub struct InputCtx { #[derive(Debug, Clone)] pub struct FunctionOps { - pub id: Option, - pub name: String, + pub id: String, + pub name: Option, pub retries: u8, } impl Default for FunctionOps { fn default() -> Self { FunctionOps { - id: None, - name: String::new(), + id: String::new(), + name: None, retries: 3, } } @@ -59,25 +43,19 @@ where { pub opts: FunctionOps, pub trigger: Trigger, - pub func: Box) -> Result, String> + Send + Sync>, + pub func: fn(Input) -> Result, String>, } -impl ServableFunction for ServableFn +impl ServableFn where T: Serialize + for<'a> Deserialize<'a> + 'static, { - fn slug(&self) -> String { - match &self.opts.id { - Some(id) => id.clone(), - None => slugify(self.name()), - } - } - - fn name(&self) -> String { - self.opts.name.clone() + // TODO: prepend app_id + pub fn slug(&self) -> String { + slugify(self.opts.id.clone()) } - fn trigger(&self) -> Trigger { + pub fn trigger(&self) -> Trigger { self.trigger.clone() } } @@ -125,13 +103,11 @@ pub enum Trigger { pub fn create_function Deserialize<'a> + 'static>( opts: FunctionOps, trigger: Trigger, - func: Box) -> Result, String> + Send + Sync>, -) -> Box { - let servable = ServableFn { + func: fn(Input) -> Result, String>, +) -> ServableFn { + ServableFn { opts, trigger, func, - }; - - Box::new(servable) + } } diff --git a/inngest/src/router/axum.rs b/inngest/src/router/axum.rs index 27d4264..1aa2f8d 100644 --- a/inngest/src/router/axum.rs +++ b/inngest/src/router/axum.rs @@ -2,6 +2,7 @@ use axum::{ extract::{Query, State}, Json, }; +use serde::{Deserialize, Serialize}; use serde_json::Value; use crate::{ @@ -14,7 +15,9 @@ use std::{collections::HashMap, default::Default, sync::Arc}; use super::InvokeQuery; -pub async fn register(State(handler): State>) -> Result<(), String> { +pub async fn register Deserialize<'a> + 'static>( + State(handler): State>> +) -> Result<(), String> { let funcs: Vec = handler .funcs .iter() @@ -36,7 +39,7 @@ pub async fn register(State(handler): State>) -> Result<(), String> Function { id: f.slug(), - name: f.name(), + name: f.slug(), // TODO: use the proper name triggers: vec![f.trigger()], steps, } @@ -52,20 +55,18 @@ pub async fn register(State(handler): State>) -> Result<(), String> let client = reqwest::Client::new(); - match client + client .post("http://127.0.0.1:8288/fn/register") .json(&req) .send() .await - { - Ok(_) => Ok(()), - Err(_) => Err("error".to_string()), - } + .map(|_| ()) + .map_err(|_| "error".to_string()) } -pub async fn invoke( +pub async fn invoke Deserialize<'a> + 'static>( Query(query): Query, - State(handler): State>, + State(handler): State>>, Json(body): Json, ) -> Result<(), String> { println!("Body: {:#?}", body); @@ -73,11 +74,19 @@ pub async fn invoke( match handler.funcs.iter().find(|f| f.slug() == query.fn_id) { None => Err(format!("no function registered as ID: {}", query.fn_id)), Some(func) => { - println!("Name: {}", func.name()); println!("Slug: {}", func.slug()); println!("Trigger: {:?}", func.trigger()); // println!("Event: {:?}", func.event()); + // match (func.func)() { + // Ok(_) => { + // println!("OK!!"); + // } + // Err(err) => { + // println!("{:?}", err) + // } + // } + Ok(()) // match serde_json::from_value::>(body) { // Ok(body) => { diff --git a/inngest/src/router/mod.rs b/inngest/src/router/mod.rs index 2e89cf5..d55c2cc 100644 --- a/inngest/src/router/mod.rs +++ b/inngest/src/router/mod.rs @@ -1,25 +1,32 @@ pub mod axum; -use crate::function::ServableFunction; -use serde::Deserialize; -use std::default::Default; +use crate::function::ServableFn; +use serde::{Deserialize, Serialize}; -#[derive(Debug)] -pub struct Handler { +pub struct Handler +where + T: Serialize + for<'a> Deserialize<'a> + 'static, +{ app_name: String, - funcs: Vec>, + funcs: Vec>, } -impl Handler { +impl Handler +where + T: Serialize + for<'a> Deserialize<'a> + 'static, +{ pub fn new() -> Self { - Self::default() + Handler { + app_name: "RustDev".to_string(), + funcs: vec![], + } } pub fn set_name(&mut self, name: &str) { self.app_name = name.to_string() } - pub fn register_fn(&mut self, func: Box) { + pub fn register_fn(&mut self, func: ServableFn) { self.funcs.push(func); } @@ -28,15 +35,6 @@ impl Handler { // } } -impl Default for Handler { - fn default() -> Self { - Handler { - app_name: "InngestApp".to_string(), - funcs: vec![], - } - } -} - #[derive(Deserialize)] pub struct InvokeQuery { #[serde(rename = "fnId")]