-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
60 lines (50 loc) · 1.53 KB
/
index.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
'use strict'
require('dotenv').config()
const moduleAlias = require('module-alias') // custom local paths
moduleAlias.addAlias('@lib', __dirname + '/lib')
moduleAlias()
const Microapi = require('microapi/koa')
const Hashtable = require('./lib/clients/hashtable')
const Graphstore = require('./lib/clients/graphstore')
let options = {
hashtable: {
datastore: {
projectId: process.env.PROJECTID,
apiEndpoint: process.env.APIENDPOINT,
namespace: process.env.DATASTORE_NAMESPACE
},
couchbase: {
host: process.env.COUCHHOST, name: process.env.COUCHNAME,
auth: {username: process.env.COUCHUSER, password: process.env.COUCHPASS}
}
},
graphstore: {
scheme: process.env.GRAPHSCHEME,
host: process.env.GRAPHHOST,
auth: {password: process.env.GRAPHPASS}
}
}
function databaseMiddleware(databases) {
return async (context, next) => {
context.database = databases
await next()
}
}
async function init({port = 3000} = {}) {
const [api, hashtable, graphstore] = await Promise.all([
new Microapi(),
// Hashtable.database.couchbase.connect(options.hashtable.couchbase),
Hashtable.database.datastore.connect(options.hashtable.datastore),
Graphstore.database.connect(options.graphstore)
])
api.use(databaseMiddleware({graphstore, hashtable}))
api.define(`${__dirname}/api`)
api.listen(port, () => {
console.log(`Listening on port ${port}`)
})
return {api, graphstore, hashtable}
}
if (require.main === module) {
init({port: process.env.PORT})
}
exports.init = init