-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (39 loc) · 1.47 KB
/
Copy pathmain.py
File metadata and controls
57 lines (39 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
from flask import Flask
from api.conf.config import SQLALCHEMY_DATABASE_URI
from api.conf.routes import generate_routes
from api.database.database import db
def create_app():
# Create a flask app.
app = Flask(__name__)
# Set secret key from environment.
SECRET_KEY = os.environ.get("SECRET_KEY")
if not SECRET_KEY:
raise RuntimeError(
"SECRET_KEY environment variable must be set. "
"Generate one with: python -c 'import secrets; print(secrets.token_hex(32))'"
)
app.config['SECRET_KEY'] = SECRET_KEY
# Set debug from environment (default: False for production safety).
app.config['DEBUG'] = os.environ.get("DEBUG", "False").lower() in ("true", "1", "yes")
# Set database url.
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# Generate routes.
generate_routes(app)
# Database initialize with app.
db.init_app(app)
# Create all database tables within an application context.
# create_all() is a no-op for tables that already exist.
with app.app_context():
db.create_all()
# Return app.
return app
if __name__ == '__main__':
# Create app.
app = create_app()
# Run app. For production use another web server.
# Set debug and use_reloader parameters as False.
app.run(port=5000, debug=True, host='localhost', use_reloader=True)