Skip to content

Commit

Permalink
improve server answer speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Oct 5, 2024
1 parent a39b5a3 commit 700e714
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 13 deletions.
2 changes: 1 addition & 1 deletion happyx.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

description = "Macro-oriented asynchronous web-framework written with ♥"
author = "HapticX"
version = "4.6.0"
version = "4.6.1"
license = "MIT"
srcDir = "src"
installExt = @["nim"]
Expand Down
2 changes: 1 addition & 1 deletion src/happyx/core/constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const
# Framework version
HpxMajor* = 4
HpxMinor* = 6
HpxPatch* = 0
HpxPatch* = 1
HpxVersion* = $HpxMajor & "." & $HpxMinor & "." & $HpxPatch


Expand Down
4 changes: 4 additions & 0 deletions src/happyx/ssr/cors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type
allowMethods*: string


const corsRegistered* = CacheCounter"HappyXCORSRegistered"



when not defined(js) and not (exportJvm or exportPython or defined(napibuild)):
var currentCORS {. compileTime .} = CORSObj()
Expand Down Expand Up @@ -81,6 +84,7 @@ when not defined(js) and not (exportJvm or exportPython or defined(napibuild)):
else:
result.add quote do:
`headers`["Access-Control-Allow-Origin"] = `allowOrigins`
inc corsRegistered

macro regCORS*(body: untyped): untyped =
## Register CORS
Expand Down
115 changes: 104 additions & 11 deletions src/happyx/ssr/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,105 @@ macro `.`*(obj: JsonNode, field: untyped): JsonNode =
newCall("[]", obj, newLit($field.toStrLit))


const defaultHeaders = "Content-Type: text/plain;charset=utf-8"


template answer*(
req: Request,
message: string | int | float | bool | char,
code: HttpCode = Http200,
headers: HttpHeaders = newHttpHeaders([
code: HttpCode = Http200
) =
## Answers to the request
##
## ⚠ `Low-level API` ⚠
##
## Arguments:
## `req: Request`: An instance of the Request type, representing the request that we are responding to.
## `message: string`: The message that we want to include in the response body.
## `code: HttpCode = Http200`: The HTTP status code that we want to send in the response.
## This argument is optional, with a default value of Http200 (OK).
##
## Use this example instead
##
## .. code-block::nim
## get "/":
## return "Hello, world!"
##
const useHeaders = declared(outHeaders) or declared(outCookies) or corsRegistered.value > 0
when useHeaders:
var h = newHttpHeaders([
("Content-Type", "text/plain; charset=utf-8")
]),
])
when corsRegistered.value > 0:
when exportJvm or exportPython or defined(napibuild):
when enableHttpBeast or enableHttpx:
addCORSHeaders(req.ip, h)
else:
addCORSHeaders(req.hostname, h)
else:
h.addCORSHeaders()
when declared(outHeaders):
for key, val in outHeaders.pairs():
h[key] = val
# HTTPX
when enableHttpx:
when useHeaders:
var headersArr = ""
for key, value in h.pairs():
headersArr &= key & ':' & value & "\r\n"
when declared(outCookies):
for cookie in outCookies:
headersArr &= cookie & "\r\n"
if headersArr.len > 0:
headersArr.delete(headersArr.len-2..headersArr.len-1)
when declared(statusCode):
when statusCode is int:
req.send(statusCode.HttpCode, $message, when useHeaders: headersArr else: defaultHeaders)
else:
req.send(code, $message, when useHeaders: headersArr else: defaultHeaders)
else:
req.send(code, $message, when useHeaders: headersArr else: defaultHeaders)
# HTTP BEAST
elif enableHttpBeast:
when useHeaders:
var headersArr = ""
for key, value in h.pairs():
headersArr &= key & ':' & value & "\r\n"
when declared(outCookies):
for cookie in outCookies:
headersArr &= cookie & "\r\n"
if headersArr.len > 0:
headersArr.delete(headersArr.len-2..headersArr.len-1)
when declared(statusCode):
when statusCode is int:
req.send(statusCode.HttpCode, $message, when useHeaders: headersArr else: defaultHeaders)
else:
req.send(code, $message, when useHeaders: headersArr else: defaultHeaders)
else:
req.send(code, $message, when useHeaders: headersArr else: defaultHeaders)
# ASYNC HTTP SERVER / MICRO ASYNC HTTP SERVER
else:
when useHeaders:
when declared(outCookies):
for cookie in outCookies:
let data = cookie.split(":", 1)
h.add("Set-Cookie", data[1].strip())
else:
let h = newHttpHeaders([("Content-Type", "text/plain; charset=utf-8")])
when declared(statusCode):
when statusCode is int:
await req.respond(statusCode.HttpCode, $message, h)
else:
await req.respond(code, $message, h)
else:
await req.respond(code, $message, h)


template answer*(
req: Request,
message: string | int | float | bool | char,
code: HttpCode = Http200,
headers: HttpHeaders,
contentLength: Option[int] = int.none
) =
## Answers to the request
Expand All @@ -272,13 +364,14 @@ template answer*(
## return "Hello, world!"
##
var h = headers
when exportJvm or exportPython or defined(napibuild):
when enableHttpBeast or enableHttpx:
addCORSHeaders(req.ip, h)
when corsRegistered.value > 0:
when exportJvm or exportPython or defined(napibuild):
when enableHttpBeast or enableHttpx:
addCORSHeaders(req.ip, h)
else:
addCORSHeaders(req.hostname, h)
else:
addCORSHeaders(req.hostname, h)
else:
h.addCORSHeaders()
h.addCORSHeaders()
when declared(outHeaders):
for key, val in outHeaders.pairs():
h[key] = val
Expand All @@ -291,7 +384,7 @@ template answer*(
for cookie in outCookies:
headersArr &= cookie & "\r\n"
if headersArr.len > 0:
headersArr = headersArr[0..^3]
headersArr.delete(headersArr.len-2..headersArr.len-1)
if contentLength.isSome:
# useful for file answers
when declared(statusCode):
Expand All @@ -318,7 +411,7 @@ template answer*(
for cookie in outCookies:
headersArr &= cookie & "\r\n"
if headersArr.len > 0:
headersArr = headersArr[0..^3]
headersArr.delete(headersArr.len-2..headersArr.len-1)
when declared(statusCode):
when statusCode is int:
req.send(statusCode.HttpCode, $message, headersArr)
Expand Down

0 comments on commit 700e714

Please sign in to comment.