-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcreateAssetServer.js
More file actions
78 lines (63 loc) · 2.1 KB
/
createAssetServer.js
File metadata and controls
78 lines (63 loc) · 2.1 KB
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
const fs = require('fs')
const url = require('url')
const assert = require('assert')
const { join, basename } = require('path')
const merge = require('ramda/src/merge')
const feathers = require('feathers')
const configuration = require('feathers-configuration')
const httpLogger = require('pino-http')
const compress = require('compression')
const helmet = require('helmet')
const favicon = require('serve-favicon')
const forceSsl = require('express-enforces-ssl')
const propOr = require('ramda/src/propOr')
const Bundler = require('bankai/http')
const createLog = require('./createLog')
const normalizePort = require('./lib/normalizePort')
const startServer = require('./lib/startServer')
const getEntryFile = propOr('browser.js', 'entry')
module.exports = createServer
function createServer (options) {
const {
cwd = process.cwd()
} = options
const app = feathers()
// load config from ./config
app.configure(configuration())
const logConfig = app.get('log')
const log = createLog({ name: basename(cwd), level: logConfig.level })
app.set('log', log)
const assetConfig = app.get('asset')
assert(assetConfig, 'must set `asset` in config. example: "asset"')
const assetUrl = url.parse(assetConfig.url)
app.set('port', assetConfig.port)
app.set('host', assetUrl.hostname)
// log requests and responses
app.use(httpLogger({ logger: log }))
// gzip compression
app.use(compress())
// http security headers
app.use(helmet())
// favicon
const faviconConfig = app.get('favicon')
assert(faviconConfig, 'must set `favicon` in config. example: "app/favicon.ico"')
app.use(favicon(faviconConfig))
// static files
if (assetConfig.root) {
app.use('/', feathers.static(assetConfig.root, assetConfig))
}
// javascript bundler
const entryFile = getEntryFile(assetConfig)
const entryPath = join(cwd, entryFile)
const bundlerHandler = Bundler(entryPath, {
dirname: cwd
})
const compiler = bundlerHandler.compiler
app.use(bundlerHandler)
compiler.on('error', (nodeName, edgeName, err) => {
log.fatal(err)
})
return (cb) => {
return startServer(app, cb)
}
}