staticfile.lua 645 B

12345678910111213141516171819202122232425262728293031323334
  1. --[[
  2. Descripttion:
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-07-06 10:42:14
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-07-06 10:49:07
  8. --]]
  9. local skynet = require "skynet"
  10. local io = io
  11. local root = skynet.getenv "static_path" or "../nodes/web/static/"
  12. local cache = setmetatable({}, {__mode = "kv"})
  13. local function cachefile(_, filename)
  14. local v = cache[filename]
  15. if v then
  16. return v[1]
  17. end
  18. local f = io.open(root .. filename)
  19. if f then
  20. local content = f:read "a"
  21. f:close()
  22. cache[filename] = {content}
  23. return content
  24. else
  25. cache[filename] = {}
  26. end
  27. end
  28. local staticfile = setmetatable({}, {__index = cachefile})
  29. return staticfile