aboutsummaryrefslogtreecommitdiffstats
path: root/libpathod/app.py
blob: fd742ce746e38c956cf784da10584b8a62e96334 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import logging, pprint, cStringIO
from flask import Flask, jsonify, render_template, request
import version, rparse

logging.basicConfig(level="DEBUG")
app = Flask(__name__)

@app.route('/api/info')
def api_info():
    return jsonify(
        version = version.IVERSION
    )


@app.route('/api/log')
def api_log():
    return jsonify(
        log = app.config["pathod"].get_log()
    )


@app.route('/api/clear_log')
def api_clear_log():
    app.config["pathod"].clear_log()
    return "OK"


@app.route('/')
@app.route('/index.html')
def index():
    return render_template("index.html", section="main")


@app.route('/docs/pathod')
def docs_pathod():
    return render_template("docs_pathod.html", section="docs")


@app.route('/docs/pathoc')
def docs_pathoc():
    return render_template("docs_pathoc.html", section="docs")


@app.route('/docs/test')
def docs_test():
    return render_template("docs_test.html", section="docs")


@app.route('/log')
def log():
    return render_template("log.html", section="log", log=app.config["pathod"].get_log())


@app.route('/log/<int:lid>')
def onelog(lid):
    l = pprint.pformat(app.config["pathod"].log_by_id(int(lid)))
    return render_template("onelog.html", section="log", alog=l, lid=lid)


SANITY = 1024*1024
@app.route('/preview')
def preview():
    spec = request.args["spec"]
    args = dict(
        spec = spec,
        section = "main",
        syntaxerror = None,
        error = None
    )
    try:
        r = rparse.parse_response(app.config["pathod"].request_settings, spec)
    except rparse.ParseException, v:
        args["syntaxerror"] = str(v)
        args["marked"] = v.marked()
        return render_template("preview.html", **args)
    if r.length() > SANITY:
        error = "Refusing to preview a response of %s bytes. This is for your own good."%r.length()
        args["error"] = error
    else:
        s = cStringIO.StringIO()
        r.serve(s)
        args["output"] = s.getvalue()
    return render_template("preview.html", **args)