change how to make a route

This commit is contained in:
William Bell committed 2023-11-11 00:30:07 +00:00
1 parent 35786a87c9
commit 32d88ddd63
4 files changed
+18 -17

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
{
"name": "http.ar",
"version": "1.0.5"
"version": "1.1.0"
}
+3 -4
View File
@@ -19,7 +19,7 @@ let handle(routes, conn, onErr) = do
request.on = {}
request.on.headersSent = []
request.on.end = []
let bufferSize = 1024*1024*10
let bufferSize = 10485760
request.headersSent = false
let response = {code:200, headers:__initHeaders()}
@@ -31,8 +31,7 @@ let handle(routes, conn, onErr) = do
let response_data = buildResponse.build(response.headers, response.code, "")
request.headersSent = true
conn.write(buffer().from(response_data))
for (i from 0 to request.on.headersSent.length) do
request.on.headersSent[i]()
for (i from 0 to request.on.headersSent.length)request.on.headersSent[i]()
response.sendFile(f) = do
let path = null
@@ -134,7 +133,7 @@ let handle(routes, conn, onErr) = do
trying += 1
if (trying < toTry.length) do
let route = toTry[trying]
if ("params" in route) do
if (route == true || "params" in route) do
request.params = route.params
else do
request.params = {}
+12 -5
View File
@@ -37,9 +37,16 @@ let server() = do
class.close() = Socket.close()
class.route(path, methods, callback) = do
if (methods == null) do
methods = ["GET"]
routes.append({path: path, callback: callback, methods: methods, type: "route"})
class.route(path) = do
let class = {}
class.get(callback) = routes.append({path: path, callback: callback, methods: ["GET"], type: "route"})
class.post(callback) = routes.append({path: path, callback: callback, methods: ["POST"], type: "route"})
class.put(callback) = routes.append({path: path, callback: callback, methods: ["PUT"], type: "route"})
class.delete(callback) = routes.append({path: path, callback: callback, methods: ["DELETE"], type: "route"})
class.all(callback) = routes.append({path: path, callback: callback, methods: true, type: "route"})
class.get(path, callback) = class.route(path).get(callback)
class.post(path, callback) = class.route(path).post(callback)
class.put(path, callback) = class.route(path).put(callback)
class.all(path, callback) = class.route(path).all(callback)
class.use(callback) = routes.append({callback: callback, type: "middleware"})
return class
return class
+2 -7
View File
@@ -1,10 +1,5 @@
import "handleRequest" as handleRequest
let listener(Socket, routes, onErr) = do
while (not Socket.isClosed()) do
try do
let conn = Socket.accept()
t = thread(()=handleRequest.handle(routes, conn, onErr))
t.start()
catch (e) do
if (onErr) onErr(e)
while (not Socket.isClosed()) try thread(()=handleRequest.handle(routes, Socket.accept(), onErr)).start()
catch (e) if (onErr) onErr(e)