-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (54 loc) · 1.67 KB
/
app.js
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
/**
* IdentityChain Agent REST API
* Main
*/
require('./config');
const express = require('express');
const YAML = require('yamljs');
const swaggerUi = require('swagger-ui-express');
// require db at start to establish connection
// and all models so they are available
// through Mongoose.model later on
require('./db');
require('./models');
const log = require('./log').log;
const pool = require('./pool');
const middleware = require('./middleware');
const routes = require('./routes');
const message = require('./controllers/message');
const credentialDefinition = require('./controllers/credentialdef');
const APIResult = require('./api-result');
const swaggerDoc = YAML.load('./swagger.yaml');
const app = express();
app.use(middleware.before);
app.get('/tails/:tailsHash', credentialDefinition.retrieveTails);
app.post('/indy', message.receiveMessage);
app.get('/healthcheck', (req, res, next) => {
return next(APIResult.success({ healthy: true }));
});
app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerDoc));
app.use('/api/', routes);
app.use(middleware.after);
/**
* Initializes pool and db connection
*/
async function initialize() {
try {
await pool.createConfig();
} catch (err) {
log.warn(err);
}
await pool.openLedger();
}
initialize()
.then(() => {
const server = app.listen(process.env.IDC_API_PORT, process.env.IDC_API_HOST, async () => {
log.info('IDChain API now up at %s:%s', server.address().address, server.address().port);
log.info('Access APIDocs at /api/docs');
});
})
.catch(err => {
log.error(err);
process.exit(1);
});
module.exports = app;