From 7d37facaecfb15778e4bde18ae59414477cc55ec Mon Sep 17 00:00:00 2001 From: Riadh <22998716+KiKoS0@users.noreply.github.com> Date: Sun, 25 Feb 2024 00:35:13 +0100 Subject: [PATCH] wip --- .../src/main/kotlin/com/inngest/Comm.kt | 2 +- .../src/main/kotlin/com/inngest/Inngest.kt | 2 - inngest-spring-boot-adapter/build.gradle.kts | 34 ++++++++ .../springboot/InngestConfiguration.java | 22 +++++ .../springboot}/InngestController.java | 23 ++--- .../java/com/inngest/springboot}/Result.java | 2 +- .../src/main/resources/application.yml | 4 + inngest-spring-boot-demo/build.gradle.kts | 4 +- .../springbootdemo/DemoConfiguration.java | 83 ++++++++++++++++++ .../springbootdemo/DemoController.java | 10 +++ .../springbootdemo/InngestSingleton.java | 85 ------------------- ...ollerTest.java => DemoControllerTest.java} | 6 +- .../springbootdemo/DemoTestConfiguration.java | 19 +++++ settings.gradle.kts | 2 +- 14 files changed, 193 insertions(+), 105 deletions(-) create mode 100644 inngest-spring-boot-adapter/build.gradle.kts create mode 100644 inngest-spring-boot-adapter/src/main/java/com/inngest/springboot/InngestConfiguration.java rename {inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo => inngest-spring-boot-adapter/src/main/java/com/inngest/springboot}/InngestController.java (67%) rename {inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo => inngest-spring-boot-adapter/src/main/java/com/inngest/springboot}/Result.java (85%) create mode 100644 inngest-spring-boot-adapter/src/main/resources/application.yml create mode 100644 inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/DemoConfiguration.java create mode 100644 inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/DemoController.java delete mode 100644 inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java rename inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/{InngestControllerTest.java => DemoControllerTest.java} (83%) create mode 100644 inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java diff --git a/inngest-core/src/main/kotlin/com/inngest/Comm.kt b/inngest-core/src/main/kotlin/com/inngest/Comm.kt index b8c49acc..6472b382 100644 --- a/inngest-core/src/main/kotlin/com/inngest/Comm.kt +++ b/inngest-core/src/main/kotlin/com/inngest/Comm.kt @@ -47,7 +47,7 @@ data class CommError( val jsonMediaType = "application/json".toMediaType() -class CommHandler(val functions: HashMap) { +class CommHandler(val functions: HashMap, val client: Inngest? = null) { private fun getHeaders(): Map { return mapOf( "Content-Type" to "application/json", diff --git a/inngest-core/src/main/kotlin/com/inngest/Inngest.kt b/inngest-core/src/main/kotlin/com/inngest/Inngest.kt index 5256ef6c..e28f3a34 100644 --- a/inngest-core/src/main/kotlin/com/inngest/Inngest.kt +++ b/inngest-core/src/main/kotlin/com/inngest/Inngest.kt @@ -1,7 +1,5 @@ package com.inngest -// import okhttp3.RequestBody.Companion.toRequestBody - class Inngest { constructor( app_id: String, diff --git a/inngest-spring-boot-adapter/build.gradle.kts b/inngest-spring-boot-adapter/build.gradle.kts new file mode 100644 index 00000000..910c5a02 --- /dev/null +++ b/inngest-spring-boot-adapter/build.gradle.kts @@ -0,0 +1,34 @@ +plugins { + `java-library` + id("io.spring.dependency-management") version "1.1.4" +} + +group = "com.inngest" +version = "0.0.1-SNAPSHOT" + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 +} + +repositories { + mavenCentral() +} + +dependencies { + api(project(":inngest-core")) + + implementation("org.springframework.boot:spring-boot-starter-web") + testImplementation("org.springframework.boot:spring-boot-starter-test") +} + +dependencyManagement { + imports { + mavenBom("org.springframework.boot:spring-boot-dependencies:2.7.18") { + bomProperty("kotlin.version", "1.9.10") + } + } +} + +tasks.withType { + useJUnitPlatform() +} diff --git a/inngest-spring-boot-adapter/src/main/java/com/inngest/springboot/InngestConfiguration.java b/inngest-spring-boot-adapter/src/main/java/com/inngest/springboot/InngestConfiguration.java new file mode 100644 index 00000000..74f5db64 --- /dev/null +++ b/inngest-spring-boot-adapter/src/main/java/com/inngest/springboot/InngestConfiguration.java @@ -0,0 +1,22 @@ +package com.inngest.springboot; + +import com.inngest.CommHandler; +import com.inngest.Inngest; +import com.inngest.InngestFunction; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; + +import java.util.HashMap; + +public abstract class InngestConfiguration { + protected abstract HashMap functions(); + + @Bean + protected abstract Inngest inngestClient(); + + + @Bean + protected CommHandler commHandler(@Autowired Inngest inngestClient) { + return new CommHandler(functions(), inngestClient); + } +} diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestController.java b/inngest-spring-boot-adapter/src/main/java/com/inngest/springboot/InngestController.java similarity index 67% rename from inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestController.java rename to inngest-spring-boot-adapter/src/main/java/com/inngest/springboot/InngestController.java index bb4ea2e7..b219c010 100644 --- a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestController.java +++ b/inngest-spring-boot-adapter/src/main/java/com/inngest/springboot/InngestController.java @@ -1,15 +1,17 @@ -package com.inngest.springbootdemo; +package com.inngest.springboot; +import com.inngest.CommHandler; import com.inngest.CommResponse; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -@RestController -@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_VALUE) -public class InngestController { +public abstract class InngestController { + @Autowired + CommHandler commHandler; private static final HttpHeaders commonHeaders = new HttpHeaders(); @@ -18,26 +20,25 @@ public class InngestController { commonHeaders.add("x-inngest-sdk", inngestSdk); } - @GetMapping("/inngest") + @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity index() { - String response = InngestSingleton.getInstance().introspect(); - + String response = commHandler.introspect(); return ResponseEntity.ok().headers(commonHeaders).body(response); } - @PutMapping("/inngest") + @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity put() { - String response = InngestSingleton.getInstance().register(); + String response = commHandler.register(); return ResponseEntity.ok().headers(commonHeaders).body(response); } - @PostMapping(value = "/inngest", produces = MediaType.APPLICATION_JSON_VALUE) + @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity handleRequest( @RequestParam(name = "fnId") String functionId, @RequestBody String body ) { try { - CommResponse response = InngestSingleton.getInstance().callFunction(functionId, body); + CommResponse response = commHandler.callFunction(functionId, body); return ResponseEntity.status(response.getStatusCode().getCode()).headers(commonHeaders) .body(response.getBody()); diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/Result.java b/inngest-spring-boot-adapter/src/main/java/com/inngest/springboot/Result.java similarity index 85% rename from inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/Result.java rename to inngest-spring-boot-adapter/src/main/java/com/inngest/springboot/Result.java index 01a87978..9b8b9cdd 100644 --- a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/Result.java +++ b/inngest-spring-boot-adapter/src/main/java/com/inngest/springboot/Result.java @@ -1,4 +1,4 @@ -package com.inngest.springbootdemo; +package com.inngest.springboot; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/inngest-spring-boot-adapter/src/main/resources/application.yml b/inngest-spring-boot-adapter/src/main/resources/application.yml new file mode 100644 index 00000000..a1f56d6e --- /dev/null +++ b/inngest-spring-boot-adapter/src/main/resources/application.yml @@ -0,0 +1,4 @@ +server: + port: 8080 # We probably want to change this to 8081 to avoid port conflicts + # but I kept it for now because of the hardcoded URL that are sent + # to the inngest dev-server. diff --git a/inngest-spring-boot-demo/build.gradle.kts b/inngest-spring-boot-demo/build.gradle.kts index 722494e5..5a50489e 100644 --- a/inngest-spring-boot-demo/build.gradle.kts +++ b/inngest-spring-boot-demo/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "com.inngest" -version = "0.0.1" +version = "0.0.1-SNAPSHOT" java { sourceCompatibility = JavaVersion.VERSION_1_8 @@ -16,7 +16,7 @@ repositories { } dependencies { - implementation(project(":inngest-core")) + implementation(project(":inngest-spring-boot-adapter")) implementation("org.springframework.boot:spring-boot-starter-web") testImplementation("org.springframework.boot:spring-boot-starter-test") diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/DemoConfiguration.java b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/DemoConfiguration.java new file mode 100644 index 00000000..4cd12bff --- /dev/null +++ b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/DemoConfiguration.java @@ -0,0 +1,83 @@ +package com.inngest.springbootdemo; + + +import com.inngest.*; +import com.inngest.springboot.InngestConfiguration; +import com.inngest.springboot.Result; +import kotlin.jvm.functions.Function2; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.LinkedHashMap; +import java.time.Duration; +import java.util.HashMap; + +@Configuration +public class DemoConfiguration extends InngestConfiguration { + + @Override + public HashMap functions() { + String followUpEvent = "user.signup.completed"; + FunctionTrigger fnTrigger = new FunctionTrigger("user-signup", null, null); + FunctionTrigger[] triggers = {fnTrigger}; + FunctionOptions fnConfig = new FunctionOptions("fn-id-slug", "My function!", triggers); + + Function2> handler = (ctx, step) -> { + int x = 10; + + System.out.println("-> handler called " + ctx.getEvent().getName()); + + int y = step.run("add-ten", () -> x + 10, Integer.class); + + Result res = step.run("cast-to-type-add-ten", () -> { + System.out.println("-> running step 1!! " + x); + return new Result(y + 10); + }, Result.class); + + System.out.println("res" + res); + + step.waitForEvent("wait-for-hello", "hello", "10m", null); + + int add = step.run("add-one-hundred", () -> { + System.out.println("-> running step 2 :) " + (res != null ? res.sum : "")); + return (res != null ? res.sum : 0) + 100; + }, Integer.class); + + step.sleep("wait-one-sec", Duration.ofSeconds(2)); + + step.run("last-step", () -> (res != null ? res.sum : 0) * add, Integer.class); + + HashMap data = new HashMap() {{ + put("hello", "world"); + }}; + step.sendEvent("followup-event-id", new InngestEvent(followUpEvent, data)); + + return new HashMap() {{ + put("message", "cool - this finished running"); + }}; + }; + + FunctionTrigger followupFnTrigger = new FunctionTrigger(followUpEvent, null, null); + FunctionOptions followupFnConfig = new FunctionOptions( + "fn-follow-up", + "Follow up function!", + new FunctionTrigger[]{followupFnTrigger} + ); + Function2> followupHandler = (ctx, step) -> { + System.out.println("-> follow up handler called " + ctx.getEvent().getName()); + return ctx.getEvent().getData(); + }; + + HashMap functions = new HashMap<>(); + functions.put("fn-id-slug", new InngestFunction(fnConfig, handler)); + functions.put("fn-follow-up", new InngestFunction(followupFnConfig, followupHandler)); + + return functions; + } + + @Bean + public Inngest inngestClient() { + return new Inngest("spring_demo"); + } +} + diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/DemoController.java b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/DemoController.java new file mode 100644 index 00000000..06b639a6 --- /dev/null +++ b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/DemoController.java @@ -0,0 +1,10 @@ +package com.inngest.springbootdemo; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import com.inngest.springboot.InngestController; + +@RestController +@RequestMapping(value = "/api/inngest") +public class DemoController extends InngestController { +} diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java deleted file mode 100644 index 9ea216c9..00000000 --- a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/InngestSingleton.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.inngest.springbootdemo; - -import com.inngest.CommHandler; -import com.inngest.FunctionOptions; -import com.inngest.FunctionTrigger; -import com.inngest.InngestFunction; -import com.inngest.FunctionContext; -import com.inngest.Step; -import com.inngest.InngestEvent; -import kotlin.jvm.functions.Function2; - -import java.time.Duration; -import java.util.HashMap; -import java.util.LinkedHashMap; - -// NOTE: We probably don't need this singleton anymore -// when revisiting the SDK's interface. -public class InngestSingleton { - private static CommHandler instance; - - private static final String followUpEvent = "user.signup.completed"; - - public static synchronized CommHandler getInstance() { - if (instance == null) { - FunctionTrigger fnTrigger = new FunctionTrigger("user-signup", null, null); - FunctionOptions fnConfig = new FunctionOptions( - "fn-id-slug", - "My function!", - new FunctionTrigger[]{fnTrigger} - ); - - Function2> handler = (ctx, step) -> { - int x = 10; - - System.out.println("-> handler called " + ctx.getEvent().getName()); - - int y = step.run("add-ten", () -> x + 10, Integer.class); - - Result res = step.run("cast-to-type-add-ten", () -> { - System.out.println("-> running step 1!! " + x); - return new Result(y + 10); - }, Result.class); - System.out.println("res" + res); - - step.waitForEvent("wait-for-hello", "hello", "10m", "event.data.hello == async.data.hello"); - - int add = step.run("add-one-hundred", () -> { - System.out.println("-> running step 2 :) " + (res != null ? res.sum : "")); - return (res != null ? res.sum : 0) + 100; - }, Integer.class); - - step.sleep("wait-one-sec", Duration.ofSeconds(2)); - - step.run("last-step", () -> (res != null ? res.sum : 0) * add, Integer.class); - - HashMap data = new HashMap() {{ - put("hello", "world"); - }}; - step.sendEvent("followup-event-id", new InngestEvent(followUpEvent, data)); - - return new HashMap() {{ - put("message", "cool - this finished running"); - }}; - }; - - FunctionTrigger followupFnTrigger = new FunctionTrigger(followUpEvent, null, null); - FunctionOptions followupFnConfig = new FunctionOptions( - "fn-follow-up", - "Follow up function!", - new FunctionTrigger[]{followupFnTrigger} - ); - Function2> followupHandler = (ctx, step) -> { - System.out.println("-> follow up handler called " + ctx.getEvent().getName()); - return ctx.getEvent().getData(); - }; - - HashMap functions = new HashMap<>(); - functions.put("fn-id-slug", new InngestFunction(fnConfig, handler)); - functions.put("fn-follow-up", new InngestFunction(followupFnConfig, followupHandler)); - - instance = new CommHandler(functions); - } - return instance; - } -} diff --git a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/InngestControllerTest.java b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoControllerTest.java similarity index 83% rename from inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/InngestControllerTest.java rename to inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoControllerTest.java index a15316af..ca4b4d13 100644 --- a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/InngestControllerTest.java +++ b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoControllerTest.java @@ -3,13 +3,15 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.context.annotation.Import; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -@WebMvcTest(InngestController.class) -public class InngestControllerTest { +@Import(DemoTestConfiguration.class) +@WebMvcTest(DemoController.class) +public class DemoControllerTest { @Autowired private MockMvc mockMvc; diff --git a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java new file mode 100644 index 00000000..a5d7dd53 --- /dev/null +++ b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java @@ -0,0 +1,19 @@ +package com.inngest.springbootdemo; + +import com.inngest.Inngest; +import com.inngest.InngestFunction; +import com.inngest.springboot.InngestConfiguration; +import org.springframework.context.annotation.Bean; + +import java.util.HashMap; + +public class DemoTestConfiguration extends InngestConfiguration { + protected HashMap functions() { + return new HashMap<>(); + } + + @Bean + protected Inngest inngestClient() { + return new Inngest("spring_test_demo"); + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index ffb1c5c7..afe319db 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -12,4 +12,4 @@ plugins { rootProject.name = "inngest-sdk" -include("inngest-core", "inngest-test-server", "inngest-spring-boot-demo") +include("inngest-core", "inngest-test-server", "inngest-spring-boot-adapter", "inngest-spring-boot-demo")