@stamhoofd/redirecter 2.85.4 → 2.87.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.
- package/index.ts +6 -75
- package/package.json +3 -3
- package/src/boot.ts +77 -0
package/index.ts
CHANGED
|
@@ -1,80 +1,11 @@
|
|
|
1
1
|
import backendEnv from '@stamhoofd/backend-env';
|
|
2
|
-
backendEnv.load({ service: 'redirecter' });
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import { Geolocator } from './src/classes/Geolocator';
|
|
8
|
-
|
|
9
|
-
process.on('unhandledRejection', (error: Error) => {
|
|
10
|
-
console.error('unhandledRejection');
|
|
11
|
-
console.error(error.message, error.stack);
|
|
3
|
+
backendEnv.load({ service: 'redirecter' }).catch((error) => {
|
|
4
|
+
console.error('Failed to load environment:', error);
|
|
12
5
|
process.exit(1);
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
// Quick check
|
|
19
|
-
if (new Date().getTimezoneOffset() !== 0) {
|
|
20
|
-
throw new Error('Process should always run in UTC timezone');
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const start = async () => {
|
|
24
|
-
await Geolocator.shared.load(__dirname + '/src/data/belgium.csv', Country.Belgium);
|
|
25
|
-
|
|
26
|
-
// Netherlands not needed, because it is the current default
|
|
27
|
-
// await Geolocator.shared.load(__dirname+"/src/data/netherlands.csv", Country.Netherlands)
|
|
28
|
-
|
|
29
|
-
console.log('Initialising server...');
|
|
30
|
-
const router = new Router();
|
|
31
|
-
await router.loadAllEndpoints(__dirname + '/src/endpoints');
|
|
32
|
-
await router.loadAllEndpoints(__dirname + '/src/endpoints/*');
|
|
33
|
-
|
|
34
|
-
const routerServer = new RouterServer(router);
|
|
35
|
-
routerServer.verbose = false;
|
|
36
|
-
routerServer.listen(STAMHOOFD.PORT ?? 9090);
|
|
37
|
-
|
|
38
|
-
const shutdown = async () => {
|
|
39
|
-
console.log('Shutting down...');
|
|
40
|
-
// Disable keep alive
|
|
41
|
-
routerServer.defaultHeaders = Object.assign(routerServer.defaultHeaders, { Connection: 'close' });
|
|
42
|
-
if (routerServer.server) {
|
|
43
|
-
routerServer.server.headersTimeout = 5000;
|
|
44
|
-
routerServer.server.keepAliveTimeout = 1;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
await routerServer.close();
|
|
49
|
-
console.log('HTTP server stopped');
|
|
50
|
-
}
|
|
51
|
-
catch (err) {
|
|
52
|
-
console.error('Failed to stop HTTP server:');
|
|
53
|
-
console.error(err);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Should not be needed, but added for security as sometimes a promise hangs somewhere
|
|
57
|
-
process.exit(0);
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
process.on('SIGTERM', () => {
|
|
61
|
-
console.info('SIGTERM signal received.');
|
|
62
|
-
shutdown().catch((e) => {
|
|
63
|
-
console.error(e);
|
|
64
|
-
process.exit(1);
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
process.on('SIGINT', () => {
|
|
69
|
-
console.info('SIGINT signal received.');
|
|
70
|
-
shutdown().catch((e) => {
|
|
71
|
-
console.error(e);
|
|
72
|
-
process.exit(1);
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
start().catch((error) => {
|
|
78
|
-
console.error('unhandledRejection', error);
|
|
6
|
+
}).then(async () => {
|
|
7
|
+
await import('./src/boot');
|
|
8
|
+
}).catch((error) => {
|
|
9
|
+
console.error('Failed to start the API:', error);
|
|
79
10
|
process.exit(1);
|
|
80
11
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stamhoofd/redirecter",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.87.0",
|
|
4
4
|
"main": "index.ts",
|
|
5
5
|
"license": "UNLICENCED",
|
|
6
6
|
"scripts": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"lint": "eslint"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"@types/node": "^
|
|
13
|
+
"@types/node": "^22"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@simonbackx/simple-endpoints": "1.20.1",
|
|
@@ -19,5 +19,5 @@
|
|
|
19
19
|
"publishConfig": {
|
|
20
20
|
"access": "public"
|
|
21
21
|
},
|
|
22
|
-
"gitHead": "
|
|
22
|
+
"gitHead": "372713e1e4b626d6b4ce4f3d148f14948528312e"
|
|
23
23
|
}
|
package/src/boot.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Router, RouterServer } from '@simonbackx/simple-endpoints';
|
|
2
|
+
import { Country } from '@stamhoofd/structures';
|
|
3
|
+
|
|
4
|
+
import { Geolocator } from './classes/Geolocator';
|
|
5
|
+
|
|
6
|
+
process.on('unhandledRejection', (error: Error) => {
|
|
7
|
+
console.error('unhandledRejection');
|
|
8
|
+
console.error(error.message, error.stack);
|
|
9
|
+
process.exit(1);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// Set timezone!
|
|
13
|
+
process.env.TZ = 'UTC';
|
|
14
|
+
|
|
15
|
+
// Quick check
|
|
16
|
+
if (new Date().getTimezoneOffset() !== 0) {
|
|
17
|
+
throw new Error('Process should always run in UTC timezone');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const start = async () => {
|
|
21
|
+
await Geolocator.shared.load(__dirname + '/data/belgium.csv', Country.Belgium);
|
|
22
|
+
|
|
23
|
+
// Netherlands not needed, because it is the current default
|
|
24
|
+
// await Geolocator.shared.load(__dirname+"/data/netherlands.csv", Country.Netherlands)
|
|
25
|
+
|
|
26
|
+
console.log('Initialising server...');
|
|
27
|
+
const router = new Router();
|
|
28
|
+
await router.loadAllEndpoints(__dirname + '/endpoints');
|
|
29
|
+
await router.loadAllEndpoints(__dirname + '/endpoints/*');
|
|
30
|
+
|
|
31
|
+
const routerServer = new RouterServer(router);
|
|
32
|
+
routerServer.verbose = false;
|
|
33
|
+
routerServer.listen(STAMHOOFD.PORT ?? 9090);
|
|
34
|
+
|
|
35
|
+
const shutdown = async () => {
|
|
36
|
+
console.log('Shutting down...');
|
|
37
|
+
// Disable keep alive
|
|
38
|
+
routerServer.defaultHeaders = Object.assign(routerServer.defaultHeaders, { Connection: 'close' });
|
|
39
|
+
if (routerServer.server) {
|
|
40
|
+
routerServer.server.headersTimeout = 5000;
|
|
41
|
+
routerServer.server.keepAliveTimeout = 1;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
await routerServer.close();
|
|
46
|
+
console.log('HTTP server stopped');
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
console.error('Failed to stop HTTP server:');
|
|
50
|
+
console.error(err);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Should not be needed, but added for security as sometimes a promise hangs somewhere
|
|
54
|
+
process.exit(0);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
process.on('SIGTERM', () => {
|
|
58
|
+
console.info('SIGTERM signal received.');
|
|
59
|
+
shutdown().catch((e) => {
|
|
60
|
+
console.error(e);
|
|
61
|
+
process.exit(1);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
process.on('SIGINT', () => {
|
|
66
|
+
console.info('SIGINT signal received.');
|
|
67
|
+
shutdown().catch((e) => {
|
|
68
|
+
console.error(e);
|
|
69
|
+
process.exit(1);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
start().catch((error) => {
|
|
75
|
+
console.error('unhandledRejection', error);
|
|
76
|
+
process.exit(1);
|
|
77
|
+
});
|