|
| 1 | +#!/usr/bin/env python |
| 2 | +import fastwsgi |
| 3 | +import falcon |
| 4 | +import re |
| 5 | +from db_orm import session, World, Fortune |
| 6 | +from helpers import load_template, FortuneTuple, generate_ids, sanitize |
| 7 | +from operator import attrgetter |
| 8 | +from random import randint |
| 9 | + |
| 10 | + |
| 11 | +# setup |
| 12 | +app = falcon.App() |
| 13 | + |
| 14 | +server_info = 'FastWSGI+Falcon' |
| 15 | + |
| 16 | + |
| 17 | +# resource endpoints |
| 18 | +class JSONResource(object): |
| 19 | + def on_get(self, request, response): |
| 20 | + response.set_header('Server', server_info) |
| 21 | + response.media = {'message': "Hello, world!"} |
| 22 | + |
| 23 | + |
| 24 | +class SingleQuery(object): |
| 25 | + @session(serializable=False) |
| 26 | + def on_get(self, request, response): |
| 27 | + wid = randint(1, 10000) |
| 28 | + world = World[wid] |
| 29 | + response.set_header('Server', server_info) |
| 30 | + response.media = world.to_dict() |
| 31 | + |
| 32 | + |
| 33 | +class MultipleQueries(object): |
| 34 | + @session(serializable=False) |
| 35 | + def on_get(self, request, response, num): |
| 36 | + num = sanitize(num) |
| 37 | + worlds = [World[ident].to_dict() for ident in generate_ids(num)] |
| 38 | + response.set_header('Server', server_info) |
| 39 | + response.media = worlds |
| 40 | + |
| 41 | + |
| 42 | +class UpdateQueries(object): |
| 43 | + @session(serializable=False) |
| 44 | + def on_get(self, request, response, num): |
| 45 | + num = sanitize(num) |
| 46 | + ids = generate_ids(num) |
| 47 | + ids.sort() |
| 48 | + worlds = [] |
| 49 | + for item in ids: |
| 50 | + world = World[item] |
| 51 | + world.randomNumber = randint(1, 10000) |
| 52 | + worlds.append({"id": world.id, "randomNumber": world.randomNumber}) |
| 53 | + response.set_header('Server', server_info) |
| 54 | + response.media = worlds |
| 55 | + |
| 56 | + |
| 57 | +class Fortunes(object): |
| 58 | + _template = load_template() |
| 59 | + |
| 60 | + @session(serializable=False) |
| 61 | + def on_get(self, request, response): |
| 62 | + fortunes = [FortuneTuple(id=f.id, message=f.message) for f in Fortune.select()] |
| 63 | + fortunes.append(FortuneTuple(id=0, message="Additional fortune added at request time.")) |
| 64 | + fortunes.sort(key=attrgetter("message")) |
| 65 | + content = self._template.render(fortunes=fortunes) |
| 66 | + response.set_header('Server', server_info) |
| 67 | + response.content_type = falcon.MEDIA_HTML |
| 68 | + response.text = content |
| 69 | + |
| 70 | + |
| 71 | +class PlaintextResource(object): |
| 72 | + def on_get(self, request, response): |
| 73 | + response.set_header('Server', server_info) |
| 74 | + response.content_type = falcon.MEDIA_TEXT |
| 75 | + response.text = 'Hello, world!' |
| 76 | + |
| 77 | + |
| 78 | +# register resources |
| 79 | +app.add_route("/json", JSONResource()) |
| 80 | +app.add_route("/db", SingleQuery()) |
| 81 | +app.add_route("/queries/{num}", MultipleQueries()) |
| 82 | +app.add_route("/updates/{num}", UpdateQueries()) |
| 83 | +app.add_route("/fortunes", Fortunes()) |
| 84 | +app.add_route("/plaintext", PlaintextResource()) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + import os |
| 89 | + import multiprocessing |
| 90 | + |
| 91 | + _is_travis = os.environ.get('TRAVIS') == 'true' |
| 92 | + |
| 93 | + workers = int(multiprocessing.cpu_count()) |
| 94 | + if _is_travis: |
| 95 | + workers = 2 |
| 96 | + |
| 97 | + host = '0.0.0.0' |
| 98 | + port = 8080 |
| 99 | + |
| 100 | + def run_app(): |
| 101 | + fastwsgi.run(app, host=host, port=port, loglevel=0) |
| 102 | + |
| 103 | + def create_fork(): |
| 104 | + n = os.fork() |
| 105 | + # n greater than 0 means parent process |
| 106 | + if not n > 0: |
| 107 | + run_app() |
| 108 | + |
| 109 | + # fork limiting the cpu count - 1 |
| 110 | + for i in range(1, workers): |
| 111 | + create_fork() |
| 112 | + |
| 113 | + run_app() # run app on the main process too :) |
0 commit comments