diff --git a/.env b/.env new file mode 100644 index 0000000..7eb8cc3 --- /dev/null +++ b/.env @@ -0,0 +1,16 @@ +# IN PORTS +WEB_PORT_IN=80 +MODBUS_ELEVATOR_PORT_IN=3179 +COAP_SIMPLE_PORT_IN=5683 +COAP_NEGOTIATION_PORT_IN=5684 +TRAEFIK_DASHBOARD_PORT_IN=8080 + +# OUT PORTS +WEB_PORT_OUT=80 +MODBUS_ELEVATOR_PORT_OUT=3179 +COAP_SIMPLE_PORT_OUT=5683 +COAP_NEGOTIATION_PORT_OUT=5684 +TRAEFIK_DASHBOARD_PORT_OUT=8080 + +HOSTNAME="localhost" +BROKER_URI="test.mosquitto.org" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index c54c674..4a4621b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,30 +6,50 @@ services: build: context: ./things/calculator/ dockerfile: ./coap/js/Dockerfile-simple + args: + - PORT_ARG=${COAP_SIMPLE_PORT_OUT} + environment: + - HOSTNAME=${HOSTNAME} coap-calculator-content-negotiation: labels: - traefik.udp.routers.coap-calculator-content-negotiation.entrypoints=coap-calculator-content-negotiation build: context: ./things/calculator/ dockerfile: ./coap/js/Dockerfile-contentneg + args: + - PORT_ARG=${COAP_NEGOTIATION_PORT_OUT} + environment: + - HOSTNAME=${HOSTNAME} http-express-calculator-simple: labels: - traefik.http.routers.http-express-calculator-simple.rule=PathPrefix(`/http-express-calculator-simple`) build: context: ./things/calculator/ dockerfile: ./http/express/Dockerfile-simple + args: + - PORT_ARG=${WEB_PORT_OUT} + environment: + - HOSTNAME=${HOSTNAME} http-express-calculator-content-negotiation: labels: - traefik.http.routers.http-express-calculator-content-negotiation.rule=PathPrefix(`/http-express-calculator-content-negotiation`) build: context: ./things/calculator/ dockerfile: ./http/express/Dockerfile-contentneg + args: + - PORT_ARG=${WEB_PORT_OUT} + environment: + - HOSTNAME=${HOSTNAME} http-flask-calculator: labels: - traefik.http.routers.http-flask-calculator.rule=PathPrefix(`/http-flask-calculator`) build: context: ./things/calculator/ dockerfile: ./http/flask/Dockerfile + args: + - PORT_ARG=${WEB_PORT_OUT} + environment: + - HOSTNAME=${HOSTNAME} mqtt-calculator: build: context: ./things/calculator/ @@ -38,25 +58,29 @@ services: labels: - traefik.tcp.routers.modbus-elevator.entrypoints=modbus-elevator - traefik.tcp.routers.modbus-elevator.rule=HostSNI(`*`) + environment: + - BROKER_URI=${BROKER_URI} build: context: ./things/elevator/ dockerfile: ./modbus/js/Dockerfile + args: + - PORT_ARG=${MODBUS_ELEVATOR_PORT_OUT} reverse-proxy: image: traefik:v3.0 command: - "--log.level=DEBUG" - "--api.insecure=true" - "--providers.docker=true" - - "--entrypoints.web.address=:80" - - "--entrypoints.modbus-elevator.address=:3179" - - "--entrypoints.coap-calculator-simple.address=:5683/udp" - - "--entrypoints.coap-calculator-content-negotiation.address=:5684/udp" + - "--entrypoints.web.address=:${WEB_PORT_IN}" + - "--entrypoints.modbus-elevator.address=:${MODBUS_ELEVATOR_PORT_IN}" + - "--entrypoints.coap-calculator-simple.address=:${COAP_SIMPLE_PORT_IN}/udp" + - "--entrypoints.coap-calculator-content-negotiation.address=:${COAP_NEGOTIATION_PORT_IN}/udp" ports: - - "80:80" - - "3179:3179" - - "5683:5683/udp" - - "5684:5684/udp" - - "8080:8080" + - "${WEB_PORT_OUT}:${WEB_PORT_IN}" + - "${MODBUS_ELEVATOR_PORT_OUT}:${MODBUS_ELEVATOR_PORT_IN}" + - "${COAP_SIMPLE_PORT_OUT}:${COAP_SIMPLE_PORT_IN}/udp" + - "${COAP_NEGOTIATION_PORT_OUT}:${COAP_NEGOTIATION_PORT_IN}/udp" + - "${TRAEFIK_DASHBOARD_PORT_OUT}:${TRAEFIK_DASHBOARD_PORT_IN}" volumes: # So that Traefik can listen to the Docker events - /var/run/docker.sock:/var/run/docker.sock diff --git a/package-lock.json b/package-lock.json index 54df27f..7dc5f8e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,9 +10,9 @@ "./things/*/*/*" ], "dependencies": { - "@node-wot/binding-coap": "^0.8.13", - "@node-wot/binding-http": "^0.8.13", - "@node-wot/core": "^0.8.13" + "@node-wot/binding-coap": "0.8.13", + "@node-wot/binding-http": "0.8.13", + "@node-wot/core": "0.8.13" }, "devDependencies": { "@types/express": "^4.17.17", diff --git a/things/calculator/coap/js/Dockerfile-contentneg b/things/calculator/coap/js/Dockerfile-contentneg index 70f1c8e..bf7437a 100644 --- a/things/calculator/coap/js/Dockerfile-contentneg +++ b/things/calculator/coap/js/Dockerfile-contentneg @@ -6,8 +6,10 @@ COPY ./coap/js . RUN npm install +ARG PORT_ARG=5684 +ENV PORT=${PORT_ARG} ENV TM_PATH="./calculator.tm.json" CMD ["node", "coap-content-negotiation-calculator.js"] -EXPOSE 5684/udp \ No newline at end of file +EXPOSE ${PORT}/udp \ No newline at end of file diff --git a/things/calculator/coap/js/Dockerfile-simple b/things/calculator/coap/js/Dockerfile-simple index 2cb265c..e04b669 100644 --- a/things/calculator/coap/js/Dockerfile-simple +++ b/things/calculator/coap/js/Dockerfile-simple @@ -6,8 +6,10 @@ COPY ./coap/js . RUN npm install +ARG PORT_ARG=5683 +ENV PORT=${PORT_ARG} ENV TM_PATH="./calculator.tm.json" CMD ["node", "coap-simple-calculator.js"] -EXPOSE 5683/udp \ No newline at end of file +EXPOSE ${PORT}/udp \ No newline at end of file diff --git a/things/calculator/coap/js/coap-content-negotiation-calculator.js b/things/calculator/coap/js/coap-content-negotiation-calculator.js index c5e5138..a16b668 100644 --- a/things/calculator/coap/js/coap-content-negotiation-calculator.js +++ b/things/calculator/coap/js/coap-content-negotiation-calculator.js @@ -7,8 +7,8 @@ const cbor = require('cbor') require('dotenv').config() const server = coap.createServer() -const hostname = 'localhost' -let portNumber = 5684 +const hostname = process.env.HOSTNAME ?? 'localhost' +let portNumber = process.env.PORT ?? 5684 const thingName = 'coap-calculator-content-negotiation' const { values: { port } } = parseArgs({ @@ -217,6 +217,7 @@ server.on('request', (req, res) => { } else { if (!segments[2]) { if (req.method === 'GET') { + //FIXME: No null check for acceptHeaders if (acceptHeaders.includes('application/json') || acceptHeaders.includes('application/td+json') || acceptHeaders.includes('application/*') || acceptHeaders === '*/*') { res.setOption('Content-Format', 'application/json') res.end(JSON.stringify(thingDescription)) diff --git a/things/calculator/coap/js/coap-simple-calculator.js b/things/calculator/coap/js/coap-simple-calculator.js index 802888d..d347803 100644 --- a/things/calculator/coap/js/coap-simple-calculator.js +++ b/things/calculator/coap/js/coap-simple-calculator.js @@ -6,8 +6,8 @@ const { JsonPlaceholderReplacer } = require('json-placeholder-replacer') require('dotenv').config() const server = coap.createServer() -const hostname = 'localhost' -let portNumber = 5683 +const hostname = process.env.HOSTNAME ?? 'localhost' +let portNumber = process.env.PORT ?? 5683 const thingName = 'coap-calculator-simple' const { values: { port } } = parseArgs({ diff --git a/things/calculator/http/express/Dockerfile-contentneg b/things/calculator/http/express/Dockerfile-contentneg index 4c304ad..0ff6da9 100644 --- a/things/calculator/http/express/Dockerfile-contentneg +++ b/things/calculator/http/express/Dockerfile-contentneg @@ -6,8 +6,10 @@ COPY ./http/express . RUN npm install +ARG PORT_ARG=3001 +ENV PORT=${PORT_ARG} ENV TM_PATH="./calculator.tm.json" CMD ["node", "http-content-negotiation-calculator.js"] -EXPOSE 3001 \ No newline at end of file +EXPOSE ${PORT} \ No newline at end of file diff --git a/things/calculator/http/express/Dockerfile-simple b/things/calculator/http/express/Dockerfile-simple index 8382623..b194e7e 100644 --- a/things/calculator/http/express/Dockerfile-simple +++ b/things/calculator/http/express/Dockerfile-simple @@ -6,8 +6,10 @@ COPY ./http/express . RUN npm install +ARG PORT_ARG=3000 +ENV PORT=${PORT_ARG} ENV TM_PATH="./calculator.tm.json" CMD ["node", "http-simple-calculator.js"] -EXPOSE 3000 \ No newline at end of file +EXPOSE ${PORT} \ No newline at end of file diff --git a/things/calculator/http/express/http-content-negotiation-calculator.js b/things/calculator/http/express/http-content-negotiation-calculator.js index bbc15b0..e51644d 100644 --- a/things/calculator/http/express/http-content-negotiation-calculator.js +++ b/things/calculator/http/express/http-content-negotiation-calculator.js @@ -10,8 +10,8 @@ require('dotenv').config() const app = express() app.use(express.json({ strict: false })); -const hostname = 'localhost' -let portNumber = 3001 +const hostname = process.env.HOSTNAME ?? 'localhost' +let portNumber = process.env.PORT ?? 3001 const thingName = 'http-express-calculator-content-negotiation' const TDEndPoint = `/${thingName}`, diff --git a/things/calculator/http/express/http-simple-calculator.js b/things/calculator/http/express/http-simple-calculator.js index 8f414be..84763e2 100644 --- a/things/calculator/http/express/http-simple-calculator.js +++ b/things/calculator/http/express/http-simple-calculator.js @@ -9,8 +9,8 @@ require("dotenv").config(); const app = express(); app.use(express.json({ strict: false })); -const hostname = "localhost"; -let portNumber = 3000; +const hostname = process.env.HOSTNAME ?? "localhost"; +let portNumber = process.env.PORT ?? 3000; const thingName = "http-express-calculator-simple"; const TDEndPoint = `/${thingName}`, diff --git a/things/calculator/http/flask/Dockerfile b/things/calculator/http/flask/Dockerfile index fecd32c..00866c7 100644 --- a/things/calculator/http/flask/Dockerfile +++ b/things/calculator/http/flask/Dockerfile @@ -7,10 +7,12 @@ WORKDIR /app COPY ./calculator.tm.json . COPY ./http/flask . +ARG PORT_ARG=5000 +ENV PORT=${PORT_ARG} ENV TM_PATH=./calculator.tm.json RUN poetry install CMD [ "poetry", "run", "python", "main.py" ] -EXPOSE 5000 \ No newline at end of file +EXPOSE ${PORT} \ No newline at end of file diff --git a/things/calculator/http/flask/main.py b/things/calculator/http/flask/main.py index 7fd2eec..6a348ab 100644 --- a/things/calculator/http/flask/main.py +++ b/things/calculator/http/flask/main.py @@ -25,6 +25,12 @@ hostname = "0.0.0.0" portNumber = 5000 +if "HOSTNAME" in os.environ: + hostname = os.environ["HOSTNAME"] + +if "PORT" in os.environ: + portNumber = os.environ["PORT"] + thingName = "http-flask-calculator" PROPERTIES = "properties" ACTIONS = "actions" diff --git a/things/calculator/mqtt/js/Dockerfile b/things/calculator/mqtt/js/Dockerfile index 6862080..09e09b6 100644 --- a/things/calculator/mqtt/js/Dockerfile +++ b/things/calculator/mqtt/js/Dockerfile @@ -9,5 +9,3 @@ RUN npm install ENV TM_PATH="./calculator.tm.json" CMD ["node", "main.js"] - -EXPOSE 1883 \ No newline at end of file diff --git a/things/calculator/mqtt/js/main.js b/things/calculator/mqtt/js/main.js index 2c6ff73..9d86cd0 100644 --- a/things/calculator/mqtt/js/main.js +++ b/things/calculator/mqtt/js/main.js @@ -5,8 +5,8 @@ const path = require('path') const { JsonPlaceholderReplacer } = require('json-placeholder-replacer') require('dotenv').config() -const hostname = 'test.mosquitto.org' -let portNumber = 1883 +const brokerURI = process.env.BROKER_URI ?? 'test.mosquitto.org' +let portNumber = process.env.PORT ?? 1883 const { values: { port } } = parseArgs({ options: { @@ -26,7 +26,7 @@ const PROPERTIES = 'properties' const ACTIONS = 'actions' const EVENTS = 'events' -const broker = mqtt.connect(`mqtt://${hostname}`, { port: portNumber }) +const broker = mqtt.connect(`mqtt://${brokerURI}`, { port: portNumber }) const tmPath = process.env.TM_PATH @@ -43,7 +43,7 @@ placeholderReplacer.addVariableMap({ PROPERTIES, ACTIONS, EVENTS, - HOSTNAME: hostname, + HOSTNAME: brokerURI, PORT_NUMBER: portNumber, RESULT_OBSERVABLE: true, LAST_CHANGE_OBSERVABLE: true diff --git a/things/elevator/modbus/js/Dockerfile b/things/elevator/modbus/js/Dockerfile index 7cf9f84..6bb1698 100644 --- a/things/elevator/modbus/js/Dockerfile +++ b/things/elevator/modbus/js/Dockerfile @@ -6,7 +6,10 @@ COPY ./modbus/js . RUN npm install +ARG PORT_ARG=8502 +ENV PORT=${PORT_ARG} ENV TM_PATH="./elevator.tm.json" CMD ["node", "main.js"] -EXPOSE 8502 + +EXPOSE ${PORT} diff --git a/things/elevator/modbus/js/main.js b/things/elevator/modbus/js/main.js index 378beff..c3f8647 100644 --- a/things/elevator/modbus/js/main.js +++ b/things/elevator/modbus/js/main.js @@ -6,8 +6,11 @@ const { parseArgs } = require('node:util') require('dotenv').config() const thingName = "modbus-elevator" -const hostname = "0.0.0.0" -let portNumber = "8502" +// The following is needed since the modbus library we use does not support localhost but does support 0.0.0.0 +const hostname = process.env.HOSTNAME + ? process.env.HOSTNAME === "localhost" ? "0.0.0.0" : process.env.HOSTNAME + : "0.0.0.0" +let portNumber = process.env.PORT ?? "8502" const thingUnitID = 1 const { values: { port } } = parseArgs({ @@ -189,4 +192,4 @@ const serverTCP = new ServerTCP(vector, { host: hostname, port: portNumber, debu serverTCP.on("socketError", function(err){ // Handle socket error if needed, can be ignored console.error(err) -}); \ No newline at end of file +});