diff --git a/src/prologue/core/context.nim b/src/prologue/core/context.nim index e32ce64f5..58902289f 100644 --- a/src/prologue/core/context.nim +++ b/src/prologue/core/context.nim @@ -272,7 +272,7 @@ proc multiMatch*(s: string, replacements: StringTableRef): string = pos += parseUntil(s, tok, startChar, pos) result.add tok if pos < s.len: - assert s[pos-1] == sep, "The char before '{' must be '/'" + assert s[pos - 1] == sep, "The char before '{' must be '/'" else: break inc(pos) diff --git a/src/prologue/core/middlewaresbase.nim b/src/prologue/core/middlewaresbase.nim index eb8ac2de8..49004a799 100644 --- a/src/prologue/core/middlewaresbase.nim +++ b/src/prologue/core/middlewaresbase.nim @@ -27,13 +27,13 @@ proc switch*(ctx: Context) {.async.} = let handler = findHandler(ctx) next = handler.handler - var - middlewares = handler.middlewares - ctx.middlewares = middlewares & next + ctx.middlewares = handler.middlewares + ctx.middlewares.add next ctx.first = false incSize(ctx) + if ctx.size <= ctx.middlewares.len: let next = ctx.middlewares[ctx.size - 1] await next(ctx) @@ -41,10 +41,12 @@ proc switch*(ctx: Context) {.async.} = let handler = findHandler(ctx) lastHandler = handler.handler - middlewares = handler.middlewares + ctx.localSettings = handler.settings - ctx.middlewares.add middlewares & lastHandler + ctx.middlewares.add handler.middlewares + ctx.middlewares.add lastHandler ctx.first = false + let next = ctx.middlewares[ctx.size - 1] await next(ctx) diff --git a/src/prologue/core/nativesettings.nim b/src/prologue/core/nativesettings.nim index 49caf09e3..ee4151844 100644 --- a/src/prologue/core/nativesettings.nim +++ b/src/prologue/core/nativesettings.nim @@ -57,8 +57,7 @@ proc newLocalSettings*(data: JsonNode): LocalSettings {.inline.} = proc newLocalSettings*(configPath: string): LocalSettings {.inline.} = ## Creates a new localSettings. - var data = parseFile(configPath) - result = LocalSettings(data: data) + result = LocalSettings(data: parseFile(configPath)) proc newSettings*(address = "", port = Port(8080), debug = true, reusePort = true, staticDirs: openArray[string] = ["static"], secretKey = randomString(8), @@ -85,7 +84,6 @@ proc newSettings*(configPath: string, address = "", port = Port(8080), debug = t appName = ""): Settings {.inline.} = ## Creates a new settings. # make sure reserved keys must appear in settings - var data = parseFile(configPath) result = Settings(address: address, port: port, debug: debug, reusePort: reusePort, staticDirs: @staticDirs, appName: appName, - data: data) + data: parseFile(configPath)) diff --git a/src/prologue/core/urandom.nim b/src/prologue/core/urandom.nim index 0c6c072e9..979ece55d 100644 --- a/src/prologue/core/urandom.nim +++ b/src/prologue/core/urandom.nim @@ -14,7 +14,7 @@ from strutils import strip -from nimcrypto import randomBytes +from nimcrypto/sysrand import randomBytes from ../core/types import SecretKey from ../core/encode import urlsafeBase64Encode diff --git a/src/prologue/middlewares/utils.nim b/src/prologue/middlewares/utils.nim index b1d476197..1b0c48428 100644 --- a/src/prologue/middlewares/utils.nim +++ b/src/prologue/middlewares/utils.nim @@ -23,6 +23,13 @@ from ../core/middlewaresbase import switch import ../core/request +proc testMiddleware*(): HandlerAsync = + result = proc(ctx: Context) {.async.} = + logging.info "debug->begin" + await switch(ctx) + logging.info "debug->end" + + proc loggingMiddleware*(appName = "Starlight"): HandlerAsync = result = proc(ctx: Context) {.async.} = logging.info "loggingMiddleware->begin"