view server.py @ 24:b094768c285b

wiki.html now communicates w/ the server instead of the static file.
author Atul Varma <varmaa@toolness.com>
date Thu, 12 Feb 2009 19:32:36 -0800
parents 6c84924cb63a
children 0aecc756ea18
line wrap: on
line source

import re

import atulweb

status = dict(max_changeset = 0)
changesets = {
    0: dict(date='Thu Feb 12 2009 19:03:56 GMT-0800 (PST)',
            content=open('static-files/wiki.txt', 'r').read())
    }

CID_PATH = re.compile(r'/(\d+)')

@atulweb.wsgiapp
def application(environ, start_response):
    if environ['REQUEST_METHOD'] == 'GET':
        path = environ['PATH_INFO']
        if path == '/status':
            return atulweb.Response(status, 'application/json')
        else:
            match = CID_PATH.match(path)
            if match:
                cid = int(match.group(1))
                if cid in changesets:
                    return atulweb.Response(changesets[cid],
                                            'application/json')
                else:
                    raise atulweb.HttpError('not found')
            else:
                raise atulweb.HttpError('not found')
    elif environ['REQUEST_METHOD'] == 'PUT':
        match = CID_PATH.match(environ['PATH_INFO'])
        if match:
            cid = int(match.group(1))
            if cid == status['max_changeset'] + 1:
                status['max_changeset'] = cid
                req = atulweb.Request(environ)
                changeset = req.get_input('application/json')
                changesets[cid] = changeset
            else:
                raise atulweb.HttpError('bad request',
                                        'invalid changeset id')
        else:
            raise atulweb.HttpError('bad request')
    else:
        raise atulweb.HttpError('method not allowed')

if __name__ == '__main__':
    atulweb.main(application)