#!/usr/bin/python

import flup.server.fcgi
import json

import trigger_build
import process_comment

requests_map = {
    'trigger-build': trigger_build.handle,
    'process-comment': process_comment.handle,
}

def wsgi_app(environ, start_response):
    obj = json.load(environ['wsgi.input'])
    request = environ['PATH_INFO']
    request = request.split('/')[-1]
    try:
        requests_map[request](obj)
    except KeyError:
        start_response('404 Not found', [('Content-type', 'text/plain')])
    else:
        start_response('200 OK', [('Content-type', 'text/plain')])
    return ['OK']

if __name__ == '__main__':
    flup.server.fcgi.WSGIServer(wsgi_app).run()
