Rewrite using jester
This commit is contained in:
parent
879568cd04
commit
9d362344c9
2 changed files with 28 additions and 17 deletions
|
|
@ -12,3 +12,4 @@ bin = @["etherpad2latex"]
|
||||||
# Dependencies
|
# Dependencies
|
||||||
|
|
||||||
requires "nim >= 0.20.0"
|
requires "nim >= 0.20.0"
|
||||||
|
requires "jester >= 0.4.1"
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,34 @@
|
||||||
import asynchttpserver, asyncdispatch, httpclient
|
import asynchttpserver, asyncdispatch, httpclient
|
||||||
import osproc, strutils, parseopt
|
import os, osproc, strutils, parseopt
|
||||||
|
|
||||||
proc getContent(pad:string):string =
|
import jester
|
||||||
discard
|
|
||||||
|
proc getContent(pad:string, etherpadUrl:string):string =
|
||||||
|
var client = newHTTPClient()
|
||||||
|
result = client.getContent(etherpadUrl & "pad/p/" & pad & "/export/txt")
|
||||||
|
|
||||||
proc requestHandler(req:Request) {.async.} =
|
|
||||||
let
|
|
||||||
padName = req.url.path
|
|
||||||
content = getContent(padName)
|
|
||||||
let (output, exitCode) = execCmdEx("make")
|
|
||||||
let headers = if exitCode == 0: newHttpHeaders([("Content-Type","application/pdf")])
|
|
||||||
else: newHttpHeaders([("Content-Type","text/plain")])
|
|
||||||
let pdf = if exitCode == 0: readFile("Abgabe.pdf")
|
|
||||||
else: output
|
|
||||||
await req.respond(Http200, pdf, headers)
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
var
|
var
|
||||||
p = initOptParser()
|
p = initOptParser()
|
||||||
port = 8080
|
port = 8080
|
||||||
etherpadAddress = "http://localhost:9001/"
|
etherpadUrl = "http://localhost:9001/"
|
||||||
server = newAsyncHttpServer()
|
|
||||||
|
router main:
|
||||||
|
get "/@pad":
|
||||||
|
let
|
||||||
|
pad = @"pad"
|
||||||
|
content = getContent(pad, etherpadUrl)
|
||||||
|
texFile = open("etherpad.tex", fmWrite)
|
||||||
|
|
||||||
|
texFile.write(content)
|
||||||
|
texFile.close()
|
||||||
|
|
||||||
|
let (output, exitCode) = execCmdEx(quoteShellCommand(["pdflatex", "-halt-on-error", "etherpad.tex"]))
|
||||||
|
if exitCode == 0:
|
||||||
|
sendFile("etherpad.pdf")
|
||||||
|
else:
|
||||||
|
resp output
|
||||||
|
|
||||||
# Parse command-line args
|
# Parse command-line args
|
||||||
while true:
|
while true:
|
||||||
|
|
@ -32,8 +40,10 @@ when isMainModule:
|
||||||
of "p", "port":
|
of "p", "port":
|
||||||
port = p.val.parseInt
|
port = p.val.parseInt
|
||||||
of "a", "etherpad-address":
|
of "a", "etherpad-address":
|
||||||
etherpadAddress = p.val
|
etherpadUrl = p.val
|
||||||
else: discard
|
else: discard
|
||||||
of cmdArgument: continue
|
of cmdArgument: continue
|
||||||
|
|
||||||
waitFor server.serve(Port(port), requestHandler)
|
let s = newSettings(port=Port(port))
|
||||||
|
var j = initJester(main, settings=s)
|
||||||
|
j.serve()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue