quotesite/src/quotesite.nim

136 lines
3.4 KiB
Nim
Raw Normal View History

2016-02-06 02:07:11 +00:00
import asyncdispatch, db_sqlite, jester, moustachu, os,
2016-02-06 02:43:30 +00:00
shorturl, strutils, tables, times, typetraits
2016-02-05 18:40:26 +00:00
const
baseTemplate*: string = staticRead "./templates/layout.mustache"
indexTemplate*: string = staticRead "./templates/index.mustache"
2016-02-06 05:02:12 +00:00
browseTemplate*: string = staticRead "./templates/browse.mustache"
2016-02-06 02:07:11 +00:00
errorTemplate*: string = staticRead "./templates/error.mustache"
2016-02-06 02:43:30 +00:00
quoteTemplate*: string = staticRead "./templates/quote.mustache"
testTemplate*: string = """<p>hi!</p>"""
2016-02-06 02:07:11 +00:00
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)
2016-02-05 18:40:26 +00:00
2016-02-06 02:07:11 +00:00
template fail*(): expr =
var ctx = newContext()
ctx["exception"] = getCurrentExceptionMsg()
2016-02-06 02:43:30 +00:00
var
layoutCtx = moustachu.newContext()
2016-02-06 05:02:12 +00:00
layoutCtx["title"] = "fail"
layoutCtx["body"] = render(errorTemplate, ctx)
2016-02-06 02:43:30 +00:00
halt render(baseTemplate, layoutCtx)
2016-02-06 02:07:11 +00:00
2016-02-06 05:02:12 +00:00
template contextQuote(ctx: Context, qid: int, quote: Row): expr =
ctx["listid"] = qid
ctx["id"] = quote[0].parseInt().encodeURLSimple()
ctx["channel"] = quote[1]
ctx["channelsafe"] = quote[1].replace("#", "hashtag-")
ctx["nick"] = quote[2]
ctx["adder"] = quote[3]
ctx["message"] = quote[4]
ctx["time"] = parseInt(split(quote[5], '.')[0])
template pagination(ctx: Context, qid: int, kind: string): expr =
ctx["paging"] = true
if qid != 0:
ctx["isnt1"] = true
ctx["prev"] = qid - 1
ctx["next"] = qid + 1
ctx["kind"] = kind
2016-02-05 18:40:26 +00:00
settings:
port = 5000.Port
bindAddr = "0.0.0.0"
routes:
2016-02-06 02:07:11 +00:00
get "/test":
renderMustache("test", testTemplate, newContext())
2016-02-05 18:40:26 +00:00
2016-02-06 02:07:11 +00:00
get "/":
2016-02-06 05:02:12 +00:00
redirect "/browse/0"
2016-02-06 02:07:11 +00:00
2016-02-06 05:02:12 +00:00
get "/channel":
try:
var
channels = db.getAllRows(sql"select channel from quotes group by channel")
ctx: Context = newContext()
2016-02-06 02:07:11 +00:00
2016-02-06 05:02:12 +00:00
renderMustache("channel list", testTemplate, ctx)
except:
fail()
2016-02-06 02:07:11 +00:00
2016-02-06 05:02:12 +00:00
get "/@kind/@id":
var
title: string = ""
ctx: Context
rows: seq[Row]
qid: int = 0
2016-02-06 02:43:30 +00:00
2016-02-06 02:07:11 +00:00
try:
2016-02-06 02:43:30 +00:00
qid = (@"id").parseInt
2016-02-06 02:07:11 +00:00
except:
2016-02-06 02:43:30 +00:00
qid = (@"id").decodeURLSimple()
2016-02-06 05:02:12 +00:00
redirect "/" & @"kind" & "/" & $qid
2016-02-06 02:07:11 +00:00
2016-02-06 05:02:12 +00:00
case @"kind":
of "browse":
rows = db.getAllRows(sql"SELECT * FROM quotes ORDER BY time desc LIMIT 20 OFFSET ?", (qid * 20))
2016-02-06 02:07:11 +00:00
2016-02-06 05:02:12 +00:00
title = "page " & @"id"
of "quotes":
rows = db.getAllRows(sql"SELECT * FROM quotes WHERE id = ?", qid)
2016-02-06 02:43:30 +00:00
2016-02-06 05:02:12 +00:00
title = "quote #" & $qid & " by " & rows[0][2]
else:
if (@"kind").startsWith "hashtag-":
let channel = (@"kind").replace("hashtag-", "#")
rows = db.getAllRows(sql"SELECT * FROM quotes WHERE channel=? ORDER BY time desc LIMIT 20 OFFSET ?", channel, qid * 20)
2016-02-06 02:43:30 +00:00
2016-02-06 05:02:12 +00:00
title = channel & " page " & @"id"
else:
halt Http404, "not found"
2016-02-06 02:43:30 +00:00
2016-02-06 05:02:12 +00:00
ctx = newContext()
var quoteSeq = newSeq[Context]()
for row in items(rows):
var c = newContext()
c.contextQuote row[0].parseInt(), row
quoteSeq.add c
ctx["quotes"] = quoteSeq
ctx.pagination(qid, @"kind")
2016-02-06 02:07:11 +00:00
2016-02-06 05:02:12 +00:00
renderMustache(title, browseTemplate, ctx)
2016-02-06 02:07:11 +00:00
2016-02-05 18:40:26 +00:00
runForever()