http server
This commit is contained in:
parent
60b16577ae
commit
870c8cb012
9 changed files with 246 additions and 81 deletions
66
src/rss.nim
Normal file
66
src/rss.nim
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import std/[algorithm, json, xmltree, times, options, sequtils]
|
||||
import pkg/[mummy, rssatom, puppy, pretty]
|
||||
|
||||
const
|
||||
atomTimeFormat* {.strdefine.} = "yyyy-MM-dd'T'HH:mm:ss'Z'"
|
||||
rssv2TimeFormat* {.strdefine.} = "ddd',' dd MMM yyyy HH:mm:ss ZZZ"
|
||||
|
||||
type RssFeed* = object # name:string
|
||||
url*: string
|
||||
kind*: FeedType
|
||||
|
||||
proc parseAtomTime(s: string): DateTime =
|
||||
parse(s, atomTimeFormat)
|
||||
|
||||
proc parseRSSv2Time(s: string): DateTime =
|
||||
parse(s, rssv2TimeFormat)
|
||||
|
||||
proc getrssv2Time(r: RssItem): DateTime =
|
||||
result = dateTime(0, Month.low, MonthdayRange.low)
|
||||
if r.pubDate.isSome():
|
||||
result = parseRssv2Time(r.pubDate.get())
|
||||
|
||||
proc getTime(r: RssItem): DateTime =
|
||||
result = dateTime(0, Month.low, MonthdayRange.low)
|
||||
if r.updated.isSome():
|
||||
result = parseAtomTime(r.updated.get())
|
||||
elif r.pubDate.isSome():
|
||||
result = parseAtomTime(r.pubDate.get())
|
||||
|
||||
proc cmpRssItem(x, y: RssItem): int =
|
||||
cmp(x.getTime(), y.getTime())
|
||||
|
||||
proc mixRssFeeds*(feeds: seq[RssFeed], name, id, link, authorName, description:string): RSS =
|
||||
result = RSS()
|
||||
result.id = name.some()
|
||||
result.title = id.some()
|
||||
result.link = link.some()
|
||||
result.author.name = authorName.some()
|
||||
result.description = description.some()
|
||||
var entries: seq[RSS]
|
||||
for feed in feeds:
|
||||
case feed.kind
|
||||
of Atom:
|
||||
entries.add parseAtom(fetch(feed.url))
|
||||
of RSSv2:
|
||||
entries.add parseRss(fetch(feed.url))
|
||||
|
||||
entries[^1].items = entries[^1].items.mapIt(
|
||||
block:
|
||||
var item = it
|
||||
item.pubdate = some(getRssv2Time(item).format(atomTimeFormat))
|
||||
item
|
||||
)
|
||||
|
||||
for feed in entries:
|
||||
result.items.add feed.items
|
||||
|
||||
result.items.sort(cmp = cmpRssItem, order = Descending)
|
||||
|
||||
proc assembleAtom*(r: RSS): XmlNode =
|
||||
result = buildAtom(r)
|
||||
result.attrs = {
|
||||
"xmlns": "http://www.w3.org/2005/Atom",
|
||||
"xmlns:thr": "http://purl.org/syndication/thread/1.0",
|
||||
"xml:lang": "en-EN",
|
||||
}.toXmlAttributes()
|
||||
171
src/rssmix.nim
171
src/rssmix.nim
|
|
@ -1,80 +1,95 @@
|
|||
import std/[algorithm, json, xmltree, times, options, sequtils]
|
||||
import pkg/[mummy, rssatom, puppy, pretty]
|
||||
|
||||
const
|
||||
atomTimeFormat {.strdefine.} = "yyyy-MM-dd'T'HH:mm:ss'Z'"
|
||||
rssv2TimeFormat {.strdefine.} = "ddd',' dd MMM yyyy HH:mm:ss ZZZ"
|
||||
|
||||
type RssFeed = object # name:string
|
||||
url: string
|
||||
kind: FeedType
|
||||
|
||||
proc parseAtomTime(s: string): DateTime =
|
||||
parse(s, atomTimeFormat)
|
||||
|
||||
proc parseRSSv2Time(s: string): DateTime =
|
||||
parse(s, rssv2TimeFormat)
|
||||
|
||||
proc getrssv2Time(r:RssItem):DateTime=
|
||||
result = dateTime(0, Month.low, MonthdayRange.low)
|
||||
if r.pubDate.isSome():
|
||||
result = parseRssv2Time(r.pubDate.get())
|
||||
|
||||
|
||||
proc getTime(r: RssItem): DateTime =
|
||||
result = dateTime(0, Month.low, MonthdayRange.low)
|
||||
if r.updated.isSome():
|
||||
result = parseAtomTime(r.updated.get())
|
||||
elif r.pubDate.isSome():
|
||||
result = parseAtomTime(r.pubDate.get())
|
||||
|
||||
proc cmpRssItem(x, y: RssItem): int =
|
||||
cmp(x.getTime(), y.getTime())
|
||||
|
||||
proc mixRssFeeds(feeds: seq[RssFeed]): RSS =
|
||||
result = RSS()
|
||||
result.id = "https://example.com/".some()
|
||||
result.title = "This is a test".some()
|
||||
result.link = "link.com".some()
|
||||
result.author.name = "Samuel R.".some()
|
||||
result.description = "this is a test for this and that".some()
|
||||
var entries: seq[RSS]
|
||||
for feed in feeds:
|
||||
case feed.kind
|
||||
of Atom:
|
||||
entries.add parseAtom(fetch(feed.url))
|
||||
of RSSv2:
|
||||
entries.add parseRss(fetch(feed.url))
|
||||
|
||||
entries[^1].items = entries[^1].items.mapIt(
|
||||
block:
|
||||
var item = it
|
||||
item.pubdate = some(getRssv2Time(item).format(atomTimeFormat))
|
||||
item
|
||||
)
|
||||
|
||||
for feed in entries:
|
||||
result.items.add feed.items
|
||||
|
||||
result.items.sort(cmp = cmpRssItem, order = Descending)
|
||||
|
||||
proc assembleAtom(r: RSS): XmlNode =
|
||||
result = buildAtom(r)
|
||||
result.attrs = {
|
||||
"xmlns": "http://www.w3.org/2005/Atom",
|
||||
"xmlns:thr": "http://purl.org/syndication/thread/1.0",
|
||||
"xml:lang": "en-EN"
|
||||
}.toXmlAttributes()
|
||||
|
||||
discard
|
||||
|
||||
echo assembleAtom(
|
||||
mixRssFeeds(
|
||||
@[
|
||||
RssFeed(kind: Atom, url: "https://github.com/nim-lang/Nim/releases.atom"),
|
||||
RssFeed(kind: Atom, url: "https://github.com/fatedier/frp/releases.atom"),
|
||||
RssFeed(kind: RSSv2, url: "https://fedoramagazine.org/rss"),
|
||||
RssFeed(kind: RSSv2, url: "http://communityblog.fedoraproject.org/?feed=rss2"),
|
||||
import
|
||||
std/
|
||||
[
|
||||
algorithm, json, xmltree, times, options, sequtils, tables, os, exitprocs,
|
||||
strutils,
|
||||
]
|
||||
import pkg/[rssatom, pretty, parsetoml, jsony]
|
||||
import pkg/[mummy, mummy/routers]
|
||||
import ./[tomlToJson, rss]
|
||||
|
||||
const defaultConfigPath {.strdefine.} = ""
|
||||
|
||||
type Config = object
|
||||
name: string
|
||||
feeds: seq[RssFeed]
|
||||
|
||||
proc parseConfig(content: string): Config =
|
||||
parseString(content).customtoJson().toJson().fromJson(Config)
|
||||
|
||||
var
|
||||
configs: Table[string, seq[RssFeed]]
|
||||
server: Server
|
||||
stopping: bool
|
||||
|
||||
proc atomHandler(r: Request) {.gcsafe, thread.} =
|
||||
let name = r.pathParams["name"]
|
||||
{.gcsafe.}:
|
||||
# safe as configs won't be modified after the server started
|
||||
if not configs.contains(name):
|
||||
r.respond(404, body = "no such mix")
|
||||
return
|
||||
var headers: HttpHeaders
|
||||
headers["Content-Type"] = "application/atom+xml"
|
||||
|
||||
var cfg = configs[name]
|
||||
let mixed = mixRssFeeds(cfg, "rssmix: "&name, name, "/atom/"&name, "rssmix", "mix of various rss feeds")
|
||||
|
||||
r.respond(200, headers, body = $assembleAtom(mixed))
|
||||
|
||||
proc stop() =
|
||||
if stopping:
|
||||
return
|
||||
stopping = true
|
||||
echo "closing server"
|
||||
server.close()
|
||||
quit()
|
||||
|
||||
proc addfeeds(name:string, feeds:seq[RssFeed])=
|
||||
if configs.contains(name): raise newException(KeyError, "duplicate config: "&name)
|
||||
configs[name] = feeds
|
||||
|
||||
|
||||
proc main() =
|
||||
block:
|
||||
var cfgs = commandLineParams()
|
||||
if cfgs.len == 0:
|
||||
when defaultConfigPath.len == 0:
|
||||
quit("rssmix configs", QuitFailure)
|
||||
else:
|
||||
cfgs = @[defaultConfigPath]
|
||||
for cfg in cfgs:
|
||||
if fileExists(cfg):
|
||||
let cfg = readFile(cfg).parseConfig()
|
||||
addfeeds(cfg.name, cfg.feeds)
|
||||
elif dirExists(cfg):
|
||||
for f in walkFiles(cfg/"*"):
|
||||
let cfg = readFile(f).parseConfig()
|
||||
addfeeds(cfg.name, cfg.feeds)
|
||||
if configs.len == 0: quit("no configs found.", QuitFailure)
|
||||
echo "registered feeds:"
|
||||
for key, value in configs.pairs:
|
||||
echo " - ", key, " (", value.len, " sub-feeds)"
|
||||
|
||||
|
||||
|
||||
var router: Router
|
||||
router.get("/atom/@name", atomHandler)
|
||||
|
||||
server = newServer(router)
|
||||
|
||||
addExitProc(stop)
|
||||
setControlCHook(
|
||||
proc() {.noconv.} =
|
||||
stop()
|
||||
)
|
||||
)
|
||||
|
||||
let
|
||||
bindAddr = getEnv("RSSMIX_BINDADDR", "0.0.0.0")
|
||||
port = getEnv("RSSMIX_PORT", "8085")
|
||||
|
||||
echo "running on http://" & bindAddr & ":" & port
|
||||
server.serve(Port(parseInt(port)), bindAddr)
|
||||
|
||||
when isMainModule:
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue