diff --git a/connect/connect-http-sink/README.md b/connect/connect-http-sink/README.md index bc3466897..4fddeef61 100644 --- a/connect/connect-http-sink/README.md +++ b/connect/connect-http-sink/README.md @@ -1,16 +1,18 @@ # HTTP Sink connector - ## Objective Quickly test [HTTP Sink](https://docs.confluent.io/current/connect/kafka-connect-http/index.html#kconnect-long-http-sink-connector) connector. This is based on [Kafka Connect HTTP Connector Quick Start](https://docs.confluent.io/current/connect/kafka-connect-http/index.html#kconnect-long-http-connector-quick-start) -The HTTP service is using [vdesabou/kafka-connect-http-demo](https://github.com/vdesabou/kafka-connect-http-demo). +The HTTP service is using [vdesabou/kafka-connect-http-demo](https://github.com/vdesabou/kafka-connect-http-demo) when authentication is used, otherwise it use a simple http server where error code returned can be changed simply using: -Note: A great resource to test different HTTP status code is [http://httpstat.us](http://httpstat.us). +```bash +log "Set webserver to reply with 503" +curl -X PUT -H "Content-Type: application/json" --data '{"errorCode": 503}' http://localhost:9006 +``` ## How to run @@ -43,111 +45,3 @@ $ playground run -f http_ssl_basic_auth ```bash $ playground run -f http_mtls_auth ``` - -### JSON Converter Example - -```bash -$ playground run -f http_json_basic_auth -``` - -Sending using: - -``` -docker exec -i broker kafka-console-producer --broker-list broker:9092 --topic json-topic << EOF -{"customer_name":"Ed", "complaint_type":"Dirty car", "trip_cost": 29.10, "new_customer": false, "number_of_rides": 22} -EOF -``` - -Json data: - -```json -{ - "customer_name": "Ed", - "complaint_type": "Dirty car", - "trip_cost": 29.1, - "new_customer": false, - "number_of_rides": 22 -} -``` - -Getting: - -```bash -curl admin:password@localhost:9083/api/messages | jq . -[ - { - "id": 1, - "message": "[{\"complaint_type\":\"Dirty car\",\"new_customer\":false,\"trip_cost\":29.1,\"customer_name\":\"Ed\",\"number_of_rides\":22}]" - } -] -``` -### AVRO Converter Example - -``` -$ playground run -f http_avro_basic_auth -``` - -Sending using: - -```bash -playground topic produce -t avro-topic --nb-messages 10 --forced-value '{"f1":"value%g"}' << 'EOF' -{ - "type": "record", - "name": "myrecord", - "fields": [ - { - "name": "f1", - "type": "string" - } - ] -} -EOF -``` - -Getting: - -``` -curl admin:password@localhost:9083/api/messages | jq . -[ - { - "id": 1, - "message": "[{\"f1\":\"value1\"}]" - }, - { - "id": 2, - "message": "[{\"f1\":\"value2\"}]" - }, - { - "id": 3, - "message": "[{\"f1\":\"value3\"}]" - }, - { - "id": 4, - "message": "[{\"f1\":\"value4\"}]" - }, - { - "id": 5, - "message": "[{\"f1\":\"value5\"}]" - }, - { - "id": 6, - "message": "[{\"f1\":\"value6\"}]" - }, - { - "id": 7, - "message": "[{\"f1\":\"value7\"}]" - }, - { - "id": 8, - "message": "[{\"f1\":\"value8\"}]" - }, - { - "id": 9, - "message": "[{\"f1\":\"value9\"}]" - }, - { - "id": 10, - "message": "[{\"f1\":\"value10\"}]" - } -] -``` diff --git a/connect/connect-http-sink/docker-compose.plaintext copy.yml b/connect/connect-http-sink/docker-compose.plaintext copy.yml new file mode 100644 index 000000000..27db9dcfb --- /dev/null +++ b/connect/connect-http-sink/docker-compose.plaintext copy.yml @@ -0,0 +1,75 @@ +--- +version: '3.5' +services: + + connect: + volumes: + - ../../connect/connect-http-sink/security:/tmp + - ../../connect/connect-http-sink/jcl-over-slf4j-2.0.7.jar:/usr/share/confluent-hub-components/confluentinc-kafka-connect-http/lib/jcl-over-slf4j-2.0.7.jar + environment: + CONNECT_PLUGIN_PATH: /usr/share/confluent-hub-components/confluentinc-kafka-connect-http + + http-service-no-auth: + image: vdesabou/http-sink-demo # created from https://github.com/vdesabou/kafka-connect-http-demo with mvn clean install -DskipTests && docker build -t vdesabou/http-sink-demo:latest . && docker push vdesabou/http-sink-demo:latest + hostname: http-service-no-auth + container_name: http-service-no-auth + ports: + - "8080:8080" + environment: + SPRING_PROFILES_ACTIVE: 'simple-auth' + + http-service-basic-auth: + image: vdesabou/http-sink-demo + hostname: http-service-basic-auth + container_name: http-service-basic-auth + ports: + - "9083:8080" + environment: + SPRING_PROFILES_ACTIVE: 'basic-auth' + + # for testing error 500 + http-service-no-auth-500: + image: vdesabou/http-service-no-auth-500 + hostname: http-service-no-auth-500 + container_name: http-service-no-auth-500 + ports: + - "9082:8080" + environment: + SPRING_PROFILES_ACTIVE: 'simple-auth' + + # for testing error 204 + http-service-basic-auth-204: + image: vdesabou/http-sink-demo-error-204 + hostname: http-service-basic-auth-204 + container_name: http-service-basic-auth-204 + ports: + - "9081:8080" + environment: + SPRING_PROFILES_ACTIVE: 'basic-auth' + + http-service-oauth2-auth: + image: vdesabou/http-sink-demo + hostname: http-service-oauth2-auth + container_name: http-service-oauth2-auth + ports: + - "10080:8080" + environment: + SPRING_PROFILES_ACTIVE: 'oauth2' + + http-service-ssl-basic-auth: + image: vdesabou/http-sink-demo + hostname: http-service-ssl-basic-auth + container_name: http-service-ssl-basic-auth + ports: + - "8443:8443" + environment: + SPRING_PROFILES_ACTIVE: 'ssl-basic-auth' + + http-service-mtls-auth: + image: vdesabou/http-sink-demo + hostname: http-service-mtls-auth + container_name: http-service-mtls-auth + ports: + - "8643:8443" + environment: + SPRING_PROFILES_ACTIVE: 'ssl-auth' \ No newline at end of file diff --git a/connect/connect-http-sink/docker-compose.plaintext.basic.auth.yml b/connect/connect-http-sink/docker-compose.plaintext.basic.auth.yml new file mode 100644 index 000000000..6a8e770bb --- /dev/null +++ b/connect/connect-http-sink/docker-compose.plaintext.basic.auth.yml @@ -0,0 +1,19 @@ +--- +version: '3.5' +services: + + connect: + volumes: + - ../../connect/connect-http-sink/security:/tmp + - ../../connect/connect-http-sink/jcl-over-slf4j-2.0.7.jar:/usr/share/confluent-hub-components/confluentinc-kafka-connect-http/lib/jcl-over-slf4j-2.0.7.jar + environment: + CONNECT_PLUGIN_PATH: /usr/share/confluent-hub-components/confluentinc-kafka-connect-http + + http-service-basic-auth: + image: vdesabou/http-sink-demo + hostname: http-service-basic-auth + container_name: http-service-basic-auth + ports: + - "9083:8080" + environment: + SPRING_PROFILES_ACTIVE: 'basic-auth' diff --git a/connect/connect-http-sink/docker-compose.plaintext.mtls.yml b/connect/connect-http-sink/docker-compose.plaintext.mtls.yml new file mode 100644 index 000000000..309edccbc --- /dev/null +++ b/connect/connect-http-sink/docker-compose.plaintext.mtls.yml @@ -0,0 +1,19 @@ +--- +version: '3.5' +services: + + connect: + volumes: + - ../../connect/connect-http-sink/security:/tmp + - ../../connect/connect-http-sink/jcl-over-slf4j-2.0.7.jar:/usr/share/confluent-hub-components/confluentinc-kafka-connect-http/lib/jcl-over-slf4j-2.0.7.jar + environment: + CONNECT_PLUGIN_PATH: /usr/share/confluent-hub-components/confluentinc-kafka-connect-http + + http-service-mtls-auth: + image: vdesabou/http-sink-demo + hostname: http-service-mtls-auth + container_name: http-service-mtls-auth + ports: + - "8643:8443" + environment: + SPRING_PROFILES_ACTIVE: 'ssl-auth' \ No newline at end of file diff --git a/connect/connect-http-sink/docker-compose.plaintext.oauth2.yml b/connect/connect-http-sink/docker-compose.plaintext.oauth2.yml new file mode 100644 index 000000000..942ed4960 --- /dev/null +++ b/connect/connect-http-sink/docker-compose.plaintext.oauth2.yml @@ -0,0 +1,19 @@ +--- +version: '3.5' +services: + + connect: + volumes: + - ../../connect/connect-http-sink/security:/tmp + - ../../connect/connect-http-sink/jcl-over-slf4j-2.0.7.jar:/usr/share/confluent-hub-components/confluentinc-kafka-connect-http/lib/jcl-over-slf4j-2.0.7.jar + environment: + CONNECT_PLUGIN_PATH: /usr/share/confluent-hub-components/confluentinc-kafka-connect-http + + http-service-oauth2-auth: + image: vdesabou/http-sink-demo + hostname: http-service-oauth2-auth + container_name: http-service-oauth2-auth + ports: + - "10080:8080" + environment: + SPRING_PROFILES_ACTIVE: 'oauth2' diff --git a/connect/connect-http-sink/docker-compose.plaintext.proxy.yml b/connect/connect-http-sink/docker-compose.plaintext.proxy.yml index af7933ea5..8be0392c6 100644 --- a/connect/connect-http-sink/docker-compose.plaintext.proxy.yml +++ b/connect/connect-http-sink/docker-compose.plaintext.proxy.yml @@ -19,15 +19,6 @@ services: CONNECT_PLUGIN_PATH: /usr/share/confluent-hub-components/confluentinc-kafka-connect-http dns: 0.0.0.0 - http-service-no-auth: - image: vdesabou/http-sink-demo # created from https://github.com/vdesabou/kafka-connect-http-demo with mvn clean install -DskipTests && docker build -t vdesabou/http-sink-demo:latest . && docker push vdesabou/http-sink-demo:latest - hostname: http-service-no-auth - container_name: http-service-no-auth - ports: - - "8080:8080" - environment: - SPRING_PROFILES_ACTIVE: 'simple-auth' - http-service-basic-auth: image: vdesabou/http-sink-demo hostname: http-service-basic-auth @@ -36,50 +27,3 @@ services: - "9083:8080" environment: SPRING_PROFILES_ACTIVE: 'basic-auth' - - # for testing error 500 - http-service-no-auth-500: - image: vdesabou/http-service-no-auth-500 - hostname: http-service-no-auth-500 - container_name: http-service-no-auth-500 - ports: - - "9082:8080" - environment: - SPRING_PROFILES_ACTIVE: 'simple-auth' - - # for testing error 204 - http-service-basic-auth-204: - image: vdesabou/http-sink-demo-error-204 - hostname: http-service-basic-auth-204 - container_name: http-service-basic-auth-204 - ports: - - "9081:8080" - environment: - SPRING_PROFILES_ACTIVE: 'basic-auth' - - http-service-oauth2-auth: - image: vdesabou/http-sink-demo - hostname: http-service-oauth2-auth - container_name: http-service-oauth2-auth - ports: - - "10080:8080" - environment: - SPRING_PROFILES_ACTIVE: 'oauth2' - - http-service-ssl-basic-auth: - image: vdesabou/http-sink-demo - hostname: http-service-ssl-basic-auth - container_name: http-service-ssl-basic-auth - ports: - - "8443:8443" - environment: - SPRING_PROFILES_ACTIVE: 'ssl-basic-auth' - - http-service-mtls-auth: - image: vdesabou/http-sink-demo - hostname: http-service-mtls-auth - container_name: http-service-mtls-auth - ports: - - "8643:8443" - environment: - SPRING_PROFILES_ACTIVE: 'ssl-auth' \ No newline at end of file diff --git a/connect/connect-http-sink/docker-compose.plaintext.ssl.basic.auth.yml b/connect/connect-http-sink/docker-compose.plaintext.ssl.basic.auth.yml new file mode 100644 index 000000000..4f1e4435b --- /dev/null +++ b/connect/connect-http-sink/docker-compose.plaintext.ssl.basic.auth.yml @@ -0,0 +1,19 @@ +--- +version: '3.5' +services: + + connect: + volumes: + - ../../connect/connect-http-sink/security:/tmp + - ../../connect/connect-http-sink/jcl-over-slf4j-2.0.7.jar:/usr/share/confluent-hub-components/confluentinc-kafka-connect-http/lib/jcl-over-slf4j-2.0.7.jar + environment: + CONNECT_PLUGIN_PATH: /usr/share/confluent-hub-components/confluentinc-kafka-connect-http + + http-service-ssl-basic-auth: + image: vdesabou/http-sink-demo + hostname: http-service-ssl-basic-auth + container_name: http-service-ssl-basic-auth + ports: + - "8443:8443" + environment: + SPRING_PROFILES_ACTIVE: 'ssl-basic-auth' diff --git a/connect/connect-http-sink/docker-compose.plaintext.yml b/connect/connect-http-sink/docker-compose.plaintext.yml index 27db9dcfb..a5bb352ac 100644 --- a/connect/connect-http-sink/docker-compose.plaintext.yml +++ b/connect/connect-http-sink/docker-compose.plaintext.yml @@ -9,67 +9,10 @@ services: environment: CONNECT_PLUGIN_PATH: /usr/share/confluent-hub-components/confluentinc-kafka-connect-http - http-service-no-auth: - image: vdesabou/http-sink-demo # created from https://github.com/vdesabou/kafka-connect-http-demo with mvn clean install -DskipTests && docker build -t vdesabou/http-sink-demo:latest . && docker push vdesabou/http-sink-demo:latest - hostname: http-service-no-auth - container_name: http-service-no-auth + httpserver: + build: + context: ../../connect/connect-http-sink/httpserver + hostname: httpserver + container_name: httpserver ports: - - "8080:8080" - environment: - SPRING_PROFILES_ACTIVE: 'simple-auth' - - http-service-basic-auth: - image: vdesabou/http-sink-demo - hostname: http-service-basic-auth - container_name: http-service-basic-auth - ports: - - "9083:8080" - environment: - SPRING_PROFILES_ACTIVE: 'basic-auth' - - # for testing error 500 - http-service-no-auth-500: - image: vdesabou/http-service-no-auth-500 - hostname: http-service-no-auth-500 - container_name: http-service-no-auth-500 - ports: - - "9082:8080" - environment: - SPRING_PROFILES_ACTIVE: 'simple-auth' - - # for testing error 204 - http-service-basic-auth-204: - image: vdesabou/http-sink-demo-error-204 - hostname: http-service-basic-auth-204 - container_name: http-service-basic-auth-204 - ports: - - "9081:8080" - environment: - SPRING_PROFILES_ACTIVE: 'basic-auth' - - http-service-oauth2-auth: - image: vdesabou/http-sink-demo - hostname: http-service-oauth2-auth - container_name: http-service-oauth2-auth - ports: - - "10080:8080" - environment: - SPRING_PROFILES_ACTIVE: 'oauth2' - - http-service-ssl-basic-auth: - image: vdesabou/http-sink-demo - hostname: http-service-ssl-basic-auth - container_name: http-service-ssl-basic-auth - ports: - - "8443:8443" - environment: - SPRING_PROFILES_ACTIVE: 'ssl-basic-auth' - - http-service-mtls-auth: - image: vdesabou/http-sink-demo - hostname: http-service-mtls-auth - container_name: http-service-mtls-auth - ports: - - "8643:8443" - environment: - SPRING_PROFILES_ACTIVE: 'ssl-auth' \ No newline at end of file + - "9006:9006" \ No newline at end of file diff --git a/connect/connect-http-sink/http_avro_basic_auth.sh b/connect/connect-http-sink/http_avro_basic_auth.sh deleted file mode 100755 index 8dd160148..000000000 --- a/connect/connect-http-sink/http_avro_basic_auth.sh +++ /dev/null @@ -1,60 +0,0 @@ -#!/bin/bash -set -e - -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" -source ${DIR}/../../scripts/utils.sh - -if [ ! -f jcl-over-slf4j-2.0.7.jar ] -then - wget https://repo1.maven.org/maven2/org/slf4j/jcl-over-slf4j/2.0.7/jcl-over-slf4j-2.0.7.jar -fi - -${DIR}/../../environment/plaintext/start.sh "${PWD}/docker-compose.plaintext.yml" - -log "Sending messages to topic avro-topic" -playground topic produce -t avro-topic --nb-messages 10 --forced-value '{"f1":"value%g"}' << 'EOF' -{ - "type": "record", - "name": "myrecord", - "fields": [ - { - "name": "f1", - "type": "string" - } - ] -} -EOF - -playground debug log-level set --package "org.apache.http" --level TRACE - -log "Creating http-sink connector" -playground connector create-or-update --connector http-sink << EOF -{ - "topics": "avro-topic", - "tasks.max": "1", - "connector.class": "io.confluent.connect.http.HttpSinkConnector", - "key.converter": "org.apache.kafka.connect.storage.StringConverter", - "value.converter": "io.confluent.connect.avro.AvroConverter", - "value.converter.schema.registry.url":"http://schema-registry:8081", - "confluent.topic.bootstrap.servers": "broker:9092", - "confluent.topic.replication.factor": "1", - "reporter.bootstrap.servers": "broker:9092", - "reporter.error.topic.name": "error-responses", - "reporter.error.topic.replication.factor": 1, - "reporter.result.topic.name": "success-responses", - "reporter.result.topic.replication.factor": 1, - "http.api.url": "http://http-service-basic-auth:8080/api/messages", - "request.body.format": "json", - "auth.type": "BASIC", - "connection.user": "admin", - "connection.password": "password" -} -EOF - - -sleep 10 - -log "Confirm that the data was sent to the HTTP endpoint." -curl admin:password@localhost:9083/api/messages | jq . > /tmp/result.log 2>&1 -cat /tmp/result.log -grep "value1" /tmp/result.log diff --git a/connect/connect-http-sink/http_basic_auth.sh b/connect/connect-http-sink/http_basic_auth.sh index 00f2fc70b..17885b08e 100755 --- a/connect/connect-http-sink/http_basic_auth.sh +++ b/connect/connect-http-sink/http_basic_auth.sh @@ -9,7 +9,7 @@ then wget https://repo1.maven.org/maven2/org/slf4j/jcl-over-slf4j/2.0.7/jcl-over-slf4j-2.0.7.jar fi -${DIR}/../../environment/plaintext/start.sh "${PWD}/docker-compose.plaintext.yml" +${DIR}/../../environment/plaintext/start.sh "${PWD}/docker-compose.plaintext.basic.auth.yml" log "Sending messages to topic http-messages" playground topic produce -t http-messages --nb-messages 10 << 'EOF' diff --git a/connect/connect-http-sink/http_json_basic_auth.sh b/connect/connect-http-sink/http_json_basic_auth.sh deleted file mode 100755 index 9f162d0ef..000000000 --- a/connect/connect-http-sink/http_json_basic_auth.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/bash -set -e - -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" -source ${DIR}/../../scripts/utils.sh - -if [ ! -f jcl-over-slf4j-2.0.7.jar ] -then - wget https://repo1.maven.org/maven2/org/slf4j/jcl-over-slf4j/2.0.7/jcl-over-slf4j-2.0.7.jar -fi - -${DIR}/../../environment/plaintext/start.sh "${PWD}/docker-compose.plaintext.yml" - -log "Sending messages to topic json-topic" -playground topic produce -t json-topic << 'EOF' -{"customer_name":"Ed", "complaint_type":"Dirty car", "trip_cost": 29.10, "new_customer": false, "number_of_rides": 22} -EOF - -playground debug log-level set --package "org.apache.http" --level TRACE - -log "Creating http-sink connector" -playground connector create-or-update --connector http-sink << EOF -{ - "topics": "json-topic", - "tasks.max": "1", - "connector.class": "io.confluent.connect.http.HttpSinkConnector", - "key.converter": "org.apache.kafka.connect.storage.StringConverter", - "value.converter": "org.apache.kafka.connect.json.JsonConverter", - "value.converter.schemas.enable": "false", - "confluent.topic.bootstrap.servers": "broker:9092", - "confluent.topic.replication.factor": "1", - "reporter.bootstrap.servers": "broker:9092", - "reporter.error.topic.name": "error-responses", - "reporter.error.topic.replication.factor": 1, - "reporter.result.topic.name": "success-responses", - "reporter.result.topic.replication.factor": 1, - "http.api.url": "http://http-service-basic-auth:8080/api/messages", - "request.body.format": "json", - "auth.type": "BASIC", - "connection.user": "admin", - "connection.password": "password" -} -EOF - -sleep 10 - -log "Confirm that the data was sent to the HTTP endpoint." -curl admin:password@localhost:9083/api/messages | jq . > /tmp/result.log 2>&1 -cat /tmp/result.log -grep "Dirty car" /tmp/result.log - diff --git a/connect/connect-http-sink/http_mtls_auth.sh b/connect/connect-http-sink/http_mtls_auth.sh index bfa3beac3..42580ac0f 100755 --- a/connect/connect-http-sink/http_mtls_auth.sh +++ b/connect/connect-http-sink/http_mtls_auth.sh @@ -9,7 +9,7 @@ then wget https://repo1.maven.org/maven2/org/slf4j/jcl-over-slf4j/2.0.7/jcl-over-slf4j-2.0.7.jar fi -${DIR}/../../environment/plaintext/start.sh "${PWD}/docker-compose.plaintext.yml" +${DIR}/../../environment/plaintext/start.sh "${PWD}/docker-compose.plaintext.mtls.yml" log "Sending messages to topic http-messages" playground topic produce -t http-messages --nb-messages 10 << 'EOF' diff --git a/connect/connect-http-sink/http_no_auth.sh b/connect/connect-http-sink/http_no_auth.sh index 99fd87951..fa59b4dfb 100755 --- a/connect/connect-http-sink/http_no_auth.sh +++ b/connect/connect-http-sink/http_no_auth.sh @@ -19,6 +19,9 @@ EOF playground debug log-level set --package "org.apache.http" --level TRACE +log "Set webserver to reply with 200" +curl -X PUT -H "Content-Type: application/json" --data '{"errorCode": 200}' http://localhost:9006 + log "Creating http-sink connector" playground connector create-or-update --connector http-sink << EOF { @@ -34,7 +37,7 @@ playground connector create-or-update --connector http-sink << EOF "reporter.error.topic.replication.factor": 1, "reporter.result.topic.name": "success-responses", "reporter.result.topic.replication.factor": 1, - "http.api.url": "http://http-service-no-auth:8080/api/messages", + "http.api.url": "http://httpserver:9006", "batch.max.size": "10" } EOF @@ -42,11 +45,6 @@ EOF sleep 10 -log "Confirm that the data was sent to the HTTP endpoint." -curl localhost:8080/api/messages | jq . > /tmp/result.log 2>&1 -cat /tmp/result.log -grep "10" /tmp/result.log - log "Check the success-responses topic" playground topic consume --topic success-responses --min-expected-messages 10 --timeout 60 # input_record_offset:0,input_record_timestamp:1645173514858,input_record_partition:0,input_record_topic:http-messages "{\"id\":1,\"message\":\"1,2,3,4,5,6,7,8,9,10\"}" diff --git a/connect/connect-http-sink/http_oauth2_auth.sh b/connect/connect-http-sink/http_oauth2_auth.sh index e77207dda..ade635701 100755 --- a/connect/connect-http-sink/http_oauth2_auth.sh +++ b/connect/connect-http-sink/http_oauth2_auth.sh @@ -9,7 +9,7 @@ then wget https://repo1.maven.org/maven2/org/slf4j/jcl-over-slf4j/2.0.7/jcl-over-slf4j-2.0.7.jar fi -${DIR}/../../environment/plaintext/start.sh "${PWD}/docker-compose.plaintext.yml" +${DIR}/../../environment/plaintext/start.sh "${PWD}/docker-compose.plaintext.oauth2.yml" log "Sending messages to topic http-messages" diff --git a/connect/connect-http-sink/http_ssl_basic_auth.sh b/connect/connect-http-sink/http_ssl_basic_auth.sh index d6ba56a87..bdff817f8 100755 --- a/connect/connect-http-sink/http_ssl_basic_auth.sh +++ b/connect/connect-http-sink/http_ssl_basic_auth.sh @@ -9,7 +9,7 @@ then wget https://repo1.maven.org/maven2/org/slf4j/jcl-over-slf4j/2.0.7/jcl-over-slf4j-2.0.7.jar fi -${DIR}/../../environment/plaintext/start.sh "${PWD}/docker-compose.plaintext.yml" +${DIR}/../../environment/plaintext/start.sh "${PWD}/docker-compose.plaintext.ssl.basic.auth.yml" log "Sending messages to topic http-messages" diff --git a/connect/connect-http-sink/httpserver/Dockerfile b/connect/connect-http-sink/httpserver/Dockerfile new file mode 100644 index 000000000..4f01a8f24 --- /dev/null +++ b/connect/connect-http-sink/httpserver/Dockerfile @@ -0,0 +1,7 @@ +FROM node:14 +WORKDIR /app +COPY package*.json ./ +RUN npm install +COPY . . +EXPOSE 9006 +CMD [ "node", "server.js" ] \ No newline at end of file diff --git a/connect/connect-http-sink/httpserver/package.json b/connect/connect-http-sink/httpserver/package.json new file mode 100644 index 000000000..c761e5dc3 --- /dev/null +++ b/connect/connect-http-sink/httpserver/package.json @@ -0,0 +1,13 @@ +{ + "name": "http-error-code-server", + "version": "1.0.0", + "description": "HTTP server returning configurable error codes", + "main": "server.js", + "scripts": { + "start": "node server.js" + }, + "dependencies": { + "express": "^4.17.1", + "body-parser": "^1.20.2" + } +} diff --git a/connect/connect-http-sink/httpserver/server.js b/connect/connect-http-sink/httpserver/server.js new file mode 100644 index 000000000..9c761b2dc --- /dev/null +++ b/connect/connect-http-sink/httpserver/server.js @@ -0,0 +1,33 @@ +const express = require('express'); +const app = express(); + +app.use(express.json()); + +app.use((req, res, next) => { + console.log(`[${new Date().toISOString()}] ${req.method} to ${req.url}`); + next(); +}); + +// https://stackoverflow.com/questions/9177049/express-js-req-body-undefined +var errorCode = 200; + +app.get('/', (req, res) => { + res.status(errorCode).json({ message: `Returned status: ${errorCode}` }); + console.log(`[${new Date().toISOString()}] ${req.body}: sending back ${errorCode}`); +}); + +app.post('/', (req, res) => { + res.status(errorCode).json({ message: `Returned status: ${errorCode}` }); + console.log(`[${new Date().toISOString()}] ${req.body}: sending back ${errorCode}`); +}); + +app.put('/', (req, res) => { + if(req.body.errorCode && typeof req.body.errorCode === "number"){ + errorCode = req.body.errorCode; + res.status(200).json({ message: `Error code is now: ${errorCode}` }); + } else { + res.status(400).json({ message: 'Please provide errorCode in body as number'}); + } +}); + +app.listen(9006, () => console.log('Server running...'));