Created
May 15, 2014 22:40
-
-
Save jots/d36c2a8a938bf9d550b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, db_mysql, asynchttpserver, httpclient, parseurl, asyncdispatch, strtabs, strutils | |
var server = newAsyncHttpServer() | |
var db: TDbConn | |
db = open(connection="localhost", user="root", password="", | |
database="") | |
let aheaders = {"Content-Type": "text/html"}.newStringTable | |
proc onRequest(req: TRequest) {.async.} = | |
var client = newAsyncHttpClient() | |
var htmlResp = "" | |
echo req.url.path | |
if req.url.path == "sleep": | |
# XXX was hoping that this would not block the whole server... | |
var r = db.getRow(TSqlQuery("select sleep(3)")) | |
echo "slept" | |
req.respond(Http200, "slept", aheaders) | |
return | |
try: | |
let reqResp = await client.get(req.url.path) | |
htmlResp = | |
if reqResp.status.startsWith("200"): "It's up!" | |
else: "It's down :(" | |
except EOS: | |
htmlResp = "EOS: It's down :(" | |
let headers = {"Content-Type": "text/html"} | |
echo("Replying with: $1 for $2" % [htmlResp, req.url.path]) | |
await req.respond(Http200, htmlResp, headers.newStringTable) | |
server.serve(TPort(8080), onRequest) | |
runForever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment