Browse Source

switch to Jester

master
Wesley Kerfoot 4 years ago
parent
commit
19556d3c10
  1. 82
      src/twit2blogpkg/server.nim
  2. 8
      templates/authors.html
  3. 10
      templates/layout.html
  4. 3
      templates/nonexistent.html
  5. 9
      templates/thread.html
  6. 8
      templates/threads.html
  7. 2
      twit2blog.nimble

82
src/twit2blogpkg/server.nim

@ -1,6 +1,8 @@
import strutils, options, sugar, sequtils, asyncdispatch, threadpool, db_sqlite, strformat
import twitter
import xander
import jester
from htmlgen import nil
# one thread just receives messages with thread ID / username
# thread then passes messages to worker threads in round-robin fashion
@ -57,37 +59,61 @@ proc insertThread(thread : TwitterThread) =
thread.author,
thread.tweets)
get "/thread/:author/status/:tweetID":
let tweetID = data{"tweetID"}.getStr()
let author = data{"author"}.getStr()
let thread = threadExists(tweetID, author)
if thread.isSome:
data["title"] = fmt"Thread by {author}"
data["tweets"] = thread.get.tweets.split("\n")
respond tmplt("thread", data)
else:
chan.send(ThreadRequest(tweetID: tweetID, author: author))
data["title"] = "Check back later"
respond tmplt("nonexistent", data)
get "/":
# lists all authors
data["authors"] = allAuthors.toSeq
data["title"] = "Authors"
respond tmplt("authors", data)
get "/author/:author/threads":
let author = data{"author"}.getStr()
data["author"] = author
data["title"] = fmt"Threads for {author}"
data["threads"] = toSeq(threadIDs(author))
respond tmplt("threads", data)
router twitblog:
get "/thread/@author/status/@tweetID":
let tweetID = @"tweetID"
let author = @"author"
let thread = threadExists(tweetID, author)
if thread.isSome:
let title = fmt"Thread by {author}"
let tweets = thread.get.tweets.split("\n")
resp htmlgen.body(
htmlgen.a(href=fmt"/author/{author}/threads", fmt"See all of {author}'s threads"),
htmlgen.h4(title),
htmlgen.ul(tweets.map((t) => htmlgen.p(htmlgen.li(t))).join(""))
)
else:
chan.send(ThreadRequest(tweetID: tweetID, author: author))
resp htmlgen.h4("Check back later")
get "/":
# lists all authors
let authors = allAuthors.toSeq
let title = "Authors"
resp htmlgen.body(
htmlgen.h4(title),
htmlgen.ul(
authors.map((author) =>
htmlgen.li(
htmlgen.a(href=fmt"/author/{author}/threads", author)
)
).join("")
)
)
get "/author/@author/threads":
let author = @"author"
let title = fmt"Threads for {author}"
let threads = toSeq(threadIDs(author))
resp htmlgen.body(
htmlgen.h4(title),
htmlgen.ul(
threads.map((thread) =>
htmlgen.li(
htmlgen.a(href=fmt"/thread/{author}/status/{thread}", thread)
)
).join("")
)
)
proc startServer* =
createTweetTable()
defer: db.close()
runForever(8080)
let port = 8080.Port
let settings = newSettings(port=port)
var jester = initJester(twitblog, settings=settings)
jester.serve()
proc handleRenders* =
while true:

8
templates/authors.html

@ -1,8 +0,0 @@
<h4>
{[ title ]}
</h4>
<ul>
{[ for author in authors ]}
<li><p><a href="/author/{[ author ]}/threads">{[ author ]}</a></p></li>
{[ end ]}
</ul>

10
templates/layout.html

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>{[ title ]}</title>
</head>
<body>
<h3><a href="/">Main Page</a></h3>
{[ content ]}
</body>
</html>

3
templates/nonexistent.html

@ -1,3 +0,0 @@
<h4>
Check back later
</h4>

9
templates/thread.html

@ -1,9 +0,0 @@
<a href="/author/{[ author ]}/threads">See all of {[ author ]}'s threads</a>
<h4>
{[ title ]}
</h4>
<ul>
{[ for tweet in tweets ]}
<li><p>{[ tweet ]}</p></li>
{[ end ]}
</ul>

8
templates/threads.html

@ -1,8 +0,0 @@
<h4>
{[ title ]}
</h4>
<ul>
{[ for thread in threads ]}
<li><a href="/thread/{[ author ]}/status/{[ thread ]}">{[ thread ]}</a></li>
{[ end ]}
</ul>

2
twit2blog.nimble

@ -13,4 +13,4 @@ bin = @["twit2blog"]
# Dependencies
requires "nim >= 1.0.9", "regex"
requires "https://github.com/sunjohanday/xander"
requires "https://github.com/dom96/jester"

Loading…
Cancel
Save