From f6b40d9c6356b0c4388c6267b883e72dbb639345 Mon Sep 17 00:00:00 2001 From: Mildred Ki'Lya Date: Fri, 18 Aug 2023 02:07:20 +0200 Subject: [PATCH] Allow custom socket listener (#223) --- prologue.nimble | 2 +- src/prologue/core/application.nim | 8 +++++++- src/prologue/core/beast/server.nim | 2 +- src/prologue/core/nativesettings.nim | 13 ++++++++----- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/prologue.nimble b/prologue.nimble index 31884b6f4..49f21b0fc 100644 --- a/prologue.nimble +++ b/prologue.nimble @@ -12,7 +12,7 @@ requires "nim >= 1.6.0" requires "regex >= 0.20.0" requires "nimcrypto >= 0.5.4" requires "cookiejar >= 0.2.0" -requires "httpx >= 0.3.4" +requires "httpx >= 0.3.7" requires "logue >= 0.2.0" diff --git a/src/prologue/core/application.nim b/src/prologue/core/application.nim index 016ab46ee..d3f823d43 100644 --- a/src/prologue/core/application.nim +++ b/src/prologue/core/application.nim @@ -555,6 +555,8 @@ proc runAsync*(app: Prologue) {.inline, async.} = when isMainModule: + import net + proc hello(ctx: Context) {.async.} = logging.debug "hello" resp "

Hello, Prologue!

" @@ -579,7 +581,11 @@ when isMainModule: logging.debug "doLogin" resp redirect("/hello/Nim") - let settings = newSettings(appName = "Prologue", debug = true) + let socket = newSocket() + socket.bindAddr(Port(8080), "0.0.0.0") + socket.setSockOpt(OptReuseAddr, true) + socket.listen() + let settings = newSettings(appName = "Prologue", debug = true, listener = socket) var app = newApp(settings = settings) app.addRoute("/", home, HttpGet) diff --git a/src/prologue/core/beast/server.nim b/src/prologue/core/beast/server.nim index 90ea83a5d..21ecf2f24 100644 --- a/src/prologue/core/beast/server.nim +++ b/src/prologue/core/beast/server.nim @@ -27,7 +27,7 @@ proc execStartupEvent*(app: Prologue) = proc getSettings(app: Prologue): httpx.Settings = result = httpx.initSettings(app.gScope.settings.port, app.gScope.settings.address, app.gScope.settings["prologue"].getOrDefault("numThreads").getInt(0), - app.startupClosure) + app.startupClosure, app.gScope.settings.listener) proc serve*(app: Prologue, callback: proc (request: NativeRequest): Future[void] {.closure, gcsafe.}, diff --git a/src/prologue/core/nativesettings.nim b/src/prologue/core/nativesettings.nim index 0054f5f33..8db3af32d 100644 --- a/src/prologue/core/nativesettings.nim +++ b/src/prologue/core/nativesettings.nim @@ -15,6 +15,7 @@ import std/[mimetypes, json, tables, strtabs] from std/nativesockets import Port +from std/net import Socket from ./types import SecretKey, EmptySecretKeyError, len from ./urandom import randomString @@ -24,6 +25,7 @@ type Settings* = ref object ## Global settings for all handlers. address*: string ## The address of socket. port*: Port ## The port of socket. + listener*: Socket ## listening socket to use (nil to auto create) debug*: bool ## Debug mode(true is yes). reusePort*: bool ## Use socket port in multiple times. bufSize*: int ## Buffer size of sending static files. @@ -58,23 +60,24 @@ func newSettings*( secretKey = randomString(8), appName = "", bufSize = 40960, - data: JsonNode = nil + data: JsonNode = nil, + listener: Socket = nil ): Settings = ## Creates a new `Settings`. if secretKey.len == 0: raise newException(EmptySecretKeyError, "Secret key can't be empty!") if data == nil: - result = Settings(address: address, port: port, debug: debug, - reusePort: reusePort, bufSize: bufSize, + result = Settings(address: address, port: port, listener: listener, + debug: debug, reusePort: reusePort, bufSize: bufSize, data: %* {"prologue": {"secretKey": secretKey, "appName": appName}}) else: var data = data data["prologue"] = %* {"secretKey": secretKey, "appName": appName} - result = Settings(address: address, port: port, debug: debug, - reusePort: reusePort, bufSize: bufSize, + result = Settings(address: address, port: port, listener: listener, + debug: debug, reusePort: reusePort, bufSize: bufSize, data: data) func newSettingsFromJsonNode*(settings: var Settings, data: JsonNode) {.inline.} =