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("

", "").replace("

", "") 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))