@wisemen/app-container 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ import { ProbedContainer } from './default.js';
2
+ export declare abstract class ApiContainer extends ProbedContainer {
3
+ }
@@ -0,0 +1,4 @@
1
+ import { ProbedContainer } from './default.js';
2
+ export class ApiContainer extends ProbedContainer {
3
+ }
4
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../../lib/containers/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAE9C,MAAM,OAAgB,YAAa,SAAQ,eAAe;CAAG"}
@@ -0,0 +1,20 @@
1
+ import { type Express } from 'express';
2
+ import type { INestApplicationContext } from '@nestjs/common';
3
+ export interface ProbedContainer {
4
+ execute?(nest: INestApplicationContext): Promise<void>;
5
+ }
6
+ export declare abstract class ProbedContainer {
7
+ private readonly app;
8
+ private readonly server;
9
+ private state;
10
+ protected nest?: INestApplicationContext;
11
+ abstract bootstrap(app: Express): Promise<INestApplicationContext>;
12
+ constructor();
13
+ protected init(): Promise<void>;
14
+ protected close(): Promise<void>;
15
+ private enableShutdownHooks;
16
+ private enableProbes;
17
+ private liveness;
18
+ private readiness;
19
+ private version;
20
+ }
@@ -0,0 +1,100 @@
1
+ import express from 'express';
2
+ // import { initSentry } from '../sentry/sentry.js'
3
+ const port = process.env.PORT ?? 3000;
4
+ export class ProbedContainer {
5
+ app;
6
+ server;
7
+ state;
8
+ nest;
9
+ constructor() {
10
+ this.state = 'starting';
11
+ this.app = express();
12
+ this.server = this.app.listen(port, () => {
13
+ console.log('server started');
14
+ });
15
+ this.enableShutdownHooks();
16
+ this.enableProbes();
17
+ void this.init();
18
+ // initSentry()
19
+ }
20
+ async init() {
21
+ try {
22
+ this.nest = await this.bootstrap(this.app);
23
+ await this.nest.init();
24
+ this.state = 'ready';
25
+ if (this.execute != null) {
26
+ await this.execute(this.nest);
27
+ await this.close();
28
+ }
29
+ }
30
+ catch {
31
+ await this.close();
32
+ }
33
+ }
34
+ async close() {
35
+ if (this.state === 'shutdown') {
36
+ return;
37
+ }
38
+ this.state = 'shutdown';
39
+ try {
40
+ await new Promise((resolve, reject) => {
41
+ this.server.close((err) => {
42
+ if (err)
43
+ reject(err);
44
+ else
45
+ resolve();
46
+ });
47
+ });
48
+ await this.nest?.close();
49
+ console.log('application closed');
50
+ }
51
+ catch (e) {
52
+ console.error('error shutting down', e);
53
+ process.exit(1);
54
+ }
55
+ }
56
+ enableShutdownHooks() {
57
+ process.on('SIGTERM', () => void this.close());
58
+ process.on('SIGINT', () => void this.close());
59
+ process.on('SIGUSR2', () => void this.close());
60
+ process.on('SIGHUP', () => void this.close());
61
+ }
62
+ enableProbes() {
63
+ this.app.get('/', (_, res) => {
64
+ this.version(res);
65
+ });
66
+ this.app.get('/health', (_, res) => {
67
+ this.liveness(res);
68
+ });
69
+ this.app.get('/ready', (_, res) => {
70
+ this.readiness(res);
71
+ });
72
+ }
73
+ liveness(res) {
74
+ res.writeHead(200, { 'Content-Type': 'text/plain' });
75
+ res.write('OK');
76
+ res.end();
77
+ }
78
+ readiness(res) {
79
+ if (this.state === 'ready') {
80
+ res.writeHead(200, { 'Content-Type': 'text/plain' });
81
+ res.write('OK');
82
+ }
83
+ else {
84
+ res.writeHead(500, { 'Content-Type': 'text/plain' });
85
+ res.write('not OK');
86
+ }
87
+ res.end();
88
+ }
89
+ version(res) {
90
+ res.writeHead(200, { 'Content-Type': 'application/json' });
91
+ res.write(JSON.stringify({
92
+ env: process.env.NODE_ENV,
93
+ commit: process.env.COMMIT,
94
+ build: process.env.BUILD_NUMBER,
95
+ version: process.env.VERSION
96
+ }));
97
+ res.end();
98
+ }
99
+ }
100
+ //# sourceMappingURL=default.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default.js","sourceRoot":"","sources":["../../lib/containers/default.ts"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AAC,MAAM;YAC1B,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY;YAC/B,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,OAAO;SAC7B,CAAC,CAAC,CAAA;QACH,GAAG,CAAC,GAAG,EAAE,CAAA;IACX,CAAC;CACF"}
@@ -0,0 +1,6 @@
1
+ import type { INestApplicationContext } from '@nestjs/common';
2
+ import { ProbedContainer } from './default.js';
3
+ export declare abstract class JobContainer extends ProbedContainer {
4
+ abstract execute(nest: INestApplicationContext): Promise<void>;
5
+ protected init(): Promise<void>;
6
+ }
@@ -0,0 +1,9 @@
1
+ import { ProbedContainer } from './default.js';
2
+ export class JobContainer extends ProbedContainer {
3
+ async init() {
4
+ await super.init();
5
+ await this.execute(this.nest);
6
+ await this.close();
7
+ }
8
+ }
9
+ //# sourceMappingURL=job.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"job.js","sourceRoot":"","sources":["../../lib/containers/job.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAE9C,MAAM,OAAgB,YAAa,SAAQ,eAAe;IAG9C,KAAK,CAAC,IAAI;QAClB,MAAM,KAAK,CAAC,IAAI,EAAE,CAAA;QAElB,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAK,CAAC,CAAA;QAE9B,MAAM,IAAI,CAAC,KAAK,EAAE,CAAA;IACpB,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ import { ProbedContainer } from './default.js';
2
+ export declare abstract class WorkerContainer extends ProbedContainer {
3
+ }
@@ -0,0 +1,4 @@
1
+ import { ProbedContainer } from './default.js';
2
+ export class WorkerContainer extends ProbedContainer {
3
+ }
4
+ //# sourceMappingURL=worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker.js","sourceRoot":"","sources":["../../lib/containers/worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAE9C,MAAM,OAAgB,eAAgB,SAAQ,eAAe;CAAG"}
@@ -0,0 +1,3 @@
1
+ export * from './containers/api.js';
2
+ export * from './containers/job.js';
3
+ export * from './containers/worker.js';
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from './containers/api.js';
2
+ export * from './containers/job.js';
3
+ export * from './containers/worker.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,qBAAqB,CAAA;AACnC,cAAc,wBAAwB,CAAA"}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@wisemen/app-container",
3
+ "version": "2.0.0",
4
+ "description": "A package for a nest app container in kubernetes",
5
+ "directories": {
6
+ "example": "examples"
7
+ },
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "type": "module",
11
+ "files": [
12
+ "dist/"
13
+ ],
14
+ "scripts": {
15
+ "lint": "eslint lib",
16
+ "prebuild": "rm -rf ./dist",
17
+ "clean": "rm -r ./dist",
18
+ "build": "tsc",
19
+ "pretest": "npm run clean && npm run build",
20
+ "test": "node --test"
21
+ },
22
+ "dependencies": {
23
+ "express": "^4.18.2"
24
+ },
25
+ "devDependencies": {
26
+ "@nestjs/common": "^10.4.1",
27
+ "@nestjs/core": "^10.4.1",
28
+ "@nestjs/platform-express": "^10.4.1",
29
+ "@types/express": "^4.17.17",
30
+ "@types/node": "^20.2.3",
31
+ "release-it": "^15.11.0",
32
+ "typescript": "^5.0.4"
33
+ },
34
+ "author": "Joren Vandeweyer",
35
+ "license": "ISC",
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git@github.com:appwise-labs/node-core.git"
39
+ },
40
+ "bugs": {
41
+ "url": "https://github.com/wisemen-digital/node-core/issues"
42
+ },
43
+ "homepage": "https://github.com/wisemen-digital/node-core/packages/app-container"
44
+ }