Skip to content

Commit 038f9b7

Browse files
authored
Merge pull request #6 from strongloop/nginx-maxage-support
nginx: add support for MAXAGE env var
2 parents 34d4468 + b4efc21 commit 038f9b7

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ MAINTAINER Ryan Graham <[email protected]>
33

44
RUN apk --no-cache add dnsmasq
55

6-
ADD entrypoint.sh nginx.conf ephemeral-npm.lua /
6+
ADD entrypoint.sh nginx.conf ephemeral-npm.lua ephemeral-utils.lua /
77

8-
# Not port 80 because ephemeral-npm stnadardized on couchdb's port already
8+
# Not port 80 because ephemeral-npm standardized on couchdb's port already
99
EXPOSE 4873
1010

1111
ENTRYPOINT ["/entrypoint.sh"]

entrypoint.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
# [x] npm_config_registry
55
# [ ] MAX_BODY_SIZE
66
# [ ] MAX_USERS
7-
# [ ] MAXAGE
7+
# [x] MAXAGE
88
# [ ] TIMEOUT
99
# [ ] NPM_SECRET
1010
# [ ] NPM_USER
1111
# [ ] NPM_PASSWORD
1212

1313
export npm_config_registry=${npm_config_registry:-https://registry.npmjs.org}
14+
export MAXAGE=${MAXAGE:-5m}
1415

1516
# necessary because nginx requires a resolver when upstreams are dynamic
1617
dnsmasq --listen-address=127.0.0.1 --user=root

ephemeral-npm.lua

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
local cjson = require "cjson.safe"
2+
local utils = require "ephemeral-utils"
23

34
local _M = {}
45

5-
local function parseDuration(str)
6-
-- TODO: parse inputs like "1h", "2d", "10m" and return them as seconds
7-
return 5 * 60
8-
end
9-
106
function _M.init()
117
local npmConfig = ngx.shared.npmConfig
128
_M.registry = os.getenv("npm_config_registry"):gsub("/+$", "")
139
_M.hostPattern = _M.registry:gsub("%.", "%%."):gsub("%-", "%%-")
14-
_M.MAXAGE = parseDuration(os.getenv("MAXAGE") or "5m")
10+
_M.MAXAGE = utils.parseDuration(os.getenv("MAXAGE") or "5m")
1511
-- escape . and - which have special meaning in Lua patterns
1612
npmConfig:set('npm_config_registry', _M.registry)
1713
npmConfig:set('npm_upstream_pattern', _M.hostPattern)

ephemeral-utils.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
local _Utils = {}
2+
3+
local SECONDS = {
4+
s = 1,
5+
m = 60,
6+
h = 60 * 60,
7+
d = 24 * 60 * 60,
8+
w = 7 * 24 * 60 * 60,
9+
M = 30 * 24 * 60 * 60,
10+
y = 365 * 24 * 60 * 60,
11+
}
12+
13+
function _Utils.parseDuration(str)
14+
local total = 0
15+
for n,scale in string.gmatch(str, "(%d+)([smhdwMy])") do
16+
total = total + n * SECONDS[scale]
17+
end
18+
return total
19+
end
20+
21+
assert(_Utils.parseDuration('5m') == 300)
22+
assert(_Utils.parseDuration('10m') == 600)
23+
assert(_Utils.parseDuration('1h') == 60 * 60)
24+
assert(_Utils.parseDuration('1h1m') == 61 * 60)
25+
26+
return _Utils

0 commit comments

Comments
 (0)