UNPKG

6.91 kBJavaScriptView Raw
1/**
2 * @license
3 * MOST Web Framework 2.0 Codename Blueshift
4 * Copyright (c) 2017, THEMOST LP All rights reserved
5 *
6 * Use of this source code is governed by an BSD-3-Clause license that can be
7 * found in the LICENSE file at https://themost.io/license
8 */
9///
10var ConfigurationBase = require('@themost/common/config').ConfigurationBase;
11var TraceUtils = require('@themost/common/utils').TraceUtils;
12var PathUtils = require('@themost/common/utils').PathUtils;
13var LangUtils = require('@themost/common/utils').LangUtils;
14var Symbol = require('symbol');
15var _ = require('lodash');
16
17var routesProperty = Symbol('routes');
18
19/**
20 * Defines an HTTP view engine in application configuration
21 * @class
22 * @constructor
23 */
24function HttpViewEngineConfiguration()
25{
26
27 /**
28 * @property
29 * @type {string}
30 * @name HttpViewEngineConfiguration#type
31 * @description Gets or sets the class associated with an HTTP view engine
32 *
33 */
34
35 /**
36 * @property
37 * @type {string}
38 * @name HttpViewEngineConfiguration#name
39 * @description Gets or sets a string which represents the name of an HTTP view engine
40 */
41
42 /** @property
43 * @type {string}
44 * @name HttpViewEngineConfiguration#extension
45 * @description Gets or sets or sets a string which represents the extension associated with an HTTP view engine e.g. ejs, md etc
46 */
47}
48
49/**
50 * @class
51 * @constructor
52 */
53function MimeTypeConfiguration() {
54 /**
55 * @property
56 * @name MimeTypeConfiguration#extension
57 * @type {string}
58 * @description Gets or sets a string which represents an extension associated with this mime type e.g. .css, .html, .json etc
59 */
60
61 /**
62 * @property
63 * @name MimeTypeConfiguration#type
64 * @type {string}
65 * @description Gets or sets a string which represents a media type e.g. application/json, text/html etc.
66 */
67}
68
69/**
70 * @class
71 * @constructor
72 */
73function HttpHandlerConfiguration() {
74 /**
75 * @property
76 * @name HttpHandlerConfiguration#name
77 * @type {string}
78 * @description Gets or sets a string which represents a name for this HTTP handler e.g. auth, basic-auth, post, restrict-access etc
79 */
80
81 /**
82 * @property
83 * @name HttpHandlerConfiguration#type
84 * @type {string}
85 * @description Gets or sets a string which represents the module path of this HTTP handler.
86 */
87}
88
89/**
90 * @class
91 * @constructor
92 */
93function HttpRouteConfiguration() {
94 /**
95 * @property
96 * @name HttpRouteConfiguration#url
97 * @type {string}
98 * @description Gets or sets a string which the url pattern of an HTTP route e.g. /:controller/:action, /:controller/:id/:action etc
99 */
100
101 /**
102 * @property
103 * @name HttpRouteConfiguration#controller
104 * @type {string}
105 * @description Gets or sets a string which defines the controller associated with an HTTP route
106 */
107
108 /**
109 * @property
110 * @name HttpRouteConfiguration#action
111 * @type {string}
112 * @description Gets or sets a string which defines the action of an HTTP route
113 */
114
115 /**
116 * @property
117 * @name HttpRouteConfiguration#format
118 * @type {string}
119 * @description Gets or sets a string which represents a media type etc. json, html etc
120 */
121
122 /**
123 * @property
124 * @name HttpRouteConfiguration#mime
125 * @type {string}
126 * @description Gets or sets a string which represents mime type etc. application/json, text/html etc
127 */
128
129 /**
130 * @property
131 * @name HttpRouteConfiguration#path
132 * @type {string}
133 * @description Gets or sets a string which represents the root path of view templates which are going to be used for this route
134 */
135
136 /**
137 * @property
138 * @name HttpRouteConfiguration#params
139 * @type {*}
140 * @description Gets or sets a set of parameters associated with an HTTP route e.g. static query parameters
141 */
142
143 /**
144 * @property
145 * @name HttpRouteConfiguration#name
146 * @type {string}
147 * @description Gets or sets a string which represents the name of this route.
148 */
149}
150
151/**
152 * @class
153 * @constructor
154 * @param {string} configPath
155 * @augments ConfigurationBase
156 */
157function HttpConfiguration(configPath) {
158 HttpConfiguration.super_.bind(this)(configPath);
159 if (!this.hasSourceAt('mimes')) { this.setSourceAt('mimes',[]); }
160 if (!this.hasSourceAt('engines')) { this.setSourceAt('engines',[]); }
161 if (!this.hasSourceAt('controllers')) { this.setSourceAt('controllers',[]); }
162 if (!this.hasSourceAt('handlers')) { this.setSourceAt('handlers',[]); }
163 try {
164 this[routesProperty] = require(PathUtils.join(this.getConfigurationPath(),'routes.json'))
165 }
166 catch(err) {
167 if (err.code === 'MODULE_NOT_FOUND') {
168 this[routesProperty] = require('./resources/routes.json');
169 }
170 else {
171 TraceUtils.error('An error occurred while loading routes collection');
172 TraceUtils.error(err);
173 }
174 }
175 /**
176 * @name HttpConfiguration#engines
177 * @type {Array.<HttpViewEngineConfiguration>}
178 */
179
180 Object.defineProperty(this, 'engines', {
181 get:function() {
182 return this.getSourceAt('engines');
183 }
184 });
185
186
187 /**
188 * @name HttpConfiguration#mimes
189 * @type {Array.<MimeTypeConfiguration>}
190 */
191
192 Object.defineProperty(this, 'mimes', {
193 get:function() {
194 return this.getSourceAt('mimes');
195 }
196 });
197
198 /**
199 * @name HttpConfiguration#routes
200 * @type {Array.<HttpRouteConfiguration>}
201 */
202
203 Object.defineProperty(this, 'routes', {
204 get:function() {
205 return this[routesProperty];
206 }
207 });
208
209 /**
210 * @name HttpConfiguration#controllers
211 * @type {Array}
212 */
213
214 Object.defineProperty(this, 'controllers', {
215 get:function() {
216 return this.getSourceAt('controllers');
217 }
218 });
219
220 /**
221 * @name HttpConfiguration#handlers
222 * @type {Array.<HttpHandlerConfiguration>}
223 */
224
225 Object.defineProperty(this, 'handlers', {
226 get:function() {
227 return this.getSourceAt('handlers');
228 }
229 });
230
231}
232LangUtils.inherits(HttpConfiguration, ConfigurationBase);
233
234/**
235 * Gets a mime type based on the given extension
236 * @param {string} extension
237 * @returns {*}
238 */
239HttpConfiguration.prototype.getMimeType = function(extension) {
240 return _.find(this.mimes,function(x) {
241 return (x.extension===extension) || (x.extension==='.'+extension);
242 });
243};
244
245if (typeof exports !== 'undefined') {
246 module.exports.HttpConfiguration = HttpConfiguration;
247 module.exports.HttpViewEngineConfiguration = HttpViewEngineConfiguration;
248 module.exports.MimeTypeConfiguration = MimeTypeConfiguration;
249 module.exports.HttpRouteConfiguration = HttpRouteConfiguration;
250 module.exports.HttpHandlerConfiguration = HttpHandlerConfiguration;
251}