Skip to content
This repository has been archived by the owner on Nov 20, 2019. It is now read-only.

Error: type mismatch #63

Open
s0kil opened this issue Sep 24, 2018 · 4 comments
Open

Error: type mismatch #63

s0kil opened this issue Sep 24, 2018 · 4 comments

Comments

@s0kil
Copy link
Contributor

s0kil commented Sep 24, 2018

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?

@2vg
Copy link
Owner

2vg commented Sep 24, 2018

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

@s0kil
Copy link
Contributor Author

s0kil commented Sep 24, 2018

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

@2vg
Copy link
Owner

2vg commented Sep 24, 2018

The theme variable is shared by all server threads.

@s0kil
Copy link
Contributor Author

s0kil commented Oct 27, 2018

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants