Created
July 23, 2015 19:43
-
-
Save aperezdc/a4ba784d652cb6155a9b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# vim:fenc=utf-8 | |
# | |
# Copyright © 2015 Adrian Perez <[email protected]> | |
# | |
# Distributed under terms of the MIT license. | |
import bottle | |
import hipack, yaml, json | |
loaders = { | |
"hipack": hipack.load, | |
"yaml" : yaml.safe_load, | |
"json" : json.load, | |
} | |
stats = { | |
"num_requests": 0, | |
"num_requests_with_nulls": 0, | |
} | |
@bottle.route("/<format>", method="POST") | |
def hipack_from(format): | |
bottle.response.set_header("Content-Type", "text/plain") | |
try: | |
data = loaders[format](bottle.request.body) | |
except Exception as e: | |
bottle.response.status = 500 | |
return e.message | |
global stats | |
stats["num_requests"] += 1 | |
try: | |
return hipack.dumps(data) | |
except TypeError as e: | |
if e.message == "Values of type <type 'NoneType'> cannot be dumped": | |
stats["num_requests_with_nulls"] += 1 | |
raise | |
@bottle.route("/stats") | |
def get_stats(): | |
bottle.response.set_header("Content-Type", "text/plain") | |
return hipack.dumps(stats) | |
if __name__ == "__main__": | |
bottle.run(host="localhost", port=10080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment