Skip to content

Commit 6487000

Browse files
authored
Merge pull request #2 from jkyberneees/env-vars
supporting environment variables
2 parents 3097d24 + 2143963 commit 6487000

File tree

4 files changed

+89
-17
lines changed

4 files changed

+89
-17
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ The `/restana-static/config` directory should be populated/overwritten during im
1919
```
2020
> File location : `/restana-static/config/default.json`
2121
22+
### Environment configuration
23+
Optionally, you can overwrite each configuration entry using corresponding environment variables.
24+
```bash
25+
# port
26+
PORT
27+
# cacheEnabled
28+
CACHE_ENABLED
29+
# cacheControlHeaderValue
30+
CACHE_CONTROL_HEADER_VALUE
31+
# distDirectory
32+
DIST_DIRECTORY
33+
# defaultFile
34+
DEFALUT_FILE
35+
# logsFormat
36+
LOGS_FORMAT
37+
```
38+
2239
## Adding static files
2340
The `/restana-static/dist` directory should be populated/overwritten during image creation.
2441
> Please consider that default path will change if you overwrite the `distDirectory` config.

server.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,39 @@ const path = require('path')
44
const cache = require('http-cache-middleware')
55
const 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+
}

tests/smoke.test.js renamed to tests/1.config-files.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const expect = require('chai').expect
55
const request = require('supertest')
66

7-
describe('Restana Web Framework - Smoke', () => {
7+
describe('Smoke - Config files', () => {
88
let server
99

1010
it('should successfully start server', async () => {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
/* global describe, it */
4+
const expect = require('chai').expect
5+
const request = require('supertest')
6+
7+
describe('Smoke - Environment based configuration', () => {
8+
let server
9+
10+
it('should successfully start server - env based config', async () => {
11+
process.env.PORT = 5000
12+
process.env.CACHE_ENABLED = false
13+
14+
const { server: service, port } = require('../server')()
15+
16+
server = await service.start(port)
17+
})
18+
19+
it('should GET index.html', async () => {
20+
await request(server)
21+
.get('/')
22+
.expect(200)
23+
.then((res) => {
24+
expect(res.text).to.equal('<h1>Hello from restana-static!</h1>')
25+
expect(res.headers['cache-control']).to.equal('public, max-age=0')
26+
})
27+
})
28+
29+
it('should successfully terminate the server', async () => {
30+
await server.close()
31+
})
32+
})

0 commit comments

Comments
 (0)