Z1maV1

Express.lua lib

May 6th, 2024
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. local urlCodec = require("/lib/url")
  2. local str = require("/lib/string")
  3.  
  4. server = {
  5. onClose = function() return true end,
  6. next_fun = function (self)
  7. currRouteIteration = currRouteIteration + 1
  8. if currRouteIteration > routesCount then
  9. goto finish_next1
  10. elseif genRoutes[currRouteIteration] then
  11. if genRoutes[currRouteIteration]["all"] then
  12. genRoutes[currRouteIteration]["all"](self.req, self.res, server.next_fun)
  13. elseif genRoutes[currRouteIteration][string.lower(self.req["method"])] then
  14. genRoutes[currRouteIteration][string.lower(self.req["method"])](self.req, self.res, server.next_fun)
  15. end
  16. elseif routes[currRouteIteration] then
  17. if routes[currRouteIteration][self.req["path"]]then
  18. if routes[currRouteIterations][self.req["path"]]["all"] then
  19. routes[currRouteIteration][self.req["path"]]["all"](self.req, self.res, server.next_fun)
  20. elseif routes[currRouteIteration][self.req["path"]][string.lower(self.req["method"])] then
  21. routers[currRouteIteration][self.req["path"]][string.lower(self.req["method"])](self.req, self.res, server.next_fun)
  22. end
  23. end
  24. end
  25. ::finish_next1::
  26. end,
  27. res = {},
  28. req = {}
  29. }
  30.  
  31. routesCount = 0
  32. genRoutes = {
  33.  
  34. }
  35.  
  36. routes = {
  37.  
  38. }
  39.  
  40. currRouteIteration = 0
  41.  
  42. request = {
  43. ["method"] = "",
  44. ["path"] = "",
  45. ["body"] = {},
  46. ["headers"] = {
  47. ["User-Agent"] = ""
  48. }
  49. }
  50.  
  51. response = {
  52. ["status"] = 200,
  53. ["body"] = {},
  54. ["headers"] = {
  55. ["Content-Type"] = "application/json",
  56. }
  57. }
  58.  
  59. function handleExit()
  60. while true do
  61. event = os.pullEventRaw("terminate")
  62. if event == "terminate" then
  63. rednet.close()
  64. print("stopping server")
  65. break
  66. end
  67. end
  68. end
  69.  
  70.  
  71. function server:create(protocol, routerLoc)
  72. newObj = {}
  73. newObj.protocol = protocol
  74. newObj.router = routerLoc
  75. setmetatable(newObj, self)
  76. self.__index = self
  77. return newObj
  78. end
  79.  
  80. function server:use(callback, path)
  81. routesCount = routesCount + 1
  82.  
  83. if path then
  84. routes[routesCount] = {["path"] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}}
  85. routes[routesCount][path]["all"] = callback
  86. else
  87. genRoutes[routesCount] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}
  88. genRoutes[routesCount]["all"] = callback
  89. end
  90. end
  91.  
  92. function server:get(callback, path)
  93. routesCount = routesCount + 1
  94. if path then
  95. routes[routesCount] = {["path"] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}}
  96. routes[routesCount][path]["get"] = callback
  97. else
  98. genRoutes[routesCount] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}
  99. genRoutes[routesCount]["get"] = callback
  100. end
  101. end
  102.  
  103. function server:post(callback, path)
  104. routesCount = routesCount + 1
  105. if path then
  106. routes[routesCount] = {["path"] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}}
  107. routes[routesCount][path]["post"] = callback
  108. else
  109. genRoutes[routesCount] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}
  110. genRoutes[routesCount]["post"] = callback
  111. end
  112. end
  113.  
  114. function server:put(callback, path)
  115. routesCount = routesCount + 1
  116. if path then
  117. routes[routesCount] = {["path"] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}}
  118. routes[routesCount][path]["put"] = callback
  119. else
  120. genRoutes[routesCount] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}
  121. genRoutes[routesCount]["put"] = callback
  122. end
  123. end
  124.  
  125. function server:delete(callback, path)
  126. routesCount = routesCount + 1
  127. if path then
  128. routes[routesCount] = {["path"] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}}
  129. routes[routesCount][path]["delete"] = callback
  130. else
  131. genRoutes[routesCount] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}
  132. genRoutes[routesCount]["delete"] = callback
  133. end
  134. end
  135.  
  136. function server:start()
  137. parallel.waitForAny(handleExit, function()
  138. rednet.open(self.router)
  139.  
  140. print("Running server on '"..self.protocol.."'")
  141.  
  142. while true do
  143. id, msg = rednet.receive(self.protocol)
  144.  
  145. self.req = textutils.unserialize(msg)
  146. self.req["id"] = id
  147.  
  148. local strSep = str.split(self.req["path"], "?")
  149. if strSep[2] then
  150. self.req["query"] = url.parseQuery(strSep[2])
  151. self.req["path"] = strSep[1]
  152. end
  153.  
  154. self.res = response
  155.  
  156. currRouteIteration = 0
  157.  
  158. if genRoutes[currRouteIteration] then
  159. if genRoutes[currRouteIteration]["all"] then
  160. genRoutes[currRouteIteration]["all"](self.req, self.res, server.next_fun)
  161. elseif genRoutes[currRouteIteration][string.lower(self.req["method"])] then
  162. genRoutes[currRouteIteration][string.lower(self.req["method"])](self.req, self.res, server.next_fun)
  163. end
  164. elseif routes[currRouteIteration] and routes[currRouteIteration][self.req["path"]] then
  165. if routes[currRouteIterations][self.req["path"]]["all"] then
  166. routes[currRouteIteration][self.req["path"]]["all"](self.req, self.res, server.next_fun)
  167. elseif routes[currRouteIteration][self.req["path"]][string.lower(self.req["method"])] then
  168. routers[currRouteIteration][self.req["path"]][string.lower(self.req["method"])](self.req, self.res, server.next_fun)
  169. end
  170. end
  171.  
  172. currRouteIteration = 0
  173. rednet.send(self.req["id"], textutils.serialize(self.res), self.protocol)
  174.  
  175. end
  176. end)
  177. end
  178. return { server = server }
  179.  
Advertisement
Add Comment
Please, Sign In to add comment