Skip to content

Commit

Permalink
Creates a nimble package hierarchy.
Browse files Browse the repository at this point in the history
Moves main.nim to src.
  • Loading branch information
UNIcodeX committed Nov 12, 2020
1 parent d11713e commit 808c8b5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 62 deletions.
38 changes: 38 additions & 0 deletions PrologueWebApp.nimble
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Package

version = "0.1.0"
author = "Jared Fields"
description = "A Prologue web application template"
license = "BSD-3-Clause"
srcDir = "src"
bin = @["main"]


# Dependencies

requires "nim >= 1.4.0",
"cligen == 1.2.2",
"cookiejar == 0.3.0",
"httpx == 0.2.4",
"ioselectors == 0.1.8",
"logue == 0.2.4",
"nimCommandParser == 0.1.1",
"nimcrypto == 0.5.4",
"nwt == 0.1.7",
"prologue == 0.4.2",
"regex == 0.17.1",
"segmentation == 0.1.0",
"unicodedb == 0.9.0",
"unicodeplus == 0.8.0",
"wepoll == 0.1.0"

task runr, "Build and run release version.":
exec "nim c -r -d:danger -d:release --nimblePath:nimbledeps/pkgs src/main"

task runrOrc, "Build and run release version (--gc:orc).":
exec "nim c -r -d:danger -d:release --nimblePath:nimbledeps/pkgs --gc:orc src/main"

task buildDist, "Build and place in ./dist":
exec "nim c -d:danger -d:release --nimblePath:nimbledeps/pkgs --gc:orc -o:dist/main src/main"
exec "cp -r static dist/"
exec "cp -r templates dist/"
62 changes: 0 additions & 62 deletions main.nim

This file was deleted.

43 changes: 43 additions & 0 deletions src/main.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import prologue
import prologue/middlewares/staticfile
import nwt

## To just set up threadvar and initialize per thread.
var templates {.threadvar.} : Nwt
templates = newNwt("templates/*.html")


let settings = newSettings(
address="0.0.0.0",
debug=false,
port=Port(8000),
)

var app = newApp(
settings=settings,
middlewares = @[staticFileMiddleware(["/static"])]
)

proc initTemplates() =
if templates.isNil:
templates = newNwt("templates/*.html")

proc index(ctx: Context) {.async.} =
initTemplates()
resp templates.renderTemplate("index.html")

proc explicitHTML(ctx: Context) {.async.} =
resp htmlResponse("<h2>Welcome to Prologue</h2>")

proc implicitHTML(ctx: Context) {.async.} =
resp "<html><body><h2>This is an implicit HTML response</h2></body></html>"

proc jsonResp(ctx: Context) {.async.} =
resp jsonResponse(%*{"message": "test"})

app.addRoute("/", index)
app.addRoute("/explicitHTML", explicitHTML)
app.addRoute("/implicitHTML", implicitHTML)
app.addRoute("/json", jsonResp)

app.run()

0 comments on commit 808c8b5

Please sign in to comment.