-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
50 lines (43 loc) · 1.3 KB
/
index.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
47
48
49
50
import express from 'express';
import bodyParser from 'body-parser';
import dotenv from 'dotenv';
import apiRoutes from './api/routes';
import { Blockchain } from './blockchain';
import { connectToDatabase } from './db';
import { PubSub } from './pubsub';
import { Db } from 'mongodb';
import { CHANNELS } from './pubsub';
dotenv.config();
export const blockchain:Blockchain = new Blockchain();
export const pubSub:PubSub=new PubSub(blockchain)
pubSub.requestChain();
pubSub.subscriber.on('message', (channel:string, message:string) => {
if (channel === CHANNELS.RESPONSE_CHAIN) {
const receivedChain = JSON.parse(message);
blockchain.replaceChain(receivedChain);
}
});
let db: Db;
async function main() {
const app = express();
try {
db = await connectToDatabase();
} catch (error) {
console.error("Failed to connect to database:", error);
process.exit(1);
}
app.use(bodyParser.json());
app.use('/api', apiRoutes);
const PORT = process.env.EXPRESS_PORT || 3000;
if (process.env.GENERATE_PEER_PORT === 'true') {
app.listen(3005, () => {
console.log('Listening on port 3005');
});
} else {
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
}
}
main();
export { db };