Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
KiKoS0 committed Feb 25, 2024
1 parent e8ab5d9 commit 7d37fac
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 105 deletions.
2 changes: 1 addition & 1 deletion inngest-core/src/main/kotlin/com/inngest/Comm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ data class CommError(

val jsonMediaType = "application/json".toMediaType()

class CommHandler(val functions: HashMap<String, InngestFunction>) {
class CommHandler(val functions: HashMap<String, InngestFunction>, val client: Inngest? = null) {
private fun getHeaders(): Map<String, String> {
return mapOf(
"Content-Type" to "application/json",
Expand Down
2 changes: 0 additions & 2 deletions inngest-core/src/main/kotlin/com/inngest/Inngest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.inngest

// import okhttp3.RequestBody.Companion.toRequestBody

class Inngest {
constructor(
app_id: String,
Expand Down
34 changes: 34 additions & 0 deletions inngest-spring-boot-adapter/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<Test> {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -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<String, InngestFunction> functions();

@Bean
protected abstract Inngest inngestClient();


@Bean
protected CommHandler commHandler(@Autowired Inngest inngestClient) {
return new CommHandler(functions(), inngestClient);
}
}
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -18,26 +20,25 @@ public class InngestController {
commonHeaders.add("x-inngest-sdk", inngestSdk);
}

@GetMapping("/inngest")
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> 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<String> 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<String> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.inngest.springbootdemo;
package com.inngest.springboot;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions inngest-spring-boot-demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "com.inngest"
version = "0.0.1"
version = "0.0.1-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, InngestFunction> 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<FunctionContext, Step, HashMap<String, String>> 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<String, String> data = new HashMap<String, String>() {{
put("hello", "world");
}};
step.sendEvent("followup-event-id", new InngestEvent(followUpEvent, data));

return new HashMap<String, String>() {{
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<FunctionContext, Step, LinkedHashMap<String, Object>> followupHandler = (ctx, step) -> {
System.out.println("-> follow up handler called " + ctx.getEvent().getName());
return ctx.getEvent().getData();
};

HashMap<String, InngestFunction> 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");
}
}

Original file line number Diff line number Diff line change
@@ -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 {
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, InngestFunction> functions() {
return new HashMap<>();
}

@Bean
protected Inngest inngestClient() {
return new Inngest("spring_test_demo");
}
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 7d37fac

Please sign in to comment.