Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
1
/*
2
* ecstatic.js: Top-level plugin exposing ecstatic's static server to flatiron app
3
*
4
* (C) 2012, Nodejitsu, Inc.
5
* MIT LICENSE
6
*
7
*/
8
9
var path = require('path'),
10
flatiron = require('../../flatiron'),
11
common = flatiron.common,
12
ecstatic;
13
14
try {
15
//
16
// Attempt to require ecstatic.
17
//
18
ecstatic = require('ecstatic');
19
}
20
catch (ex) {
21
//
22
// Do nothing since this is a progressive enhancement
23
//
24
console.warn('flatiron.plugins.ecstatic requires the `ecstatic` module from npm');
25
console.warn('install using `npm install ecstatic`.');
26
console.trace();
27
process.exit(1);
28
}
29
30
exports.name = 'ecstatic';
31
32
exports.attach = function (options) {
33
var app = this;
34
35
options = options || {};
36
37
//
38
// Accept string `options`
39
//
40
if (typeof options === 'string') {
41
options = { root: options };
42
}
43
44
//
45
// Attempt to merge defaults passed to `app.use(flatiron.plugins.ecstatic)`
46
// with any additional configuration that may have been loaded
47
options = common.mixin(
48
{},
49
options,
50
app.config.get('ecstatic') || {}
51
);
52
53
app.config.set('ecstatic', options);
54
55
//
56
// `app.static` api to be used by other plugins
57
// to server static files
58
//
59
app.static = function (dir) {
60
app.http.before = app.http.before.concat(ecstatic(dir, options));
61
}
62
63
// * `options.dir`: Explicit path to assets directory
64
// * `options.root`: Relative root to the assets directory ('/app/assets')
65
// * `app.root`: Relative root to the assets directory ('/app/assets')
66
if (options.dir || options.root || app.root) {
67
app._ecstaticDir = options.dir
68
|| path.join(options.root || app.root, 'app', 'assets');
69
70
//
71
// Serve ecstaticDir using middleware in union
72
//
73
app.static(app._ecstaticDir);
74
}
75
}
76
77