1 |
|
2 | Configuration Reference
|
3 | =======================
|
4 |
|
5 | The 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 |
|
22 | Web Configuration
|
23 | -----------------
|
24 |
|
25 | The 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
|
28 | web: {
|
29 | public: '/public',
|
30 | publicResources: '/public/resources',
|
31 | resources: '/resources',
|
32 | tests: '/tests'
|
33 | }
|
34 | ```
|
35 |
|
36 |
|
37 | Path Configuration
|
38 | ------------------
|
39 |
|
40 | The 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
|
43 | path: {
|
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 |
|
59 | Structure Configuration
|
60 | -----------------------
|
61 |
|
62 | The structure object defines the directory structure where Shunter's resources will reside:
|
63 |
|
64 | ```js
|
65 | structure: {
|
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 |
|
102 | Log Configuration
|
103 | -----------------
|
104 |
|
105 | The log object defines the tool that Shunter should use for logging. By default Shunter uses [Winston](https://github.com/winstonjs/winston).
|
106 |
|
107 | You 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
|
110 | log: 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 |
|
121 | The 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 |
|
123 | Shunter'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';
|
127 | module.exports = function(level, msg, meta) {
|
128 | return 'filtered message: ' + msg;
|
129 | }
|
130 | ```
|
131 |
|
132 | StatsD Configuration
|
133 | --------------------
|
134 |
|
135 | The `statsd` option defines the configuration used for the StatsD network daemon used for collecting metrics for graphing.
|
136 |
|
137 |
|
138 | Timer Configuration
|
139 | -------------------
|
140 |
|
141 | the 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
|
144 | timer: 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 |
|
154 | Trigger Parameter
|
155 | -----------------
|
156 |
|
157 | The 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 |
|
159 | This 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 |
|
161 | The default value is:
|
162 |
|
163 | ```js
|
164 | trigger {
|
165 | header: 'Content-type',
|
166 | matchExpression: 'application/x-shunter\\+json'
|
167 | }
|
168 | ```
|
169 |
|
170 | Any 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 |
|
172 | JSON View Parameter
|
173 | -------------------
|
174 |
|
175 | Sometimes 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 |
|
177 | By 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 |
|
179 | This config property sets the name of the query parameter that triggers raw JSON serving:
|
180 |
|
181 | ```js
|
182 | shunter({
|
183 | jsonViewParameter: 'show-me-the-json'
|
184 | });
|
185 | ```
|
186 |
|
187 | With 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 |
|
194 | Environment Configuration
|
195 | -------------------------
|
196 |
|
197 | The `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
|
200 | env: {
|
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 |
|
214 | You may like to modify this config object to reflect the environments to which you will be deploying your Shunter application.
|
215 |
|
216 |
|
217 | Templated Error Page Configuration
|
218 | ----------------------------------
|
219 |
|
220 | Optionally, 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
|
223 | errorPages: {
|
224 | errorLayouts: {
|
225 | default: 'layout',
|
226 | 404: 'layout-error-404'
|
227 | },
|
228 | staticData: {
|
229 | yourData: {
|
230 | goes: 'here'
|
231 | }
|
232 | }
|
233 | }
|
234 | ```
|
235 |
|
236 | The 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 |
|
238 | Any 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 |
|
240 | Additionally, 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
|
243 | layout: {
|
244 | template: 'layout-error-404',
|
245 | namespace: 'custom-errors'
|
246 | },
|
247 | errorContext: {
|
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 | },
|
258 | yourData: {
|
259 | goes: 'here'
|
260 | }
|
261 | ```
|
262 |
|
263 | If you require a large set of `staticData`, it may be more appropriate to include it as a Custom Configuration.
|
264 |
|
265 |
|
266 | Adding Custom Configurations
|
267 | ----------------------------
|
268 |
|
269 | The 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
|
272 | var 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 |
|
283 | Configuring Modules
|
284 | -------------------
|
285 |
|
286 | Shunter 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 |
|
295 | Command Line Options
|
296 | --------------------
|
297 |
|
298 | Several 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 |
|
318 | Accessing the Configuration at Run Time
|
319 | ---------------------------------------
|
320 |
|
321 | The 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 |
|
325 | Related:
|
326 |
|
327 | - [Full API Documentation](index.md)
|