Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local urlCodec = require("/lib/url")
- local str = require("/lib/string")
- server = {
- onClose = function() return true end,
- next_fun = function (self)
- currRouteIteration = currRouteIteration + 1
- if currRouteIteration > routesCount then
- goto finish_next1
- elseif genRoutes[currRouteIteration] then
- if genRoutes[currRouteIteration]["all"] then
- genRoutes[currRouteIteration]["all"](self.req, self.res, server.next_fun)
- elseif genRoutes[currRouteIteration][string.lower(self.req["method"])] then
- genRoutes[currRouteIteration][string.lower(self.req["method"])](self.req, self.res, server.next_fun)
- end
- elseif routes[currRouteIteration] then
- if routes[currRouteIteration][self.req["path"]]then
- if routes[currRouteIterations][self.req["path"]]["all"] then
- routes[currRouteIteration][self.req["path"]]["all"](self.req, self.res, server.next_fun)
- elseif routes[currRouteIteration][self.req["path"]][string.lower(self.req["method"])] then
- routers[currRouteIteration][self.req["path"]][string.lower(self.req["method"])](self.req, self.res, server.next_fun)
- end
- end
- end
- ::finish_next1::
- end,
- res = {},
- req = {}
- }
- routesCount = 0
- genRoutes = {
- }
- routes = {
- }
- currRouteIteration = 0
- request = {
- ["method"] = "",
- ["path"] = "",
- ["body"] = {},
- ["headers"] = {
- ["User-Agent"] = ""
- }
- }
- response = {
- ["status"] = 200,
- ["body"] = {},
- ["headers"] = {
- ["Content-Type"] = "application/json",
- }
- }
- function handleExit()
- while true do
- event = os.pullEventRaw("terminate")
- if event == "terminate" then
- rednet.close()
- print("stopping server")
- break
- end
- end
- end
- function server:create(protocol, routerLoc)
- newObj = {}
- newObj.protocol = protocol
- newObj.router = routerLoc
- setmetatable(newObj, self)
- self.__index = self
- return newObj
- end
- function server:use(callback, path)
- routesCount = routesCount + 1
- if path then
- routes[routesCount] = {["path"] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}}
- routes[routesCount][path]["all"] = callback
- else
- genRoutes[routesCount] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}
- genRoutes[routesCount]["all"] = callback
- end
- end
- function server:get(callback, path)
- routesCount = routesCount + 1
- if path then
- routes[routesCount] = {["path"] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}}
- routes[routesCount][path]["get"] = callback
- else
- genRoutes[routesCount] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}
- genRoutes[routesCount]["get"] = callback
- end
- end
- function server:post(callback, path)
- routesCount = routesCount + 1
- if path then
- routes[routesCount] = {["path"] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}}
- routes[routesCount][path]["post"] = callback
- else
- genRoutes[routesCount] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}
- genRoutes[routesCount]["post"] = callback
- end
- end
- function server:put(callback, path)
- routesCount = routesCount + 1
- if path then
- routes[routesCount] = {["path"] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}}
- routes[routesCount][path]["put"] = callback
- else
- genRoutes[routesCount] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}
- genRoutes[routesCount]["put"] = callback
- end
- end
- function server:delete(callback, path)
- routesCount = routesCount + 1
- if path then
- routes[routesCount] = {["path"] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}}
- routes[routesCount][path]["delete"] = callback
- else
- genRoutes[routesCount] = { ["all"] = nil, ["get"] = nil, ["put"] = nil, ["delete"] = nil}
- genRoutes[routesCount]["delete"] = callback
- end
- end
- function server:start()
- parallel.waitForAny(handleExit, function()
- rednet.open(self.router)
- print("Running server on '"..self.protocol.."'")
- while true do
- id, msg = rednet.receive(self.protocol)
- self.req = textutils.unserialize(msg)
- self.req["id"] = id
- local strSep = str.split(self.req["path"], "?")
- if strSep[2] then
- self.req["query"] = url.parseQuery(strSep[2])
- self.req["path"] = strSep[1]
- end
- self.res = response
- currRouteIteration = 0
- if genRoutes[currRouteIteration] then
- if genRoutes[currRouteIteration]["all"] then
- genRoutes[currRouteIteration]["all"](self.req, self.res, server.next_fun)
- elseif genRoutes[currRouteIteration][string.lower(self.req["method"])] then
- genRoutes[currRouteIteration][string.lower(self.req["method"])](self.req, self.res, server.next_fun)
- end
- elseif routes[currRouteIteration] and routes[currRouteIteration][self.req["path"]] then
- if routes[currRouteIterations][self.req["path"]]["all"] then
- routes[currRouteIteration][self.req["path"]]["all"](self.req, self.res, server.next_fun)
- elseif routes[currRouteIteration][self.req["path"]][string.lower(self.req["method"])] then
- routers[currRouteIteration][self.req["path"]][string.lower(self.req["method"])](self.req, self.res, server.next_fun)
- end
- end
- currRouteIteration = 0
- rednet.send(self.req["id"], textutils.serialize(self.res), self.protocol)
- end
- end)
- end
- return { server = server }
Advertisement
Add Comment
Please, Sign In to add comment