Skip to content

Commit

Permalink
Merge pull request #25 from hasanheroglu/24-parameterize-hostname-and…
Browse files Browse the repository at this point in the history
…-port

add parameterized hostnames and ports
  • Loading branch information
egekorkan authored Jul 26, 2024
2 parents f96961f + e8cbe44 commit af90e3d
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 35 deletions.
16 changes: 16 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -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"
42 changes: 33 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion things/calculator/coap/js/Dockerfile-contentneg
Original file line number Diff line number Diff line change
Expand Up @@ -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
EXPOSE ${PORT}/udp
4 changes: 3 additions & 1 deletion things/calculator/coap/js/Dockerfile-simple
Original file line number Diff line number Diff line change
Expand Up @@ -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
EXPOSE ${PORT}/udp
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions things/calculator/coap/js/coap-simple-calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 3 additions & 1 deletion things/calculator/http/express/Dockerfile-contentneg
Original file line number Diff line number Diff line change
Expand Up @@ -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
EXPOSE ${PORT}
4 changes: 3 additions & 1 deletion things/calculator/http/express/Dockerfile-simple
Original file line number Diff line number Diff line change
Expand Up @@ -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
EXPOSE ${PORT}
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
4 changes: 2 additions & 2 deletions things/calculator/http/express/http-simple-calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
4 changes: 3 additions & 1 deletion things/calculator/http/flask/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
EXPOSE ${PORT}
6 changes: 6 additions & 0 deletions things/calculator/http/flask/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions things/calculator/mqtt/js/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ RUN npm install
ENV TM_PATH="./calculator.tm.json"

CMD ["node", "main.js"]

EXPOSE 1883
8 changes: 4 additions & 4 deletions things/calculator/mqtt/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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

Expand All @@ -43,7 +43,7 @@ placeholderReplacer.addVariableMap({
PROPERTIES,
ACTIONS,
EVENTS,
HOSTNAME: hostname,
HOSTNAME: brokerURI,
PORT_NUMBER: portNumber,
RESULT_OBSERVABLE: true,
LAST_CHANGE_OBSERVABLE: true
Expand Down
5 changes: 4 additions & 1 deletion things/elevator/modbus/js/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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}
9 changes: 6 additions & 3 deletions things/elevator/modbus/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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)
});
});

0 comments on commit af90e3d

Please sign in to comment.