Set up the bones of a Flask app with configuration
This commit is contained in:
parent
0909fd7e99
commit
a7fdd6cbd0
7 changed files with 131 additions and 1 deletions
34
src/main.py
34
src/main.py
|
|
@ -1,3 +1,35 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
print('hello, world!')
|
||||
from waitress import serve
|
||||
from flask import Flask
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
from src.config import parse_config, DEFAULT_CONFIG, Config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def create_app(name: str, config: Config) -> Flask:
|
||||
logging.basicConfig(level=config.logging.level, format=('%(asctime)s %(levelname)s [%(name)s] %(message)s'))
|
||||
|
||||
app = Flask(name)
|
||||
|
||||
if config.host:
|
||||
app.config['SERVER_NAME'] = config.host
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser('Cereal', description='run the Cereal server')
|
||||
parser.add_argument('--config', '-c', help='The path to the configuration file, expects JSON')
|
||||
args = parser.parse_args()
|
||||
config = parse_config(args.config) if args.config else DEFAULT_CONFIG
|
||||
app = create_app('Cereal', config)
|
||||
logger.info(str(config))
|
||||
serve(app, host='0.0.0.0' if config.host is None else config.host, port=config.port)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue