1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import std/[os, options, cgi, times, strutils]
import libs/tiny_sqlite
import libs/[moustachu, moustachu_context]
write(stdout, "Content-Type: text/html; charset=UTF-8\n")
write(stdout, "Cache-Control: no-store, must-revalidate\n")
write(stdout, "Pragma: no-cache\n")
write(stdout, "Expires: 0\n")
write(stdout, "\n")
if getEnv("DB_PATH") == "":
write(stdout, "No DB_PATH given!")
quit(QuitSuccess)
const websiteTemplate : string = staticRead "website.tpl"
var templateContext : Context = newContext()
let
db : DbConn = openDatabase(getEnv("DB_PATH"))
image : Option[ResultRow] = db.one("SELECT post_id, url FROM images WHERE containsHorn = 1 ORDER BY RANDOM() LIMIT 1;")
templateContext["SCRIPT_URL"] = getRequestURI()
if image.isSome:
templateContext["IMAGE_URL"] = fromDbValue(image.get["url"], string)
let post : Option[ResultRow] = db.one("SELECT url, content, createdAt FROM posts WHERE id = ? LIMIT 1;", fromDbValue(image.get["post_id"], int64))
if post.isSome:
let postDateTime : DateTime = fromDbValue(post.get["createdAt"], string).parse("yyyy-MM-dd'T'HH:mm:ss")
templateContext["POST_CONTENT"] = fromDbValue(post.get["content"], string).replace("<p>", "").replace("</p>", "")
templateContext["POST_URL"] = fromDbValue(post.get["url"], string)
templateContext["POST_DATE"] = postDateTime.format("dd'.'MM'.'yyyy")
templateContext["POST_TIME"] = postDateTime.format("HH:mm")
else:
write(stdout, "Database seems empty! :(")
quit(QuitSuccess)
db.close()
write(stdout, render(websiteTemplate, templateContext))