@@ -4,25 +4,39 @@ const path = require('path')
44const cache = require ( 'http-cache-middleware' )
55const morgan = require ( 'morgan' )
66
7- // configuration
8- const distDirectory = config . get ( 'distDirectory' ) || 'dist/'
9- const port = config . get ( 'port' ) || 3000
10- const cacheEnabled = config . get ( 'cacheEnabled' ) || true
11- const cacheControlHeaderValue = config . get ( 'cacheControlHeaderValue' ) || 'public, no-cache, max-age=604800'
12- const defaultFile = config . get ( 'defaultFile' ) || 'index.html'
13- const logsFormat = config . get ( 'logsFormat' ) || 'tiny'
7+ module . exports = ( ) => {
8+ // configuration
9+ const {
10+ DIST_DIRECTORY ,
11+ PORT ,
12+ CACHE_ENABLED ,
13+ CACHE_CONTROL_HEADER_VALUE ,
14+ DEFALUT_FILE ,
15+ LOGS_FORMAT
16+ } = process . env
17+
18+ const distDirectory = DIST_DIRECTORY || config . get ( 'distDirectory' ) ||
19+ 'dist/'
20+ const port = PORT || config . get ( 'port' ) ||
21+ 3000
22+ const cacheEnabled = isCacheEnabled ( CACHE_ENABLED , config )
23+ const cacheControlHeaderValue = CACHE_CONTROL_HEADER_VALUE || config . get ( 'cacheControlHeaderValue' ) ||
24+ 'public, no-cache, max-age=604800'
25+ const defaultFile = DEFALUT_FILE || config . get ( 'defaultFile' ) ||
26+ 'index.html'
27+ const logsFormat = LOGS_FORMAT || config . get ( 'logsFormat' ) ||
28+ 'tiny'
1429
15- // middleware for serving static files
16- const serve = files ( path . join ( __dirname , distDirectory ) , {
17- lastModified : false ,
18- setHeaders : ( res ) => {
19- if ( cacheEnabled ) {
20- res . setHeader ( 'cache-control' , cacheControlHeaderValue )
30+ // middleware for serving static files
31+ const serve = files ( path . join ( __dirname , distDirectory ) , {
32+ lastModified : false ,
33+ setHeaders : ( res ) => {
34+ if ( cacheEnabled ) {
35+ res . setHeader ( 'cache-control' , cacheControlHeaderValue )
36+ }
2137 }
22- }
23- } )
38+ } )
2439
25- module . exports = ( ) => {
2640 // server bootstrap
2741 const server = require ( 'restana' ) ( { } )
2842 server . use ( morgan ( logsFormat ) )
@@ -43,3 +57,12 @@ module.exports = () => {
4357 port
4458 }
4559}
60+
61+ function isCacheEnabled ( env , config ) {
62+ if ( env !== undefined ) {
63+ if ( env === 'true' ) return true
64+ else return false
65+ } else {
66+ return config . get ( 'cacheEnabled' ) || true
67+ }
68+ }
0 commit comments