Skip to content

Commit 6b4d9c7

Browse files
committed
+ custom refresh after setup
1 parent a085e79 commit 6b4d9c7

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ curl http://localhost:8000/api/v1/apod.json -s | jq
3636
- `SERVICE_BASE_URL` (defaults to empty string, eg: `http://localhost:8000` )
3737
- `APOD_PAGE_MAX_SIZE` (defaults to 50kb) -- to save your server
3838
- `APOD_IMG_MAX_SIZE` (defaults to 10Mb) -- to save your server
39+
- `REFRESH_AFTER_HR` (defaults to 2) -- period to refresh the cache
3940

4041
### Pre-built docker images:
4142
- `bruttazz/apod-api`

nginx.conf

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,17 @@ http {
4343
if res.status == 200 then
4444
info = cjson.decode(res.body)
4545

46-
local year, month, day = info.date:match("^(%d+)%-(%d+)%-(%d+)$")
46+
local year, month, day, hr, min, sec = info.date:match("^(%d+)%-(%d+)%-(%d+)%-(%d+)%-(%d+)%-(%d+)$")
4747
local time = os.time({
4848
year = tonumber(year),
4949
month = tonumber(month),
5050
day = tonumber(day),
51-
hour = 0,
52-
min = 0,
53-
sec = 0
51+
hour = tonumber(hr),
52+
min = tonumber(min),
53+
sec = tonumber(sec),
5454
})
5555

56-
local today = os.date("!*t")
57-
today.hour = 0
58-
today.min = 0
59-
today.sec = 0
60-
61-
if time < os.time(today) then
56+
if os.time(os.date("!*t")) - time > settings.REFRESH_AFTER_S then
6257
info = apod.extract()
6358
if not info then
6459
ngx.status = 500
@@ -74,6 +69,7 @@ http {
7469
ngx.exit(500)
7570
end
7671
end
72+
info.date = os.date("%Y-%m-%d")
7773
ngx.say(cjson.encode(info))
7874
}
7975
}

src/apod.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function _apod_info_generator.extract()
190190

191191
-- write info
192192
info.img = settings.SERVICE_BASE_URL .. settings.API_PREFIX .. img_name
193-
info.date = os.date("%Y-%m-%d")
193+
info.date = os.date("%Y-%m-%d-%H-%M-%S")
194194
info.source = settings.APOD_PAGE_URL
195195
info.credits = settings.APOD_HOST
196196

src/settings.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ local _settings = {
22
APOD_PAGE_URL = os.getenv("APOD_PAGE_URL") or "http://www.star.ucl.ac.uk/~apod/apod/",
33
SERVICE_BASE_URL = os.getenv("SERVICE_BASE_URL") or "", -- without trailing slash
44

5-
APOD_PAGE_MAX_SIZE = tonumber(os.getenv("APOD_PAGE_MAX_SIZE") or 50000), -- average ~4397
6-
APOD_IMG_MAX_SIZE = tonumber(os.getenv("APOD_IMG_MAX_SIZE") or 10000000), -- average ~362026
5+
APOD_PAGE_MAX_SIZE = tonumber(os.getenv("APOD_PAGE_MAX_SIZE")) or 50000, -- average ~4397
6+
APOD_IMG_MAX_SIZE = tonumber(os.getenv("APOD_IMG_MAX_SIZE")) or 10000000, -- average ~362026
77

88
WWW_DIR = os.getenv("WWW_DIR") or ".", -- without trailing slash
9-
API_PREFIX = "/api/v1/"
9+
API_PREFIX = "/api/v1/",
10+
11+
REFRESH_AFTER_HR = tonumber(os.getenv("REFRESH_AFTER_HR")) or 2,
1012
}
1113
_settings.APOD_HOST = _settings.APOD_PAGE_URL:match("^%w+://([^/]+)")
12-
-- _settings.APOD_INFO_FILE = _settings.WWW_DIR .. "../",
14+
_settings.REFRESH_AFTER_S = _settings.REFRESH_AFTER_HR * 60 * 60
1315

1416
if os.getenv("LOCAL_ENV") then
1517
package.path = "./deps/lua-htmlparser/src/?.lua;" .. package.path

0 commit comments

Comments
 (0)