srvHttpAgent.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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:54:02
  8. --]]
  9. require "skynet.manager"
  10. local skynet = require "skynet"
  11. local sharetable = require "skynet.sharetable"
  12. local socket = require "socket"
  13. local webapp = require "webapp"
  14. local lib_url = require "http.url"
  15. local httpd = require "http.httpd"
  16. local sockethelper = require "http.sockethelper"
  17. local hotfixHelper = require "hotfix.helper"
  18. local body_limit = ...
  19. local function response(id, ...)
  20. local ok, err = httpd.write_response(sockethelper.writefunc(id), ...)
  21. if not ok then
  22. -- if err == sockethelper.socket_error , that means socket closed.
  23. skynet.error(string.format("fd = %d, %s", id, err))
  24. end
  25. end
  26. local root = {}
  27. function root.http(id, ip)
  28. socket.start(id)
  29. -- limit request body size to 8192 (you can pass nil to unlimit)
  30. local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), tonumber(body_limit) or 8192)
  31. -- skynet.error(string.format("code:%s, url:%s, method:%s, header:%s, body=%s", code, url, method, header, body))
  32. if code then
  33. if code ~= 200 then
  34. response(id, code)
  35. else
  36. local path, query = lib_url.parse(url)
  37. local q = {}
  38. if query then
  39. q = lib_url.parse_query(query)
  40. end
  41. response(id, webapp:http_request(ip, url, method, header, path, q, body))
  42. end
  43. else
  44. if url == sockethelper.socket_error then
  45. skynet.error("socket closed")
  46. else
  47. skynet.error(url)
  48. end
  49. end
  50. socket.close(id)
  51. end
  52. -- 更新配置表
  53. function root.update_config(obj, ...)
  54. -- 由于对战机器人等其他服务是传自身对象作为第一个参数
  55. if type(obj) == "string" then
  56. sharetable.update(obj, ...)
  57. else
  58. sharetable.update(...)
  59. end
  60. end
  61. -- 更新协议
  62. function root.update_proto()
  63. webapp:update()
  64. end
  65. -- 更新逻辑
  66. function root.update_logic(changeList, params)
  67. -- 由于对战机器人等其他服务是传自身对象作为第一个参数
  68. local fileChangeList = changeList
  69. for _, v in pairs(changeList) do
  70. if type(v) ~= "string" then
  71. fileChangeList = params
  72. break
  73. end
  74. end
  75. hotfixHelper.update(fileChangeList)
  76. end
  77. function root.exit()
  78. -- 发送服务停止并提出
  79. local addr = skynet.localname(".steward")
  80. skynet.send(addr, "lua", "stop_service", skynet.self())
  81. skynet.exit()
  82. end
  83. skynet.start(
  84. function()
  85. webapp:init()
  86. skynet.dispatch(
  87. "lua",
  88. function(_, _, cmd, ...)
  89. local f = assert(root[cmd], cmd)
  90. skynet.retpack(f(...))
  91. end
  92. )
  93. skynet.send(".steward", "lua", "start_service", skynet.self())
  94. end
  95. )