UNPKG

13.8 kBMarkdownView Raw
1
2Configuration Reference
3=======================
4
5The config object passed to an instance of Shunter can append or overwrite Shunter's default configuration. The default configuration options are documented below with some information on how to define and organise the configuration of a Shunter application.
6
7- [Web](#web-configuration)
8- [Path](#path-configuration)
9- [Structure](#structure-configuration)
10- [Log](#log-configuration)
11- [StatsD](#statsd-configuration)
12- [Timer](#timer-configuration)
13- [Trigger Parameter](#trigger-parameter)
14- [JSON View Parameter](#json-view-parameter)
15- [Environment](#environment-configuration)
16- [Templated Error Pages](#templated-error-page-configuration)
17- [Custom Configurations](#adding-custom-configurations)
18- [Configuring Modules](#configuring-modules)
19- [Command Line Options](#command-line-options)
20- [Accessing the Configuration at Run Time](#accessing-the-configuration-at-run-time)
21
22Web Configuration
23-----------------
24
25The web object contains the names of directory for serving private and public resources compiled by Shunter on build. It also contains the name of the directory where tests are located:
26
27```js
28web: {
29 public: '/public',
30 publicResources: '/public/resources',
31 resources: '/resources',
32 tests: '/tests'
33}
34```
35
36
37Path Configuration
38------------------
39
40The path object defines the paths to some of the key directories used by Shunter. This includes the application root path, paths to tests, themes and public resources:
41
42```js
43path: {
44 clientTests: path.join(appRoot, 'tests', 'client'),
45 dust: path.join(appRoot, 'dust'),
46 public: path.join(appRoot, 'public'),
47 publicResources: path.join(appRoot, 'public', 'resources'),
48 resources: path.join(appRoot, 'resources'),
49 root: appRoot,
50 shunterResources: path.join(shunterRoot, 'resources'),
51 shunterRoot: shunterRoot,
52 templates: path.join(appRoot, 'view'),
53 tests: path.join(appRoot, 'tests'),
54 themes: path.join(shunterRoot, 'themes')
55}
56```
57
58
59Structure Configuration
60-----------------------
61
62The structure object defines the directory structure where Shunter's resources will reside:
63
64```js
65structure: {
66 dust: 'dust',
67 ejs: 'ejs',
68 filters: 'filters',
69 filtersInput: 'input',
70 filtersOutput: 'output',
71 fonts: 'fonts',
72 images: 'img',
73 logging: 'logging',
74 loggingTransports: 'transports',
75 loggingFilters: 'filters',
76 mincer: 'mincer',
77 resources: 'resources',
78 scripts: 'js',
79 styles: 'css',
80 templateExt: '.dust',
81 templates: 'view',
82 tests: 'tests'
83}
84```
85
86- `structure.filters` defines the directory used to hold the filter functions that can be used to process JSON before and after it is rendered into to its output format.
87- `structure.filtersInput` defines the directory used to hold the filter functions that can be used to process JSON before it is rendered into its output format.
88- `structure.filtersOutput` defines the directory used to hold the filter functions that can be used to process output files once they has been rendered.
89- `structure.fonts` defines the directory used to hold web fonts.
90- `structure.images` defines the directory used to hold image files used for presentation. This default value is 'img'.
91- `structure.logging` defines the directory used to hold any user transport or filter files.
92- `structure.loggingTransports` defines the directory used to hold any user transport files, inside the `structure.logging` dir.
93- `structure.loggingFilters` defines the directory used to hold any user filter files, inside the `structure.logging` dir.
94- `structure.resources` defines the name of the directory used to house front-end resources including CSS, JavaScript and images, the default value is 'resources'.
95- `structure.scripts` defines the directory used to hold JavaScript files.
96- `structure.styles` defines the name of the directory used to hold CSS files used in your Shunter application. The default value is 'css'.
97- `structure.templateExt` defines the file extension that Shunter should use for templating. By default this is .dust as Dust is the default templating in use within Shunter.
98- `structure.templates` defines the location of the templates used to render the Shunter application's output. By default the value of this is 'view'.
99- `structure.tests` defines the directory used to hold the files that define your JavaScript and template tests.
100
101
102Log Configuration
103-----------------
104
105The log object defines the tool that Shunter should use for logging. By default Shunter uses [Winston](https://github.com/winstonjs/winston).
106
107You can specify the logging level to use (e.g. 'info' the default, or 'debug' if you'd like to see more) by using the `-l` [command line option](#command-line-options)
108
109```js
110log: new winston.Logger({
111 transports: [
112 new (winston.transports.Console)({
113 colorize: true,
114 timestamp: true,
115 level: args.logging
116 })
117 ]
118}),
119```
120
121The log configuration can also be passed to shunter via transport and filter files in user-specified drectories (specified by `structure.logging`, `structure.loggingTransports` and `structure.loggingFilters`, above).
122
123Shunter's default logging transports can be found in `logging/transports` in the Shunter source. Shunter uses no filters by default, but here's a trivial example you may find useful:
124
125```js
126'use strict';
127module.exports = function(level, msg, meta) {
128 return 'filtered message: ' + msg;
129}
130```
131
132StatsD Configuration
133--------------------
134
135The `statsd` option defines the configuration used for the StatsD network daemon used for collecting metrics for graphing.
136
137
138Timer Configuration
139-------------------
140
141the timer object defines a method used to append a date and time to messages passed to the log. By default it looks like this:
142
143```js
144timer: function() {
145 var start = Date.now();
146 return function(msg) {
147 var diff = Date.now() - start;
148 config.log.info(msg + ' - ' + diff + 'ms');
149 return diff;
150 };
151},
152```
153
154Trigger Parameter
155-----------------
156
157The trigger object defines the name of the response header that will be inspected in order to decide whether to transform the response or pass it through to the client.
158
159This setting allows you to transform responses without munging the Content-type in the upstream service - useful if your service uses HATEOAS mime-type versioning for example.
160
161The default value is:
162
163```js
164trigger {
165 header: 'Content-type',
166 matchExpression: 'application/x-shunter\\+json'
167}
168```
169
170Any proxied responses with a Content-type of `application/x-shunter+json` will be transformed. The matchExpression is a case-insensitive regular expression that is applied to the value of the named header.
171
172JSON View Parameter
173-------------------
174
175Sometimes it's helpful to view the raw JSON that's being returned by the server. Shunter supports viewing this by using a query parameter. If this parameter is present, then the raw JSON will be output when a page is requested.
176
177By default the query parameter is disabled so that nobody can look at your JSON if they know you use Shunter. You can enable it with the `jsonViewParameter` configuration.
178
179This config property sets the name of the query parameter that triggers raw JSON serving:
180
181```js
182shunter({
183 jsonViewParameter: 'show-me-the-json'
184});
185```
186
187With the above configuration, you'd just need to append a query parameter to your URL:
188
189```
190/path/to/your/page?show-me-the-json=true
191```
192
193
194Environment Configuration
195-------------------------
196
197The `env` object contains functions that return the name of the different environments which your Shunter application may be deployed. By default it looks like this:
198
199```js
200env: {
201 name: env,
202 host: function() {
203 return hostname;
204 },
205 isDevelopment: function() {
206 return this.name === 'development';
207 },
208 isProduction: function() {
209 return this.name === 'production';
210 }
211}
212```
213
214You may like to modify this config object to reflect the environments to which you will be deploying your Shunter application.
215
216
217Templated Error Page Configuration
218----------------------------------
219
220Optionally, you can configure Shunter to render error pages for recoverable Shunter errors, and also 400/500 responses from the backend. To do so, provide a configuration object similar to this:
221
222```js
223errorPages: {
224 errorLayouts: {
225 default: 'layout',
226 404: 'layout-error-404'
227 },
228 staticData: {
229 yourData: {
230 goes: 'here'
231 }
232 }
233}
234```
235
236The value of `errorPages.errorLayouts.default` should be the name of the root template you wish to use to render any error pages. It is possible to override this default by HTTP Status Code — so in the example above `layout-error-404.dust` would be used as the root template if a 404 error is passed to Shunter from the backend.
237
238Any object defined in `errorPages.staticData` will be made available in the root of the context when the page is rendered. This object must not contain keys in the top level called `layout` or `errorContext` or Shunter will silently drop them.
239
240Additionally, when using templated error pages, Shunter will automatically insert the error object and some other information into the context for use in your templates. Here is an example of the context object passed to the renderer in the event of a 404 error, given the above configuration:
241
242```js
243layout: {
244 template: 'layout-error-404',
245 namespace: 'custom-errors'
246},
247errorContext: {
248 error: {
249 status: 404,
250 message: 'Not found'
251 },
252 hostname: 'localhost.localdomain',
253 isDevelopment: true,
254 isProduction: false,
255 reqHost: 'localhost:5400',
256 reqUrl: '/does-not-exist'
257},
258yourData: {
259 goes: 'here'
260}
261```
262
263If you require a large set of `staticData`, it may be more appropriate to include it as a Custom Configuration.
264
265
266Adding Custom Configurations
267----------------------------
268
269The items above are the default configurations which may be over-ridden. You will probably need to define some configuration that is unique to your own Shunter application. These can be neatly organized as JSON files in a config directory and required by your Shunter application at start-up to either append or overwrite existing configs. In the example below the `routes.json` usually required by Shunter has been placed in a `routes.json` file in the config directory and required from that location:
270
271```js
272var app = shunter({
273 routes: require('./config/routes.json'),
274 statsd: require('./config/statsd.json'),
275 syslogAppName: 'my-shunter-app',
276 path: {
277 themes: __dirname
278 }
279});
280```
281
282
283Configuring Modules
284-------------------
285
286Shunter allows you to make use of a module format that lets you to do things like share common presentational features between a set of dependent apps. This allows you do things like manage shared assets and components in one place. As your dependent module may also contain config items this needs to be managed in a particular way. If you wish to use a module then place a file named `local.json` in your config directory containing the name of your module in the following format:
287
288```json
289{
290 "modules": ["common-theme"]
291}
292```
293
294
295Command Line Options
296--------------------
297
298Several aspects of Shunter behaviour can be configured via command line arguments when you start your application.
299
300 * `-p`, `--port` Sets the port number Shunter will listen on, defaults to 5400.
301 * `-m`, `--max-post-size` Sets the maximum size in bytes for the Shunter API, defaults to 204800.
302 * `-c`, `--max-child-processes` When Shunter runs it spawns child worker processes to handle requests, this option sets the maximum number of child processes it will create. It defaults to 10, but will never exceed the number of CPU cores you have available.
303 * `-r`, `--route-config` Sets the name of the default route, see [Routing](routing.md#route-config-options) for more details. Defaults to default.
304 * `-l`, `--logging` Sets the logging level for your configured logger (e.g. 'error', 'warn', 'info', 'debug'). Defaults to 'info'.
305 * `-s`, `--syslog` Turns on logging to syslog. Boolean.
306 * `-d`, `--source-directory` Sets the root directory for your app, paths will be resolved from here. This setting is useful if you don't want to start your Shunter app from it's own directory. Defaults to the current working directory.
307 * `-o`, `--route-override` Sets the proxy destination for all requests see [Routing](routing.md#route-override) for more details.
308 * `-g`, `--origin-override` Requires `--route-override`. Sets `changeOrigin: true` for the route set up via `--route-override`, see [Routing](routing.md#route-config-options) for more details.
309 * `--rewrite-redirect` Sets `autoRewrite` option on the proxy, see the [Node HTTP Proxy documentation](https://github.com/nodejitsu/node-http-proxy#options) for more details.
310 * `--rewrite-protocol` Sets the `protocolRewrite` option on the proxy, see the [Node HTTP Proxy documentation](https://github.com/nodejitsu/node-http-proxy#options) for more details.
311 * `--compile-on-demand` Compiles templates on demand instead of at application start up, only recommended in development mode if you want to speed up the application's start time (e.g. to run automated tests).
312 * `-v`, `--version` Prints the Shunter version number.
313 * `-w`, `--preserve-whitespace` Preserves whitespace in HTML output.
314 * `-h`, `--help` Prints help about these options.
315
316
317
318Accessing the Configuration at Run Time
319---------------------------------------
320
321The final configuration actually used by Shunter at run time is available to your `app.js` via `app.getConfig()`. *Warning* modifying the returned data structure may result in unpredictable behaviour.
322
323---
324
325Related:
326
327- [Full API Documentation](index.md)