Add /api/info, returning general info on the running pathod daemon.

This commit is contained in:
Aldo Cortesi 2012-06-07 11:39:37 +12:00
parent 14b2a69d21
commit 34ffe46fa0
3 changed files with 36 additions and 10 deletions

View File

@ -1,6 +1,6 @@
import urllib, pprint import urllib, pprint
import tornado.web, tornado.template, tornado.ioloop, tornado.httpserver import tornado.web, tornado.template, tornado.ioloop, tornado.httpserver
import rparse, utils import rparse, utils, version
class APILog(tornado.web.RequestHandler): class APILog(tornado.web.RequestHandler):
@ -24,6 +24,15 @@ class APIShutdown(tornado.web.RequestHandler):
self.write("OK") self.write("OK")
class APIInfo(tornado.web.RequestHandler):
def get(self):
self.write(
dict(
version = version.IVERSION
)
)
class _Page(tornado.web.RequestHandler): class _Page(tornado.web.RequestHandler):
def render(self, name, **kwargs): def render(self, name, **kwargs):
tornado.web.RequestHandler.render(self, name + ".html", **kwargs) tornado.web.RequestHandler.render(self, name + ".html", **kwargs)
@ -141,6 +150,7 @@ class PathodApp(tornado.web.Application):
(r"/help", Help), (r"/help", Help),
(r"/preview", Preview), (r"/preview", Preview),
(r"/api/shutdown", APIShutdown), (r"/api/shutdown", APIShutdown),
(r"/api/info", APIInfo),
(r"/api/log", APILog), (r"/api/log", APILog),
(r"/api/log/clear", APILogClear), (r"/api/log/clear", APILogClear),
(r"/p/.*", RequestPathod, settings), (r"/p/.*", RequestPathod, settings),
@ -228,7 +238,7 @@ def make_app(staticdir=None, anchors=()):
def make_server(application, port, address, ssl_options): def make_server(application, port, address, ssl_options):
""" """
Returns a (server, port) tuple. Returns a (server, port) tuple.
The returned port will match the passed port, unless the passed port The returned port will match the passed port, unless the passed port
was 0. In that case, an arbitrary empty port will be bound to, and this was 0. In that case, an arbitrary empty port will be bound to, and this

View File

@ -1,8 +1,9 @@
import threading import json, threading, Queue
import requests import requests
import Queue
import pathod import pathod
IFACE = "127.0.0.1"
class PaThread(threading.Thread): class PaThread(threading.Thread):
def __init__(self, q, app): def __init__(self, q, app):
threading.Thread.__init__(self) threading.Thread.__init__(self)
@ -11,7 +12,7 @@ class PaThread(threading.Thread):
self.port = None self.port = None
def run(self): def run(self):
self.server, self.port = pathod.make_server(self.app, 0, "127.0.0.1", None) self.server, self.port = pathod.make_server(self.app, 0, IFACE, None)
self.q.put(self.port) self.q.put(self.port)
pathod.run(self.server) pathod.run(self.server)
@ -23,9 +24,12 @@ class Daemon:
self.thread = PaThread(self.q, self.app) self.thread = PaThread(self.q, self.app)
self.thread.start() self.thread.start()
self.port = self.q.get(True, 5) self.port = self.q.get(True, 5)
self.urlbase = "http://%s:%s"%(IFACE, self.port)
def clear(self): def info(self):
pass resp = requests.get("%s/api/info"%self.urlbase)
if resp.ok:
return json.loads(resp.read())
def shutdown(self): def shutdown(self):
requests.post("http://localhost:%s/api/shutdown"%self.port) requests.post("%s/api/shutdown"%self.urlbase)

View File

@ -1,10 +1,10 @@
import time import time
import libpry import libpry
import requests import requests
from libpathod import test from libpathod import test, version
class uDaemon(libpry.AutoTree): class uDaemonManual(libpry.AutoTree):
def test_startstop(self): def test_startstop(self):
d = test.Daemon() d = test.Daemon()
rsp = requests.get("http://localhost:%s/p/202"%d.port) rsp = requests.get("http://localhost:%s/p/202"%d.port)
@ -15,7 +15,19 @@ class uDaemon(libpry.AutoTree):
assert not rsp.ok assert not rsp.ok
class uDaemon(libpry.AutoTree):
def setUpAll(self):
self.d = test.Daemon()
def tearDownAll(self):
self.d.shutdown()
def test_info(self):
assert tuple(self.d.info()["version"]) == version.IVERSION
tests = [ tests = [
uDaemonManual(),
uDaemon() uDaemon()
] ]