Skip to content

Instantly share code, notes, and snippets.

@0atman
Last active February 27, 2023 19:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0atman/827c7e23f83cb5b56528faf52192037f to your computer and use it in GitHub Desktop.
Save 0atman/827c7e23f83cb5b56528faf52192037f to your computer and use it in GitHub Desktop.
Full-stack, pure Nim. Backend and JS using the same staticly-typed model.
import htmlgen
import jester
import data
routes:
get "/":
resp(
html(
head(
script(src="/js/js.js")),
body(
ul(
li(db))))
get "/js/js.js":
const result = staticExec "nim -d:release js jsclient.nim"
const clientJS = staticRead "nimcache/jsclient.js"
resp clientJS
const db* = "database"
import data
when isMainModule:
echo db
app:
nim c app.nim
clean:
rm app
run:
./app
@0atman
Copy link
Author

0atman commented Apr 12, 2020

Both the string on the page and in the console log come from the same source - db.nim.

Screenshot_2020-04-12_15-52-58

@0atman
Copy link
Author

0atman commented Apr 12, 2020

To run this example, nimble install jester before you make app run.

@0atman
Copy link
Author

0atman commented Apr 12, 2020

I used this layout:

 $ tree
.
├── app
├── app.nim
├── data.nim
├── js.nim
├── Makefile
└── public
    └── js
        └── js.js

@0atman
Copy link
Author

0atman commented Apr 20, 2020

Updated to statically build the js code into the app using compiletime magic. (ie, staticExec and staticRead)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment