Skip to content

Commit

Permalink
Implement runAsync (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
mildred authored Apr 18, 2023
1 parent 3d2a9f7 commit 9b33f76
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
26 changes: 21 additions & 5 deletions src/prologue/core/application.nim
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,7 @@ proc handleRequest*(app: Prologue, nativeRequest: NativeRequest, ctxTyp: typedes
extend(ctx)
result = handleContext(app, ctx)

proc run*(app: Prologue, ctxTyp: typedesc[Context]) =
## Starts an Application.

proc prepareRun(app: Prologue) =
# assert ctx != nil, "The memory of `ctx` must be allocated!"
app.gScope.router.compress()

Expand All @@ -529,14 +527,32 @@ proc run*(app: Prologue, ctxTyp: typedesc[Context]) =
else:
logging.debug(fmt"Prologue is serving at http://{app.appAddress}:{app.appPort} {app.appName}")

proc run*(app: Prologue, ctxTyp: typedesc[Context]) =
## Starts an Application.

prepareRun(app)

app.serve(proc (nativeRequest: NativeRequest): Future[void] {.gcsafe.} =
result = handleRequest(app, nativeRequest, ctxTyp))
proc handler(nativeRequest: NativeRequest): Future[void] {.gcsafe.} =
result = handleRequest(app, nativeRequest, ctxTyp)

app.serve(handler)

proc run*(app: Prologue) {.inline.} =
app.run(Context)

proc runAsync*(app: Prologue, ctxTyp: typedesc[Context]) {.async.} =
## Starts an Application.

prepareRun(app)

proc handler(nativeRequest: NativeRequest): Future[void] {.gcsafe.} =
result = handleRequest(app, nativeRequest, ctxTyp)

await app.serveAsync(handler)

proc runAsync*(app: Prologue) {.inline, async.} =
await app.runAsync(Context)


when isMainModule:
proc hello(ctx: Context) {.async.} =
Expand Down
16 changes: 12 additions & 4 deletions src/prologue/core/beast/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,22 @@ proc execStartupEvent*(app: Prologue) =

app.startupClosure = doStartup

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)

proc serve*(app: Prologue,
callback: proc (request: NativeRequest): Future[void] {.closure, gcsafe.},
) {.inline.} =
## Serves a new web application.
run(callback, httpx.initSettings(app.gScope.settings.port, app.gScope.settings.address,
app.gScope.settings["prologue"].getOrDefault("numThreads").getInt(0),
app.startupClosure)
)
run(callback, getSettings(app))

proc serveAsync*(app: Prologue,
callback: proc (request: NativeRequest): Future[void] {.closure, gcsafe.},
) {.inline, async.} =
## Serves a new web application.
await runAsync(callback, getSettings(app))

func newPrologue*(settings: Settings, ctxSettings: CtxSettings, router: Router,
reversedRouter: ReversedRouter, reRouter: ReRouter, middlewares: openArray[HandlerAsync],
Expand Down
8 changes: 7 additions & 1 deletion src/prologue/core/naive/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ proc execStartupEvent*(app: Prologue) =
for event in app.startup:
execEvent(event)

proc serveAsync*(app: Prologue,
callback: proc (request: NativeRequest): Future[void] {.closure, gcsafe.},
) {.inline, async.} =
## Serves a new web application.
await app.server.serve(app.gScope.settings.port, callback, app.gScope.settings.address)

proc serve*(app: Prologue,
callback: proc (request: NativeRequest): Future[void] {.closure, gcsafe.},
) {.inline.} =
## Serves a new web application.
waitFor app.server.serve(app.gScope.settings.port, callback, app.gScope.settings.address)
waitFor serveAsync(app, callback)

func newPrologueServer(reuseAddr = true, reusePort = false,
maxBody = 8388608): Server {.inline.} =
Expand Down

0 comments on commit 9b33f76

Please sign in to comment.