90 lines
2.7 KiB
Nim
90 lines
2.7 KiB
Nim
## Compile with: -d:release --mm:orc --threads:on -d:threadsafe -d:useMalloc
|
|
##
|
|
## To modify the thread count, use -d:threadCount
|
|
##
|
|
## To test with 100 dummy middleware, use -d:dummyMiddlewareCount=100
|
|
## To test with 100 dummy routes, use -d:dummyRouteCount=100
|
|
##
|
|
## Dummy middleware and routes can be used to test what real-world performance might be with more realistic conditions.
|
|
|
|
import std/[asyncdispatch, strformat, strutils, tables, json, options, os, mimetypes]
|
|
import ../src/kommandkit
|
|
import ../src/kommandkit/[httpserver, middleware]
|
|
import microasynchttpserver
|
|
|
|
const threadCount {.intdefine.} = 0
|
|
const dummyMiddlewareCount {.intdefine.} = 0
|
|
const dummyRouteCount {.intdefine.} = 0
|
|
|
|
echo "Thread count: " & $threadCount
|
|
|
|
when isMainModule:
|
|
const port = 8080
|
|
const host = "127.0.0.1"
|
|
|
|
let kk = newKommandKit()
|
|
|
|
kk.createKommandKitThreads(kk, threadCount):
|
|
var http = kk.newHttpServer(Port(port), host)
|
|
|
|
http.addHandler(createReverseProxyIpMiddleware())
|
|
|
|
http.addHandler(createBodyHandlerMiddleware(BodyHandlerMiddlewareOptions(
|
|
maxSize: 4 * 1024,
|
|
parseUrlEncodedAsJson: false,
|
|
onlyAcceptJsonObject: true,
|
|
)))
|
|
|
|
for i in 0 ..< dummyMiddlewareCount:
|
|
http.handler(ctx):
|
|
return nextResponse()
|
|
|
|
for i in 0 ..< dummyRouteCount:
|
|
http.route(ctx, HttpGet, "/dummy"):
|
|
return await ctx.response.endWith(Http200, "dummy")
|
|
|
|
http.route(ctx, "/home"):
|
|
return await ctx.endWithFile("/tmp/index.html", "text/html;charset=utf-8")
|
|
|
|
http.route(ctx, HttpGet, "/"):
|
|
return await ctx.response.endWith(Http200, "Hello world")
|
|
|
|
http.route(ctx, "/form"):
|
|
var formDataStr = ""
|
|
let formDataRes = ctx.formData()
|
|
if formDataRes.isSome:
|
|
let formData = formDataRes.unsafeGet()
|
|
|
|
formDataStr = $formData
|
|
|
|
return await ctx.response.endWith(Http200,
|
|
fmt"""
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Dummy Page</title>
|
|
</head>
|
|
<body>
|
|
<form action="" method="POST">
|
|
<input type="text" name="name" placeholder="Name" />
|
|
<input type="submit" />
|
|
</form>
|
|
<br /> <br />
|
|
<pre>{formDataStr}</pre>
|
|
</body>
|
|
</html>
|
|
""".strip())
|
|
|
|
http.addHandler(createStaticFilesMiddleware(StaticFilesMiddlewareOptions(
|
|
filesRoot: getCurrentDir() / "tests" / "static",
|
|
mountRoot: "/static",
|
|
excludeHiddenFiles: true,
|
|
supportRanges: true,
|
|
mimeDb: newMimetypes(),
|
|
)))
|
|
|
|
http.run()
|
|
|
|
while true:
|
|
sleep(10_000)
|
|
GC_runOrc()
|