Light, fluffy, and always free - The AWS Local Emulator alternative
feat: add HOSTNAME_EXTERNAL support for multi-container Docker setups (#82)
Ben Rozenboim20b40c1565b8
diff --git a/README.md b/README.mdindex 234beb68ec..0b8b91d959 100644--- a/README.md+++ b/README.md@@ -157,12 +157,33 @@ All settings are overridable via environment variables (`FLOCI_` prefix).| `QUARKUS_HTTP_PORT` | `4566` | HTTP port || `FLOCI_DEFAULT_REGION` | `us-east-1` | Default AWS region || `FLOCI_DEFAULT_ACCOUNT_ID` | `000000000000` | Default AWS account ID |+| `FLOCI_BASE_URL` | `http://localhost:4566` | Base URL used in API responses (e.g. SQS QueueUrl) |+| `FLOCI_HOSTNAME` | *(unset)* | Override hostname in response URLs (for Docker Compose) || `FLOCI_STORAGE_MODE` | `hybrid` | `memory` · `persistent` · `hybrid` · `wal` || `FLOCI_STORAGE_PERSISTENT_PATH` | `./data` | Data directory |→ Full reference: [configuration docs](https://hectorvent.dev/floci/configuration/application-yml/)→ Per-service storage overrides: [storage docs](https://hectorvent.dev/floci/configuration/storage/#per-service-storage-overrides)+**Multi-container Docker Compose:** When your application runs in a separate container from Floci, set `FLOCI_HOSTNAME` to the Floci service name so that returned URLs (e.g. SQS QueueUrl) resolve correctly:++```yaml+services:+ floci:+ image: hectorvent/floci:latest+ ports:+ - "4566:4566"+ environment:+ - FLOCI_HOSTNAME=floci # URLs will use http://floci:4566/...+ my-app:+ environment:+ - AWS_ENDPOINT_URL=http://floci:4566+ depends_on:+ - floci+```++Without this, SQS returns `http://localhost:4566/...` in QueueUrl responses, which resolves to the wrong container.+## LicenseMIT — use it however you want.diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex 517ecb0d6c..33125417b0 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -10,6 +10,28 @@ public interface EmulatorConfig {@WithDefault("http://localhost:4566")String baseUrl();+ /**+ * When set, overrides the hostname in base-url for URLs returned in API responses+ * (e.g. SQS QueueUrl, SNS TopicArn). This is needed in multi-container Docker setups+ * where "localhost" in the response URL would resolve to the wrong container.+ *+ * Example: FLOCI_HOSTNAME=floci makes SQS return+ * http://floci:4566/000000000000/my-queue instead of http://localhost:4566/...+ *+ * Equivalent to LocalStack's LOCALSTACK_HOSTNAME.+ */+ Optional<String> hostname();++ /**+ * Returns the effective base URL, taking hostname into account.+ * If hostname is set, replaces the host in baseUrl with it.+ */+ default String effectiveBaseUrl() {+ return hostname()+ .map(h -> baseUrl().replaceFirst("://[^:/]+(:\\d+)?", "://" + h + "$1"))+ .orElse(baseUrl());+ }+@WithDefault("us-east-1")String defaultRegion();diff --git a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.javaindex d555c2348d..9f727d1256 100644--- a/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java+++ b/src/main/java/io/github/hectorvent/floci/services/eventbridge/EventBridgeService.java@@ -59,7 +59,7 @@ public class EventBridgeService {new TypeReference<Map<String, Rule>>() {}),storageFactory.create("eventbridge", "eventbridge-targets.json",new TypeReference<Map<String, List<Target>>>() {}),- regionResolver, lambdaService, sqsService, snsService, objectMapper, config.baseUrl()+ regionResolver, lambdaService, sqsService, snsService, objectMapper, config.effectiveBaseUrl());}diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.javaindex 41f28b951d..2edd39cb14 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java@@ -262,7 +262,7 @@ public class LambdaService {int batchSize = toInt(request.get("BatchSize"), 10);boolean enabled = !Boolean.FALSE.equals(request.get("Enabled"));- String queueUrl = eventSourceArn.contains(":sqs:") ? AwsArnUtils.arnToQueueUrl(eventSourceArn, config.baseUrl()) : null;+ String queueUrl = eventSourceArn.contains(":sqs:") ? AwsArnUtils.arnToQueueUrl(eventSourceArn, config.effectiveBaseUrl()) : null;EventSourceMapping esm = new EventSourceMapping();esm.setUuid(UUID.randomUUID().toString());@@ -437,7 +437,7 @@ public class LambdaService {urlConfig.setAuthType((String) request.getOrDefault("AuthType", "NONE"));String urlId = UUID.nameUUIDFromBytes((region + functionName + (qualifier != null ? qualifier : "")).getBytes()).toString().replace("-", "").substring(0, 32);- String baseHost = config.baseUrl().replaceFirst("https?://", "");+ String baseHost = config.effectiveBaseUrl().replaceFirst("https?://", "");String url = String.format("http://%s.lambda-url.%s.%s/", urlId, region, baseHost);urlConfig.setFunctionUrl(url);diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java b/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.javaindex 0d63722493..2cce690c62 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/SqsEventSourcePoller.java@@ -62,7 +62,7 @@ public class SqsEventSourcePoller {this.functionStore = functionStore;this.esmStore = esmStore;this.pollIntervalMs = config.services().lambda().pollIntervalMs();- this.baseUrl = config.baseUrl();+ this.baseUrl = config.effectiveBaseUrl();this.objectMapper = objectMapper;}diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.javaindex 9f3416f2dd..c58bd398bb 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java@@ -57,7 +57,7 @@ public class S3Service {new TypeReference<Map<String, S3Object>>() {}),Path.of(config.storage().persistentPath()).resolve("s3"),- sqsService, snsService, regionResolver, config.baseUrl(), objectMapper+ sqsService, snsService, regionResolver, config.effectiveBaseUrl(), objectMapper);}diff --git a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.javaindex ea5ef42b3e..8499c79fbf 100644--- a/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java+++ b/src/main/java/io/github/hectorvent/floci/services/sns/SnsService.java@@ -61,7 +61,7 @@ public class SnsService {regionResolver,sqsService,lambdaService,- config.baseUrl(),+ config.effectiveBaseUrl(),objectMapper);}diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.javaindex 3b008e607a..b58512ef34 100644--- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java+++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsService.java@@ -54,7 +54,7 @@ public class SqsService {}),config.services().sqs().defaultVisibilityTimeout(),config.services().sqs().maxMessageSize(),- config.baseUrl(),+ config.effectiveBaseUrl(),regionResolver);}diff --git a/src/main/resources/application.yml b/src/main/resources/application.ymlindex a45c4b8a71..09a87e13dd 100644--- a/src/main/resources/application.yml+++ b/src/main/resources/application.yml@@ -15,6 +15,8 @@ quarkus:floci:base-url: "http://localhost:4566"+ # hostname: "" # When set, overrides the hostname in response URLs (e.g. SQS QueueUrl).+ # Needed for multi-container Docker setups. Example: FLOCI_HOSTNAME=flocidefault-region: us-east-1default-account-id: "000000000000"storage: