import asyncdispatch, db_sqlite, jester, moustachu, os, shorturl, strutils, tables, times, typetraits const baseTemplate*: string = staticRead "./templates/layout.mustache" errorTemplate*: string = staticRead "./templates/error.mustache" let db = open("data/twtxt.db", nil, nil, nil) try: db.exec sql"""create table if not exists users ( id INTEGER PRIMARY KEY, username TEXT UNIQUE, url TEXT UNIQUE );""" db.exec sql"""create table if not exists tweets ( id INTEGER PRIMARY KEY, username TEXT, time REAL, tweet TEXT, UNIQUE (username, time, tweet) );""" except: quit getCurrentExceptionMsg() template renderMustache*(title: string, templ: string, ctx: Context): expr = var layoutCtx = moustachu.newContext() layoutCtx["title"] = title layoutCtx["body"] = render(templ, ctx) resp render(baseTemplate, layoutCtx) template fail*(): expr = var ctx = newContext() ctx["exception"] = getCurrentExceptionMsg() var layoutCtx = moustachu.newContext() layoutCtx["title"] = "fail" layoutCtx["body"] = render(errorTemplate, ctx) halt render(baseTemplate, layoutCtx) settings: port = getEnv("PORT").parseInt().Port bindAddr = "0.0.0.0" routes: get "/": try: raise newException(ValueError, "yolo") except: fail() runForever()