-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.js
243 lines (212 loc) · 6.56 KB
/
config.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// @themost-framework 2.0 Codename Blueshift Copyright (c) 2017-2025, THEMOST LP All rights reserved
var ConfigurationBase = require('@themost/common').ConfigurationBase;
var TraceUtils = require('@themost/common').TraceUtils;
var PathUtils = require('@themost/common').PathUtils;
var LangUtils = require('@themost/common').LangUtils;
var Symbol = require('symbol');
var _ = require('lodash');
var routesProperty = Symbol('routes');
/**
* Defines an HTTP view engine in application configuration
* @class
* @constructor
*/
function HttpViewEngineConfiguration()
{
/**
* @property
* @type {string}
* @name HttpViewEngineConfiguration#type
* @description Gets or sets the class associated with an HTTP view engine
*
*/
/**
* @property
* @type {string}
* @name HttpViewEngineConfiguration#name
* @description Gets or sets a string which represents the name of an HTTP view engine
*/
/** @property
* @type {string}
* @name HttpViewEngineConfiguration#extension
* @description Gets or sets or sets a string which represents the extension associated with an HTTP view engine e.g. ejs, md etc
*/
}
/**
* @class
* @constructor
*/
function MimeTypeConfiguration() {
/**
* @property
* @name MimeTypeConfiguration#extension
* @type {string}
* @description Gets or sets a string which represents an extension associated with this mime type e.g. .css, .html, .json etc
*/
/**
* @property
* @name MimeTypeConfiguration#type
* @type {string}
* @description Gets or sets a string which represents a media type e.g. application/json, text/html etc.
*/
}
/**
* @class
* @constructor
*/
function HttpHandlerConfiguration() {
/**
* @property
* @name HttpHandlerConfiguration#name
* @type {string}
* @description Gets or sets a string which represents a name for this HTTP handler e.g. auth, basic-auth, post, restrict-access etc
*/
/**
* @property
* @name HttpHandlerConfiguration#type
* @type {string}
* @description Gets or sets a string which represents the module path of this HTTP handler.
*/
}
/**
* @class
* @constructor
*/
function HttpRouteConfiguration() {
/**
* @property
* @name HttpRouteConfiguration#url
* @type {string}
* @description Gets or sets a string which the url pattern of an HTTP route e.g. /:controller/:action, /:controller/:id/:action etc
*/
/**
* @property
* @name HttpRouteConfiguration#controller
* @type {string}
* @description Gets or sets a string which defines the controller associated with an HTTP route
*/
/**
* @property
* @name HttpRouteConfiguration#action
* @type {string}
* @description Gets or sets a string which defines the action of an HTTP route
*/
/**
* @property
* @name HttpRouteConfiguration#format
* @type {string}
* @description Gets or sets a string which represents a media type etc. json, html etc
*/
/**
* @property
* @name HttpRouteConfiguration#mime
* @type {string}
* @description Gets or sets a string which represents mime type etc. application/json, text/html etc
*/
/**
* @property
* @name HttpRouteConfiguration#path
* @type {string}
* @description Gets or sets a string which represents the root path of view templates which are going to be used for this route
*/
/**
* @property
* @name HttpRouteConfiguration#params
* @type {*}
* @description Gets or sets a set of parameters associated with an HTTP route e.g. static query parameters
*/
/**
* @property
* @name HttpRouteConfiguration#name
* @type {string}
* @description Gets or sets a string which represents the name of this route.
*/
}
/**
* @class
* @constructor
* @param {string} configPath
* @augments ConfigurationBase
*/
function HttpConfiguration(configPath) {
HttpConfiguration.super_.bind(this)(configPath);
if (!this.hasSourceAt('mimes')) { this.setSourceAt('mimes',[]); }
if (!this.hasSourceAt('engines')) { this.setSourceAt('engines',[]); }
if (!this.hasSourceAt('controllers')) { this.setSourceAt('controllers',[]); }
if (!this.hasSourceAt('handlers')) { this.setSourceAt('handlers',[]); }
try {
this[routesProperty] = require(PathUtils.join(this.getConfigurationPath(),'routes.json'))
}
catch(err) {
if (err.code === 'MODULE_NOT_FOUND') {
this[routesProperty] = require('./resources/routes.json');
}
else {
TraceUtils.error('An error occurred while loading routes collection');
TraceUtils.error(err);
}
}
/**
* @name HttpConfiguration#engines
* @type {Array.<HttpViewEngineConfiguration>}
*/
Object.defineProperty(this, 'engines', {
get:function() {
return this.getSourceAt('engines');
}
});
/**
* @name HttpConfiguration#mimes
* @type {Array.<MimeTypeConfiguration>}
*/
Object.defineProperty(this, 'mimes', {
get:function() {
return this.getSourceAt('mimes');
}
});
/**
* @name HttpConfiguration#routes
* @type {Array.<HttpRouteConfiguration>}
*/
Object.defineProperty(this, 'routes', {
get:function() {
return this[routesProperty];
}
});
/**
* @name HttpConfiguration#controllers
* @type {Array}
*/
Object.defineProperty(this, 'controllers', {
get:function() {
return this.getSourceAt('controllers');
}
});
/**
* @name HttpConfiguration#handlers
* @type {Array.<HttpHandlerConfiguration>}
*/
Object.defineProperty(this, 'handlers', {
get:function() {
return this.getSourceAt('handlers');
}
});
}
LangUtils.inherits(HttpConfiguration, ConfigurationBase);
/**
* Gets a mime type based on the given extension
* @param {string} extension
* @returns {*}
*/
HttpConfiguration.prototype.getMimeType = function(extension) {
return _.find(this.mimes,function(x) {
return (x.extension===extension) || (x.extension==='.'+extension);
});
};
if (typeof exports !== 'undefined') {
module.exports.HttpConfiguration = HttpConfiguration;
module.exports.HttpViewEngineConfiguration = HttpViewEngineConfiguration;
module.exports.MimeTypeConfiguration = MimeTypeConfiguration;
module.exports.HttpRouteConfiguration = HttpRouteConfiguration;
module.exports.HttpHandlerConfiguration = HttpHandlerConfiguration;
}