init.lua 2.5 KB

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