2016-02-06 15:54:55 +00:00
|
|
|
import asyncdispatch, db_sqlite, jester, moustachu, os, shorturl,
|
2016-02-06 16:41:23 +00:00
|
|
|
strutils, tables, times, twtxt, typetraits
|
2016-02-06 15:54:55 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
baseTemplate*: string = staticRead "./templates/layout.mustache"
|
|
|
|
errorTemplate*: string = staticRead "./templates/error.mustache"
|
2016-02-06 16:27:31 +00:00
|
|
|
indexTemplate*: string = staticRead "./templates/index.mustache"
|
|
|
|
usersTemplate*: string = staticRead "./templates/users.mustache"
|
2016-02-06 15:54:55 +00:00
|
|
|
|
|
|
|
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:
|
2016-02-06 15:56:27 +00:00
|
|
|
port = getEnv("PORT").parseInt().Port
|
|
|
|
bindAddr = "0.0.0.0"
|
2016-02-06 15:54:55 +00:00
|
|
|
|
|
|
|
routes:
|
|
|
|
get "/":
|
2016-02-06 16:27:31 +00:00
|
|
|
renderMustache("home", indexTemplate, newContext())
|
|
|
|
|
|
|
|
get "/users":
|
|
|
|
var
|
|
|
|
users = db.getAllRows(sql"select username, url from users")
|
|
|
|
ctx = newContext()
|
|
|
|
userList = newSeq[Context]()
|
|
|
|
|
|
|
|
for user in users.items():
|
|
|
|
var c = newContext()
|
|
|
|
c["username"] = user[0]
|
|
|
|
c["url"] = user[1]
|
|
|
|
|
|
|
|
userList.add c
|
|
|
|
|
|
|
|
ctx["users"] = userList
|
|
|
|
|
|
|
|
renderMustache "users", usersTemplate, ctx
|
2016-02-06 15:54:55 +00:00
|
|
|
|
|
|
|
runForever()
|