Skip to content

Instantly share code, notes, and snippets.

@aperezdc
Created July 23, 2015 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aperezdc/a4ba784d652cb6155a9b to your computer and use it in GitHub Desktop.
Save aperezdc/a4ba784d652cb6155a9b to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2015 Adrian Perez <aperez@igalia.com>
#
# 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