onionvote/onionvote/__init__.py
2025-02-10 16:16:41 +01:00

63 lines
1.6 KiB
Python

import os
from flask import Flask
from logging.config import dictConfig
def create_app(test_config=None):
dictConfig({
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
}},
'root': {
'level': 'INFO',
'handlers': ['wsgi']
}
})
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'onionvote.sqlite3'),
ENABLE_API=True
)
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
from . import db
db.init_app(app)
if app.config['ENABLE_API']:
from . import api
app.register_blueprint(api.bp)
from . import vote
app.register_blueprint(vote.bp)
from . import index
app.register_blueprint(index.bp)
app.add_url_rule('/', endpoint='index')
# a simple page that says hello
@app.route('/hello')
def hello():
return 'Hello, World!'
return app