init.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 dispatch = function(session, address,id, addr,...)
  11. socket.start(id)
  12. local req = tools.read_request(id)
  13. -- skynet.error('id,',id)
  14. -- skynet.error('addr,',addr)
  15. -- skynet.error('url,',req.url)
  16. -- skynet.error('header,',)
  17. -- skynet.error('body,',req.body)
  18. -- local color,text = "red", "hello"
  19. -- skynet.error("req.url",req.url)
  20. if string.find(req.url, "/config/") then
  21. skynet.send("config_mgr","lua","on_recv",id,req.url,req.body)
  22. return
  23. end
  24. if string.find(req.url, "back/") then
  25. skynet.send("backmgr","lua","on_recv",id,req.url,req.body)
  26. return
  27. end
  28. if req.url == nil or #req.url==0 or req.code~=200 or #req.body==0 then
  29. skynet.error(req.url)
  30. return tools.response(id, req.code, "error!")
  31. end
  32. if string.find(req.url, "tools/") then
  33. skynet.send("tools_work","lua","on_recv",id,req.url,req.body)
  34. elseif string.find(req.url, "public") then
  35. local token = req.header.token
  36. local user_data = nil
  37. skynet.error(" header.token", req.header.token)
  38. if token~=nil and #token>1 then
  39. local user_data = tools.tokenDecode(token)
  40. user_data = cjson.decode(user_data)
  41. skynet.error("user_data",user_data)
  42. skynet.send("public_mgr","lua","on_recv",id,req.url,req.body,user_data)
  43. else
  44. skynet.send("public_mgr","lua","on_recv",id,req.url,req.body,nil)
  45. end
  46. else
  47. -- local header = cjson.encode(req.header)
  48. local token = req.header.token
  49. local user_data = nil
  50. skynet.error(" header.token", req.header.token)
  51. if token~=nil and #token>1 then
  52. local user_data = tools.tokenDecode(token)
  53. user_data = cjson.decode(user_data)
  54. skynet.error("user_data",user_data)
  55. skynet.send("agentmgr","lua","on_recv",id,req.url,req.body,user_data)
  56. else
  57. skynet.send("agentmgr","lua","on_recv",id,req.url,req.body,nil)
  58. end
  59. end
  60. end
  61. skynet.start(function()
  62. skynet.dispatch("lua", dispatch)
  63. end)