init.lua 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. local skynet = require "skynet"
  2. -- local httpc = require "http.httpc"
  3. -- local httpurl = require "http.url"
  4. -- local dns = require "skynet.dns"
  5. local httpd = require "http.httpd"
  6. local sockethelper = require "http.sockethelper"
  7. local socket = require "skynet.socket"
  8. local cjson = require "cjson"
  9. local tools = require "tools"
  10. local function handle_options(fd, header)
  11. local response_header = {
  12. ['Access-Control-Allow-Origin'] = '*',
  13. ['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS, PUT, DELETE',
  14. ['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization',
  15. ['Access-Control-Allow-Credentials'] = 'true',
  16. ['Content-Length'] = '0'
  17. }
  18. local content = ""
  19. local ok, err = httpd.write_response(sockethelper.writefunc(fd), 200, response_header, content)
  20. return ok
  21. end
  22. local dispatch = function(session, address,id, addr,...)
  23. socket.start(id)
  24. local req= tools.read_request(id)
  25. if req.url == nil or #req.url==0 or req.code~=200 or #req.body==0 then
  26. skynet.error(req.url)
  27. skynet.error("错误访问:")
  28. tools.dump({address=address,id=id,addr=addr,other=...})
  29. return tools.response(id, req.code, "error!")
  30. end
  31. -- 处理 OPTIONS 请求
  32. -- if req.method == "OPTIONS" then
  33. -- handle_options(id,req.header)
  34. -- socket.close(id)
  35. -- return tools.response(id, req.code, "error!")
  36. -- end
  37. if string.find(req.url, "tg/back/") then
  38. local func_list = string.split(req.url ,"/")
  39. skynet.send("backmgr","lua","on_recv",id,func_list[#func_list],req.body)
  40. return
  41. end
  42. end
  43. skynet.start(function()
  44. skynet.dispatch("lua", dispatch)
  45. end)