Skip to content

Commit 93f68c8

Browse files
Add cache control on web base
1 parent 19c6f9d commit 93f68c8

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

extensions/web-base/manifest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
"type": "string",
1515
"default": "assets"
1616
},
17+
"cache": {
18+
"title": "Cache control duration, 0 to disable",
19+
"type": "integer",
20+
"default": 86400
21+
},
1722
"title": {
1823
"title": "Web page title",
1924
"type": "string",

extensions/web-base/web-base.lua

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,18 @@ local function addContext(server, ...)
4444
table.insert(contexts, context)
4545
end
4646

47-
local function onWebSocketClose(self)
48-
logger:fine('WebSocket closed')
49-
List.removeFirst(websockets, self)
47+
local function onWebSocketClose(webSocket)
48+
logger:fine('WebSocket closed '..tostring(webSocket))
49+
List.removeFirst(websockets, webSocket)
5050
end
5151

5252
local batchDataChange = true
5353
local dataChangeEvent = nil
5454

5555
local function onDataChange(value, previousValue, path)
56-
logger:fine('onDataChange "'..tostring(path)..'": "'..tostring(value)..'"')
56+
if logger:isLoggable(logger.FINE) then
57+
logger:fine('onDataChange() "'..tostring(path)..'": "'..tostring(value)..'" '..tostring(#websockets))
58+
end
5759
if #websockets == 0 then
5860
return
5961
end
@@ -101,11 +103,12 @@ function extension:unregisterAddon(name)
101103
end
102104

103105
function extension:registerAddonExtension(ext, script)
106+
local configuration = extension:getConfiguration()
104107
if script == true then
105108
script = ext:getId()..'.js'
106109
end
107110
self:registerAddon(ext:getId(), {
108-
handler = AddonFileHttpHandler:new(ext:getDir()),
111+
handler = AddonFileHttpHandler:new(ext:getDir()):setCacheControl(configuration.cache),
109112
script = script or 'main.js' -- TODO change to init
110113
})
111114
end
@@ -136,11 +139,13 @@ extension:subscribeEvent('startup', function()
136139
local wwwDir = File:new(extension:getDir(), 'www')
137140

138141
cleanup(server)
139-
addContext(server, '/(.*)', FileHttpHandler:new(wwwDir, 'r', 'app.html'))
142+
addContext(server, '/(.*)', FileHttpHandler:new(wwwDir, 'r', 'app.html'):setCacheControl(configuration.cache))
140143
addContext(server, '/static/(.*)', assetsHandler)
141144
addContext(server, '/addon/([^/]*)/?(.*)', HttpHandler:new(function(self, exchange)
142145
local name, path = exchange:getRequestArguments()
143-
logger:fine('add-on handler "'..tostring(name)..'" / "'..tostring(path)..'"')
146+
if logger:isLoggable(logger.FINE) then
147+
logger:fine('add-on handler "'..tostring(name)..'" / "'..tostring(path)..'"')
148+
end
144149
if name == '' then
145150
local list = {}
146151
for id, addon in pairs(addons) do
@@ -153,15 +158,19 @@ extension:subscribeEvent('startup', function()
153158
else
154159
local addon = addons[name]
155160
if addon and addon.handler then
156-
logger:fine('calling add-on "'..tostring(name)..'" handler')
161+
if logger:isLoggable(logger.FINE) then
162+
logger:fine('calling add-on "'..tostring(name)..'" handler')
163+
end
157164
return addon.handler:handle(exchange)
158165
end
159166
HttpExchange.notFound(exchange)
160167
end
161168
end))
162169
addContext(server, '/ws/', Map.assign(WebSocketUpgradeHandler:new(), {
163170
onOpen = function(_, webSocket, exchange)
164-
logger:fine('WebSocket openned')
171+
if logger:isLoggable(logger.FINE) then
172+
logger:fine('WebSocket openned '..tostring(webSocket))
173+
end
165174
table.insert(websockets, webSocket)
166175
webSocket.onClose = onWebSocketClose
167176
end

0 commit comments

Comments
 (0)