Skip to content

Commit

Permalink
Merge branch 'main' into task/config-options-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
djfarrelly committed Jul 18, 2024
2 parents 939173a + 85e258a commit b0a57d6
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 37 deletions.
5 changes: 5 additions & 0 deletions inngest-spring-boot-adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# inngest-spring-boot-adapter

Adapter to serve Inngest functions from a SpringBoot application.


2 changes: 1 addition & 1 deletion inngest-spring-boot-adapter/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.3
0.0.5
2 changes: 1 addition & 1 deletion inngest-spring-boot-adapter/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repositories {
}

dependencies {
val pkg = if (System.getenv("RELEASE") != null) "com.inngest:inngest:[0.0, 0.1)" else project(":inngest")
val pkg = if (System.getenv("RELEASE") != null) "com.inngest:inngest:[0.0.5, 0.1)" else project(":inngest")
api(pkg)

implementation("org.springframework.boot:spring-boot-starter-web")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public abstract class InngestConfiguration {
protected abstract Inngest inngestClient();

@Bean
protected CommHandler commHandler(@Autowired Inngest inngestClient) {
// TODO: Add missing configuration
ServeConfig serveConfig = new ServeConfig(inngestClient);
return new CommHandler(functions(), inngestClient, serveConfig, frameworkName);
protected abstract ServeConfig serve(@Autowired Inngest inngestClient);

@Bean
protected CommHandler commHandler(@Autowired Inngest inngestClient, @Autowired ServeConfig serve) {
return new CommHandler(functions(), inngestClient, serve, frameworkName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import com.inngest.CommResponse;
import com.inngest.signingkey.SignatureVerificationKt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.*;

import javax.servlet.http.HttpServletRequest;

public abstract class InngestController {
@Autowired
CommHandler commHandler;
Expand All @@ -20,15 +22,33 @@ private HttpHeaders getHeaders() {
return headers;
}

// ex. https://api.mysite.com
@Value("${inngest.serveOrigin:}")
private String serveOrigin;

@GetMapping()
public ResponseEntity<String> index() {
String response = commHandler.introspect();
public ResponseEntity<String> index(
@RequestHeader(HttpHeaders.HOST) String hostHeader,
HttpServletRequest request
) {
String origin = String.format("%s://%s", request.getScheme(), hostHeader);
if (this.serveOrigin != null && !this.serveOrigin.isEmpty()) {
origin = this.serveOrigin;
}
String response = commHandler.introspect(origin);
return ResponseEntity.ok().headers(getHeaders()).body(response);
}

@PutMapping()
public ResponseEntity<String> put() {
String response = commHandler.register();
public ResponseEntity<String> put(
@RequestHeader(HttpHeaders.HOST) String hostHeader,
HttpServletRequest request
) {
String origin = String.format("%s://%s", request.getScheme(), hostHeader);
if (this.serveOrigin != null && !this.serveOrigin.isEmpty()) {
origin = this.serveOrigin;
}
String response = commHandler.register(origin);
return ResponseEntity.ok().headers(getHeaders()).body(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ public HashMap<String, InngestFunction> functions() {
}

@Override
public Inngest inngestClient() {
protected Inngest inngestClient() {
return new Inngest("spring_demo");
}

@Override
protected ServeConfig serve(Inngest inngestClient) {
return new ServeConfig(inngestClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
@RestController
@RequestMapping(value = "/api/inngest")
public class DemoController extends InngestController {

}
4 changes: 1 addition & 3 deletions inngest-spring-boot-demo/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
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.
port: 8080
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class CustomStepResultIntegrationTest {
@BeforeAll
static void setup(@Autowired CommHandler handler) {
handler.register();
handler.register("http://localhost:8080");
}

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ public class DemoControllerTest {

@Test
public void shouldReturnSyncPayload() throws Exception {
mockMvc.perform(get("/api/inngest"))
mockMvc.perform(get("/api/inngest").header("Host", "localhost:8080"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(header().string(InngestHeaderKey.Framework.getValue(), "springboot"))
.andExpect(jsonPath("$.appName").value("spring_test_demo"))
.andExpect(jsonPath("$.url").value("http://localhost:8080/api/inngest"))
.andExpect(jsonPath("$.sdk").value("inngest-kt"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ protected Inngest inngestClient() {
return new Inngest("spring_test_demo");
}

@Override
protected ServeConfig serve(Inngest client) {
return new ServeConfig(client);
}

@Bean
protected CommHandler commHandler(@Autowired Inngest inngestClient) {
ServeConfig serveConfig = new ServeConfig(inngestClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class ErrorsInStepsIntegrationTest {
@BeforeAll
static void setup(@Autowired CommHandler handler) {
handler.register();
handler.register("http://localhost:8080");
}

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class MultiStepsFunctionIntegrationTest {
@BeforeAll
static void setup(@Autowired CommHandler handler) {
handler.register();
handler.register("http://localhost:8080");
}

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class NoStepFunctionIntegrationTest {
@BeforeAll
static void setup(@Autowired CommHandler handler) {
handler.register();
handler.register("http://localhost:8080");
}

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class SendEventFunctionIntegrationTest {
@BeforeAll
static void setup(@Autowired CommHandler handler) {
handler.register();
handler.register("http://localhost:8080");
}

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SleepFunctionIntegrationTest {

@BeforeAll
static void setup(@Autowired CommHandler handler) {
handler.register();
handler.register("http://localhost:8080");
}

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class WaitForEventFunctionIntegrationTest {

@BeforeAll
static void setup(@Autowired CommHandler handler) {
handler.register();
handler.register("http://localhost:8080");
}

@Autowired
Expand Down
2 changes: 1 addition & 1 deletion inngest/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.4
0.0.5
24 changes: 13 additions & 11 deletions inngest/src/main/kotlin/com/inngest/Comm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,16 @@ class CommHandler(
return mapper.writeValueAsString(requestBody)
}

private fun getFunctionConfigs(): List<Map<String, Any>> {

private fun getFunctionConfigs(origin: String): List<Map<String, Any>> {
val configs: MutableList<Map<String, Any>> = mutableListOf()
functions.forEach { entry -> configs.add(entry.value.getFunctionConfig(getServeUrl(), client)) }
functions.forEach { entry -> configs.add(entry.value.getFunctionConfig(getServeUrl(origin), client)) }
return configs
}

fun register(): String {
fun register(origin: String): String {
val registrationUrl = "${config.baseUrl()}/fn/register"
val requestPayload = getRegistrationRequestPayload()
val requestPayload = getRegistrationRequestPayload(origin)

val httpClient = client.httpClient

Expand All @@ -147,24 +148,25 @@ class CommHandler(
return Result.success(InngestSyncResult.None)
}

fun introspect(): String {
val requestPayload = getRegistrationRequestPayload()
fun introspect(origin: String): String {
val requestPayload = getRegistrationRequestPayload(origin)
return parseRequestBody(requestPayload)
}

private fun getRegistrationRequestPayload(): RegistrationRequestPayload {
private fun getRegistrationRequestPayload(origin: String): RegistrationRequestPayload {
return RegistrationRequestPayload(
appName = config.appId(),
framework = framework.toString(),
sdk = "inngest-kt",
url = getServeUrl(),
url = getServeUrl(origin),
v = Version.getVersion(),
functions = getFunctionConfigs(),
functions = getFunctionConfigs(origin),
)
}

private fun getServeUrl(): String {
val serveOrigin = config.serveOrigin() ?: "http://localhost:8080"
private fun getServeUrl(origin: String): String {
// TODO - property from SpringBoot should take preference to env variable?
val serveOrigin = config.serveOrigin() ?: origin
val servePath = config.servePath() ?: "/api/inngest"
return "$serveOrigin$servePath"
}
Expand Down
13 changes: 11 additions & 2 deletions inngest/src/main/kotlin/com/inngest/ktor/Route.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.inngest.ktor
import com.inngest.*
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.plugins.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
Expand Down Expand Up @@ -42,7 +43,11 @@ fun Route.serve(

route(path) {
get("") {
val resp = comm.introspect()
var origin = String.format("%s://%s", call.request.origin.scheme, call.request.origin.serverHost)
if (call.request.origin.serverPort != 80 || call.request.origin.serverPort != 443) {
origin = String.format("%s:%s", origin, call.request.origin.serverPort)
}
val resp = comm.introspect(origin)
call.respond(HttpStatusCode.OK, resp)
}

Expand Down Expand Up @@ -72,7 +77,11 @@ fun Route.serve(
}

put("") {
val resp = comm.register()
var origin = String.format("%s://%s", call.request.origin.scheme, call.request.origin.serverHost)
if (call.request.origin.serverPort != 80 || call.request.origin.serverPort != 443) {
origin = String.format("%s:%s", origin, call.request.origin.serverPort)
}
val resp = comm.register(origin)
call.respond(HttpStatusCode.OK, resp)
}
}
Expand Down

0 comments on commit b0a57d6

Please sign in to comment.