mofuw
mofuw copied to clipboard
Error: type mismatch
I'm getting an error when trying to read file contents into mofuwResp
:
... nim_cms.nim(23, 12) Error: type mismatch: got <port: int literal(8000), handler: proc (ctx: MofuwCtx): Future[system.void]{.locks: <unknown>.}>
... but expected one of:
... proc newServeCtx(servername = "mofuw"; port: int;
... handler, hookrequest, hookresponse: MofuwHandler = nil;
... readBufferSize, writeBufferSize = 4096; maxBodySize = 1048576 * 5;
... timeout = 30 * 1000; poolsize = 128; isSSL = false): ServeCtx
... expression: newServeCtx(port = 8000, handler = mofuwHandler)
nim_cms.nim
import os, asyncfile
import mofuw, templates
import "./theme" as Theme
var theme = Theme(
defaultName: "default",
extension: "html",
directory: parentDir(currentSourcePath()) / "views"
)
theme.init()
routes:
serve("public")
get "/":
var data = await theme.defaultTheme.readAll()
mofuwResp(HTTP200, "text/html", data)
# mofuwOK("Hello")
echo "Starting CMS Server"
newServeCtx(
port = 8000,
handler = mofuwHandler
).serve()
theme.nim
import os, asyncfile
type Theme* = ref object
defaultName*, extension*, directory*: string
defaultTheme*: AsyncFile
proc init*(this: Theme) =
this.defaultTheme = openAsync(this.directory / (this.defaultName & "." & this.extension))
Is there something I'm doing wrong?
var theme
is not thread-safe.
using {.threadvar.}
.
I don't understand, what you mean by that.
The theme variable is shared by all server threads.
So putting the theme variable inside of /
route, allows the program to compile.
What if I need to share the theme
variable with multiple routes?
import os, asyncfile
import mofuw
import "./theme" as Theme
routes:
serve("public")
get "/":
var theme = Theme(
defaultName: "default",
extension: "html",
directory: parentDir(currentSourcePath()) / "views"
)
theme.init()
var data = await theme.defaultTheme.readAll()
mofuwResp(HTTP200, "text/html", data)
# mofuwOK("Hello")
echo "Starting CMS Server"
newServeCtx(
port = 8000,
handler = mofuwHandler
).serve()