forked from gosqasorg/asset-provenance-tracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
46 lines (38 loc) · 1.42 KB
/
app.ts
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
import os from 'os';
import dotenv from 'dotenv';
import { Sequelize } from 'sequelize';
import { createFastifyServer } from './server';
import { createSequelizeReposities } from './services/sequelizeRepo';
dotenv.config();
const port = process.env.PORT ? parseInt(process.env.PORT) : 80;
async function getReposities() {
const sequelize = process.env.DATABASE_URL
? new Sequelize(process.env.DATABASE_URL, {
dialectOptions: {
ssl: { rejectUnauthorized: false }
}
})
: new Sequelize({
dialect: 'sqlite',
storage: './database.sqlite',
})
return await createSequelizeReposities(sequelize);
}
async function main() {
const { devices, provenance } = await getReposities();
const server = await createFastifyServer(devices, provenance);
server.listen({ port, host: '0.0.0.0' }, (err, address) => {
if (err) {
console.error(err);
process.exit(1);
}
const interfaces = Object.entries(os.networkInterfaces())
.flatMap(([key, value]) => (value ?? []).map(v => ({ name: key, ...v })))
.filter(v => v.family === 'IPv4')
.map(v => ({ name: v.name, address: v.address }))
for (const i of interfaces) {
console.log(`\x1b[32m[server]: ${i.name} Server is running at \x1b[33mhttp://${i.address}:${port}\x1b[0m`);
}
});
}
main();