mofuw icon indicating copy to clipboard operation
mofuw copied to clipboard

Error: type mismatch

Open s0kil opened this issue 5 years ago • 4 comments

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?

s0kil avatar Sep 24 '18 09:09 s0kil

var theme is not thread-safe. using {.threadvar.}.

2vg avatar Sep 24 '18 09:09 2vg

I don't understand, what you mean by that.

s0kil avatar Sep 24 '18 10:09 s0kil

The theme variable is shared by all server threads.

2vg avatar Sep 24 '18 10:09 2vg

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()

s0kil avatar Oct 27 '18 18:10 s0kil