Use a template for JSON rendering, etc
This commit is contained in:
parent
50a6d7b5f6
commit
791b7d59a0
28
ponyapi.nim
28
ponyapi.nim
|
@ -44,13 +44,17 @@ proc `%%`(eps: seq[Episode]): JsonNode =
|
||||||
"episodes": eps,
|
"episodes": eps,
|
||||||
}
|
}
|
||||||
|
|
||||||
proc `%!`(why: string): JsonNode =
|
proc `%%`(why: string): JsonNode =
|
||||||
## Make an error object
|
## Make an error object
|
||||||
%*
|
%*
|
||||||
{
|
{
|
||||||
"error": why
|
"error": why
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template httpReply(code, body: expr): expr =
|
||||||
|
## Make things a lot simpler for replies, etc.
|
||||||
|
resp code, myHeaders, pretty(%%body, 4)
|
||||||
|
|
||||||
let myHeaders = {
|
let myHeaders = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"X-Powered-By": "Nim and Jester",
|
"X-Powered-By": "Nim and Jester",
|
||||||
|
@ -68,7 +72,7 @@ routes:
|
||||||
|
|
||||||
get "/all":
|
get "/all":
|
||||||
stats.all.success.inc
|
stats.all.success.inc
|
||||||
resp Http200, myHeaders, pretty(%%episodes, 4)
|
httpReply Http200, episodes
|
||||||
|
|
||||||
get "/newest":
|
get "/newest":
|
||||||
var
|
var
|
||||||
|
@ -83,11 +87,11 @@ routes:
|
||||||
break
|
break
|
||||||
|
|
||||||
stats.newest.success.inc
|
stats.newest.success.inc
|
||||||
resp Http200, myHeaders, pretty(%%ep, 4)
|
httpReply Http200, ep
|
||||||
|
|
||||||
get "/random":
|
get "/random":
|
||||||
stats.newest.success.inc
|
stats.newest.success.inc
|
||||||
resp Http200, myHeaders, pretty(%%episodes.randomChoice(), 4)
|
httpReply Http200, episodes.randomChoice()
|
||||||
|
|
||||||
get "/last_aired":
|
get "/last_aired":
|
||||||
var
|
var
|
||||||
|
@ -102,7 +106,7 @@ routes:
|
||||||
break
|
break
|
||||||
|
|
||||||
stats.lastAired.success.inc
|
stats.lastAired.success.inc
|
||||||
resp Http200, myHeaders, pretty(%%ep, 4)
|
httpReply Http200, ep
|
||||||
|
|
||||||
get "/season/@snumber":
|
get "/season/@snumber":
|
||||||
var
|
var
|
||||||
|
@ -111,10 +115,10 @@ routes:
|
||||||
|
|
||||||
if eps.len == 0:
|
if eps.len == 0:
|
||||||
stats.seasonLookup.fails.inc
|
stats.seasonLookup.fails.inc
|
||||||
resp Http404, myHeaders, pretty(%!"No episodes found", 4)
|
httpReply Http404, "No episodes found"
|
||||||
else:
|
else:
|
||||||
stats.seasonLookup.success.inc
|
stats.seasonLookup.success.inc
|
||||||
resp Http200, myHeaders, pretty(%%eps, 4)
|
httpReply Http200, eps
|
||||||
|
|
||||||
get "/season/@snumber/episode/@epnumber":
|
get "/season/@snumber/episode/@epnumber":
|
||||||
var
|
var
|
||||||
|
@ -129,10 +133,10 @@ routes:
|
||||||
|
|
||||||
if ep.air_date == 0:
|
if ep.air_date == 0:
|
||||||
stats.episodeLookup.fails.inc
|
stats.episodeLookup.fails.inc
|
||||||
resp Http404, myHeaders, pretty(%!"Not found", 4)
|
httpReply Http404, "Not found"
|
||||||
else:
|
else:
|
||||||
stats.episodeLookup.success.inc
|
stats.episodeLookup.success.inc
|
||||||
resp Http200, myHeaders, pretty(%%ep, 4)
|
httpReply Http200, ep
|
||||||
|
|
||||||
get "/search":
|
get "/search":
|
||||||
var
|
var
|
||||||
|
@ -140,7 +144,7 @@ routes:
|
||||||
|
|
||||||
if query == "":
|
if query == "":
|
||||||
stats.search.fails.inc
|
stats.search.fails.inc
|
||||||
halt Http406, myHeaders, pretty(%!"Need to specify a query", 4)
|
halt Http406, myHeaders, pretty(%%"Need to specify a query", 4)
|
||||||
|
|
||||||
var
|
var
|
||||||
eps: seq[Episode] =
|
eps: seq[Episode] =
|
||||||
|
@ -148,10 +152,10 @@ routes:
|
||||||
|
|
||||||
if eps.len == 0:
|
if eps.len == 0:
|
||||||
stats.search.fails.inc
|
stats.search.fails.inc
|
||||||
resp Http404, myHeaders, pretty(%!"No episodes found", 4)
|
httpReply Http404, "No episodes found"
|
||||||
else:
|
else:
|
||||||
stats.search.success.inc
|
stats.search.success.inc
|
||||||
resp Http200, myHeaders, pretty(%%eps, 4)
|
httpReply Http200, eps
|
||||||
|
|
||||||
get "/_stats":
|
get "/_stats":
|
||||||
resp Http200, myHeaders, pretty(%*
|
resp Http200, myHeaders, pretty(%*
|
||||||
|
|
Loading…
Reference in New Issue