Skip to content

Instantly share code, notes, and snippets.

@lost-theory
Last active February 9, 2022 16:58
Show Gist options
  • Select an option

  • Save lost-theory/d6beaeb7ae5ead166227 to your computer and use it in GitHub Desktop.

Select an option

Save lost-theory/d6beaeb7ae5ead166227 to your computer and use it in GitHub Desktop.

Revisions

  1. lost-theory revised this gist Feb 25, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion app_factory.py
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@

    from flask import Flask, Blueprint, g

    app = Flask(__name__)
    api = Blueprint('api', __name__)

    class Server(object):
  2. lost-theory revised this gist Feb 25, 2015. 4 changed files with 45 additions and 2 deletions.
    35 changes: 35 additions & 0 deletions app_factory.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import os

    from flask import Flask, Blueprint, g

    app = Flask(__name__)
    api = Blueprint('api', __name__)

    class Server(object):
    def __init__(self, data):
    self.data = data

    @api.route("/api_method", methods=['GET', 'POST'])
    def api_method():
    return g.server.data

    def make_app():
    '''
    Application factory: http://flask.pocoo.org/docs/0.10/patterns/appfactories/
    '''
    app = Flask(__name__)
    app.register_blueprint(api)

    with open(os.environ['SERVER_FILE']) as f:
    server = Server(f.read())

    @app.before_request
    def add_server_to_globals():
    g.server = server

    return app

    if __name__ == '__main__':
    app = make_app()
    app.config['DEBUG'] = True
    app.run(use_debugger=True, use_reloader=True)
    2 changes: 2 additions & 0 deletions app_gunicorn.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    from app_factory import make_app
    app = make_app()
    2 changes: 1 addition & 1 deletion app.py → app_simple.py
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ def __init__(self, data):
    def api_method():
    return g.server.data

    @app.before_first_request
    @app.before_request
    def add_server_to_globals():
    with open(os.environ['SERVER_FILE']) as f:
    g.server = Server(f.read())
    8 changes: 7 additions & 1 deletion output.txt
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,16 @@
    $ SERVER_FILE=app.py bin/gunicorn app:app
    $ SERVER_FILE=app.py bin/gunicorn app_simple:app
    [2015-02-24 10:10:20 -0800] [3217] [INFO] Starting gunicorn 19.2.1
    [2015-02-24 10:10:20 -0800] [3217] [INFO] Listening at: http://127.0.0.1:8000 (3217)
    [2015-02-24 10:10:20 -0800] [3217] [INFO] Using worker: sync
    [2015-02-24 10:10:20 -0800] [3220] [INFO] Booting worker with pid: 3220
    ...

    OR if you use the application factory:

    $ SERVER_FILE=app.py bin/gunicorn app_gunicorn:app
    [2015-02-25 09:52:29 -0800] [5613] [INFO] Starting gunicorn 19.2.1
    ...

    $ curl http://127.0.0.1:8000/api_method
    import os

  3. lost-theory created this gist Feb 24, 2015.
    21 changes: 21 additions & 0 deletions app.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    import os

    from flask import Flask, g

    app = Flask(__name__)

    class Server(object):
    def __init__(self, data):
    self.data = data

    @app.route("/api_method", methods=['GET', 'POST'])
    def api_method():
    return g.server.data

    @app.before_first_request
    def add_server_to_globals():
    with open(os.environ['SERVER_FILE']) as f:
    g.server = Server(f.read())

    if __name__ == '__main__':
    app.run()
    14 changes: 14 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    $ SERVER_FILE=app.py bin/gunicorn app:app
    [2015-02-24 10:10:20 -0800] [3217] [INFO] Starting gunicorn 19.2.1
    [2015-02-24 10:10:20 -0800] [3217] [INFO] Listening at: http://127.0.0.1:8000 (3217)
    [2015-02-24 10:10:20 -0800] [3217] [INFO] Using worker: sync
    [2015-02-24 10:10:20 -0800] [3220] [INFO] Booting worker with pid: 3220
    ...

    $ curl http://127.0.0.1:8000/api_method
    import os

    from flask import Flask, g

    app = Flask(__name__)
    ...