123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- --[[
- Descripttion:
- version:
- Author: Neo,Huang
- Date: 2022-07-06 10:42:14
- LastEditors: Neo,Huang
- LastEditTime: 2022-07-06 10:54:02
- --]]
- require "skynet.manager"
- local skynet = require "skynet"
- local sharetable = require "skynet.sharetable"
- local socket = require "socket"
- local webapp = require "webapp"
- local lib_url = require "http.url"
- local httpd = require "http.httpd"
- local sockethelper = require "http.sockethelper"
- local hotfixHelper = require "hotfix.helper"
- local body_limit = ...
- local function response(id, ...)
- local ok, err = httpd.write_response(sockethelper.writefunc(id), ...)
- if not ok then
- -- if err == sockethelper.socket_error , that means socket closed.
- skynet.error(string.format("fd = %d, %s", id, err))
- end
- end
- local root = {}
- function root.http(id, ip)
- socket.start(id)
- -- limit request body size to 8192 (you can pass nil to unlimit)
- local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), tonumber(body_limit) or 8192)
- -- skynet.error(string.format("code:%s, url:%s, method:%s, header:%s, body=%s", code, url, method, header, body))
- if code then
- if code ~= 200 then
- response(id, code)
- else
- local path, query = lib_url.parse(url)
- local q = {}
- if query then
- q = lib_url.parse_query(query)
- end
- response(id, webapp:http_request(ip, url, method, header, path, q, body))
- end
- else
- if url == sockethelper.socket_error then
- skynet.error("socket closed")
- else
- skynet.error(url)
- end
- end
- socket.close(id)
- end
- -- 更新配置表
- function root.update_config(obj, ...)
- -- 由于对战机器人等其他服务是传自身对象作为第一个参数
- if type(obj) == "string" then
- sharetable.update(obj, ...)
- else
- sharetable.update(...)
- end
- end
- -- 更新协议
- function root.update_proto()
- webapp:update()
- end
- -- 更新逻辑
- function root.update_logic(changeList, params)
- -- 由于对战机器人等其他服务是传自身对象作为第一个参数
- local fileChangeList = changeList
- for _, v in pairs(changeList) do
- if type(v) ~= "string" then
- fileChangeList = params
- break
- end
- end
- hotfixHelper.update(fileChangeList)
- end
- function root.exit()
- -- 发送服务停止并提出
- local addr = skynet.localname(".steward")
- skynet.send(addr, "lua", "stop_service", skynet.self())
- skynet.exit()
- end
- skynet.start(
- function()
- webapp:init()
- skynet.dispatch(
- "lua",
- function(_, _, cmd, ...)
- local f = assert(root[cmd], cmd)
- skynet.retpack(f(...))
- end
- )
- skynet.send(".steward", "lua", "start_service", skynet.self())
- end
- )
|