feat: docker support

This commit is contained in:
Morten Olsen
2025-10-16 14:54:52 +02:00
parent 1f4b9cab04
commit b75866bbdf
9 changed files with 91 additions and 11 deletions

6
.dockerignore Normal file
View File

@@ -0,0 +1,6 @@
*
!/bin/
!/src
!/package.json
!/pnpm-lock.yaml
!/tsconfig.json

View File

@@ -0,0 +1,8 @@
FROM node:23-slim
RUN corepack enable
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile --prod
COPY . .
RUN chmod +x /app/bin/start.js
CMD ["/app/bin/start.js"]

2
bin/start.js Normal file
View File

@@ -0,0 +1,2 @@
#!/usr/local/bin/node --no-warnings
import '../src/start.ts';

12
docker-compose.yaml Normal file
View File

@@ -0,0 +1,12 @@
name: backbone
services:
app:
build:
context: .
environment:
HTTP_ENABLED: 'true'
TCP_ENABLED: 'true'
TOKEN_SECRET: 'test'
ports:
- 1883:1883
- 8883:8883

View File

@@ -2,6 +2,7 @@
"type": "module",
"main": "dist/exports.js",
"scripts": {
"dev": "node --no-warnings --watch src/start.ts",
"test:lint": "eslint",
"build": "tsc --build",
"test:unit": "vitest --run --passWithNoTests",

View File

@@ -1,4 +1,5 @@
import { AccessHandler } from './access/access.handler.ts';
import { Config } from './config/config.ts';
import { K8sService } from './k8s/k8s.ts';
import { MqttServer } from './server/server.ts';
import { TopicsHandler } from './topics/topics.handler.ts';
@@ -15,6 +16,10 @@ class Backbone {
return this.#services;
}
public get config() {
return this.services.get(Config);
}
public get server() {
return this.#services.get(MqttServer);
}
@@ -31,10 +36,29 @@ class Backbone {
return this.#services.get(K8sService);
}
public start = async () => {
if (this.config.k8s.enabled) {
await this.setupK8sOperator();
}
if (this.config.http.enabled) {
console.log('starting http');
const http = await this.server.getHttpServer();
http.listen({ port: this.config.http.port, host: '0.0.0.0' });
}
if (this.config.tcp) {
const tcp = this.server.getTcpServer();
tcp.listen(this.config.tcp.port);
}
};
public setupK8sOperator = async () => {
await this.k8s.setup();
this.accessHandler.register('k8s', this.k8s.clients);
};
public destroy = async () => {
await this.services.destroy();
};
}
export { Backbone };

32
src/config/config.ts Normal file
View File

@@ -0,0 +1,32 @@
class Config {
public get tokenSecret() {
return process.env.TOKEN_SECRET;
}
public get k8s() {
const enabled = process.env.K8S_ENABLED === 'true';
return {
enabled,
};
}
public get http() {
const enabled = (process.env.HTTP_ENABLED = 'true');
const port = process.env.HTTP_PORT ? parseInt(process.env.HTTP_PORT) : 8883;
return {
enabled,
port,
};
}
public get tcp() {
const enabled = (process.env.TCP_ENABLED = 'true');
const port = process.env.TCP_PORT ? parseInt(process.env.TCP_PORT) : 1883;
return {
enabled,
port,
};
}
}
export { Config };

6
src/start.ts Normal file
View File

@@ -0,0 +1,6 @@
import { Backbone } from './backbone.ts';
const backbone = new Backbone();
await backbone.start();
console.log('started');

View File

@@ -1,11 +0,0 @@
import { Backbone } from './backbone.ts';
const backbone = new Backbone();
await backbone.setupK8sOperator();
const tcp = backbone.server.getTcpServer();
tcp.listen(1883);
const http = await backbone.server.getHttpServer();
http.listen({ port: 8883 });
console.log('started');