import asyncdispatch, db_sqlite, jester, moustachu, os, shorturl, strutils, tables, times const baseTemplate*: string = staticRead "./templates/layout.mustache" indexTemplate*: string = staticRead "./templates/index.mustache" errorTemplate*: string = staticRead "./templates/error.mustache" testTemplate*: string = """

hi!

""" let db = open("data/quotes.db", nil, nil, nil) try: db.exec(sql"""create table if not exists quotes ( id INTEGER PRIMARY KEY, channel TEXT, adder TEXT, nick TEXT, message TEXT, time REAL, deleted INTEGER DEFAULT 0)""") except: echo getCurrentExceptionMsg() raise 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() renderMustache("error", errorTemplate, ctx) settings: port = 5000.Port bindAddr = "0.0.0.0" routes: get "/test": renderMustache("test", testTemplate, newContext()) get "/": let quotes = db.getAllRows(sql"SELECT * FROM quotes ORDER BY time desc LIMIT 20") var ctx = newContext() quoteSeq = newSeq[Context]() for quote in items(quotes): var c = newContext() c["id"] = quote[0].parseInt().encodeURLSimple() c["channel"] = quote[1] c["channelsafe"] = quote[1].replace("#", "hashtag-") c["nick"] = quote[2] c["adder"] = quote[3] c["message"] = quote[4] c["time"] = parseInt(split(quote[5], '.')[0]) quoteSeq.add c ctx["quotes"] = quoteSeq renderMustache("most recent quotes", indexTemplate, ctx) get "/quotes/@id": var qid: int = -1 try: qid = (@"id").decodeURLSimple() except: fail() let quote = db.getRow(sql"SELECT * FROM quotes WHERE id = ?", qid) echo quote resp $quote runForever()