@aredirect and @hqm42: Thank you for your help.

After some trial and error, I came up with a solution thanks to your pointers.

1\. Use nimassets for CSS and embed the CSS style directly into the HTML.

a) Add nimassets >= 0.1.0 to your nimble file. Example (nim_heroku.nimble): 
    
    
    # Package
    
    version = "0.1.0"
    author = "sbr"
    description = "nim-heroku"
    license = "MIT"
    srcDir = "src"
    bin = @["nim_heroku"]
    skipext = @["nim"]
    
    
    # Dependencies
    
    requires "nim >= 1.0.0", "jester >= 0.4.3", "nimassets >= 0.1.0"
    
    
    Run

b) In the commandline, create the asset file. My assets are in the ./static_dir 
folder: 
    
    
    nimassets -d=static_dir -o= src/views/assetsfile.nim
    
    
    Run

c) Import the asset file, get the asset and add it directly to the HTML as a 
<style> HTML tag (src/views/general.him): 
    
    
    #? stdtmpl(subsChar = '$', metaChar = '#')
    #import assetsfile
    #
    #let css = assetsFile.getAsset("static_dir/style.css")
    #
    #proc renderMain*(): string =
    #  result = ""
    <!DOCTYPE html>
    <html>
      <head>
        <title>Nim Heroku Test</title>
        <link rel="stylesheet" 
href="https://unpkg.com/modern-css-reset/dist/reset.min.css"; />
        <style type="text/css">
          ${css}
        </style>
        <link rel="shortcut icon" href="favicon.ico">
      </head>
      <body>
        <div class="center text-center">
          <div class="[ flow ]">
            <p>Hello World</p>
            <p>This is a test</p>
          </div>
        </div>
      </body>
    </html>
    #end proc
    
    
    Run

2\. This solution doesn't work for the .ico file, as you have to use <link 
rel="shortcut icon" href="path/to/ico-file.ico"> in your HTML.

a) Use os.walkdir to find the ico file. I created a separate file for this 
functionality: src/views/static_assets.nim: 
    
    
    import os, sequtils, re
    
    var filepaths: seq[string]
    
    for kind, path in walkdir("./static_dir/"):
      filepaths.add(path)
    
    let icoFile = filter(filepaths, proc (x: string): bool =
      contains(x, re".ico"))[0]
    
    let ico* = readFile(icoFile)
    
    
    Run

b) Import ico and serve it with jester directly under the path /favicon.ico: 
    
    
    import jester, asyncdispatch, os, strutils
    import views/general, views/static_assets
    
    var settings = newSettings()
    
    settings.staticDir = "./static_dir"
    
    if existsEnv("PORT"):
      settings.port = Port(parseInt(getEnv("PORT")))
    
    
    template corsResp(code, message: untyped): untyped =
      mixin resp
      resp code, {"Access-Control-Allow-Origin": "*"}, message
    
    routes:
      get "/":
        corsResp(Http200, renderMain())
      
      get "/favicon.ico":
        corsResp(Http200, ico)
    
    runForever()
    
    
    Run

Project structure: 
    
    
    .
    ├── nim_heroku.nimble
    ├── Procfile
    ├── src
    │   ├── nim_heroku
    │   ├── nim_heroku.nim
    │   └── views
    │       ├── assetsfile.nim
    │       ├── general.nim
    │       ├── static_assets
    │       └── static_assets.nim
    ├── static_dir
    │   ├── favicon.ico
    │   └── style.css
    └── tags
    
    3 directories, 11 files
    
    
    Run

Reply via email to