-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (94 loc) · 3.23 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
const Koa = require('koa');
const morgan = require('koa-morgan');
const convert = require('koa-convert');
const cors = require('koa-cors');
const bodyParser = require('koa-bodyparser');
const compress = require('koa-compress');
const resources = require('koa-66-aggregate');
const servicesStack = require('services-stack');
const extend = require('extend');
const responseTime = require('koa-response-time');
class Acacia extends Koa {
constructor(config) {
super();
// expose config
this.context.config = config || { port: 1664};
if (this.context.config.responseTime)
this.use(responseTime());
// logger
if (this.env !== 'test')
this.use(morgan(this.context.config.log || 'dev'));
// cors
if (this.context.config.security && this.context.config.security.cors)
this.initCors(this.context.config.security.cors);
// zlib
this.use(convert(compress()));
// body parser
this.use(bodyParser());
}
initCors(config) {
const options = {
origin: req => {
if (!req.header.origin ||
!config.domains ||
!config.domains.length ||
config.domains[0].origin === '*')
return '*';
const has = config.domains.filter(domain =>
req.header.origin === domain.origin
);
return (has.length) ? has[0].origin : false;
}
};
if (config.headers) options.headers = config.headers;
if (config.credentials) options.credentials = config.credentials;
if (config.maxAge) options.maxAge = config.maxAge;
this.use(convert(cors(options)));
}
/**
* Initialise services throw directories using
* services-stack package.
*
* Inject `context` object in services of type functions
* Base context has already default properties:
* - config: config object (constructor param)
* - Validation: chain-validator objet(validation-helper)
*
* @param {object} options
* @return {object} Acacia instance
*/
services(options) {
if (!options)
throw new TypeError('options is required');
if (typeof options != 'object')
throw new TypeError('options must be an object');
options.context = options.context || {};
const base = {
config: this.context.config
};
options.context = extend(base, options.context);
this.context.service = servicesStack(options).get;
return this;
}
/**
* Initialise routers throw directories
*
* Inject `plugins` object on each Router object
*
* @param {object} options
* @return {object} Acacia instance
*/
resources(options) {
this.use(resources(options).routes());
return this;
}
listen(fn) {
return super.listen(this.context.config.port, () => {
if (this.env === 'development')
console.log('listening on ' + this.context.config.port);
if (fn && typeof fn == 'function') fn.call(this);
});
}
}
module.exports = Acacia;