Skip to content

Instantly share code, notes, and snippets.

@lost-theory
Last active February 9, 2022 16:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lost-theory/d6beaeb7ae5ead166227 to your computer and use it in GitHub Desktop.
Save lost-theory/d6beaeb7ae5ead166227 to your computer and use it in GitHub Desktop.
import os
from flask import Flask, Blueprint, g
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)
from app_factory import make_app
app = make_app()
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_request
def add_server_to_globals():
with open(os.environ['SERVER_FILE']) as f:
g.server = Server(f.read())
if __name__ == '__main__':
app.run()
$ 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
from flask import Flask, g
app = Flask(__name__)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment