-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathservice.ts
More file actions
71 lines (57 loc) · 2.1 KB
/
service.ts
File metadata and controls
71 lines (57 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import 'reflect-metadata';
import { serve } from '@hono/node-server';
import { api, getConnection, getTrashCollection, Settings } from '@rocket.chat/core-services';
import { InstanceStatus } from '@rocket.chat/instance-status';
import { License } from '@rocket.chat/license';
import { registerServiceModels } from '@rocket.chat/models';
import { startBroker } from '@rocket.chat/network-broker';
import { Hono } from 'hono';
import { config } from './config';
function handleHealthCheck(app: Hono) {
app.get('/health', async (c) => {
try {
const hasLicense = await License.hasModule('federation');
const isEnabled = await Settings.get('Federation_Service_Enabled');
return c.json({
status: 'ok',
license: hasLicense ? 'valid' : 'invalid',
settings: {
federation_enabled: isEnabled,
},
});
} catch (err) {
console.error('Service not healthy', err);
return c.json({ status: 'not healthy', error: (err as Error).message }, 500);
}
});
}
(async () => {
console.log('Starting federation-service on microservice mode');
const { db } = await getConnection();
registerServiceModels(db, await getTrashCollection());
api.setBroker(startBroker());
await api.start();
const hasLicense = License.hasModule('federation');
if (!hasLicense) {
throw new Error('Service requires a valid Enterprise license with the federation module');
}
const isEnabled = await Settings.get('Federation_Service_Enabled');
if (!isEnabled) {
throw new Error('Service is disabled in settings (Federation_Service_Enabled = false)');
}
const { FederationMatrix } = await import('@rocket.chat/federation-matrix');
const federationMatrix = await FederationMatrix.create(InstanceStatus.id());
api.registerService(federationMatrix);
const app = new Hono();
const { matrix, wellKnown } = federationMatrix.getAllRoutes();
app.mount('/_matrix', matrix.getHonoRouter().fetch);
app.mount('/.well-known', wellKnown.getHonoRouter().fetch);
handleHealthCheck(app);
serve({
fetch: app.fetch,
port: config.port,
});
})().catch((error) => {
console.error('Failed to start service:', error);
process.exit(1);
});