Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extendable controller & configuration for setup #28

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably best to version all the subpackages of inngest-kt together. I think there's something we can add in the root gradle file. I'll take a note of this for now


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
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,82 @@
package com.inngest.springbootdemo;


import com.inngest.*;
import com.inngest.springboot.InngestConfiguration;
import kotlin.jvm.functions.Function2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already discussed this but probably ideal if we don't have to pull in a kotlin Function here and can instead use lambdas (available in Java 8) or something like https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html

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;
}

@Override
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,20 @@
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 {
@Override
protected HashMap<String, InngestFunction> functions() {
return new HashMap<>();
}

@Override
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")
Loading