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

Loading…
Cancel
Save