-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
96 lines (81 loc) · 2.47 KB
/
Copy path__init__.py
File metadata and controls
96 lines (81 loc) · 2.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import logging
from logging.handlers import SMTPHandler
from flask import Flask
from flask_restplus import Api
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
authorizations = {
"bearer": {
"type": "apiKey",
"in": "header",
"name": "Authorization"
},
"basic": {
"type": "basic"
}
}
cors_resources = {
r"/*": {
"origins": [
"https://metamarcdw.github.io",
"http://localhost:3000"
],
"supports_credentials": True
}
}
api = Api(authorizations=authorizations)
cors = CORS(resources=cors_resources)
db = SQLAlchemy()
jwt = JWTManager()
jwt._set_error_handler_callbacks(api)
# https://github.com/vimalloc/flask-jwt-extended/issues/83
limiter = Limiter(
key_func=get_remote_address, default_limits=["200 per day", "50 per hour"])
def create_app():
mode = os.environ.get("TODOS_FS_MODE")
config_type = None
# Specifying the "server" module is needed when run indirectly
if mode == "production":
config_type = "server.config.ProductionConfig"
elif mode == "development":
config_type = "server.config.DevelopmentConfig"
elif mode == "testing":
config_type = "server.config.TestingConfig"
else:
raise ValueError("Mode variable not set.")
print(f" * Running the API in {mode} mode.")
app = Flask(__name__)
app.config.from_object(config_type)
api.init_app(app)
cors.init_app(app)
db.init_app(app)
jwt.init_app(app)
limiter.init_app(app)
@app.shell_context_processor
def make_shell_context():
from server.models import User, Todo
return {
"api": api,
"cors": cors,
"db": db,
"jwt": jwt,
"User": User,
"Todo": Todo
}
if mode == "production":
auth = (app.config["MAIL_USERNAME"],
app.config["MAIL_PASSWORD"])
mail_handler = SMTPHandler(
mailhost=(app.config["MAIL_SERVER"], app.config["MAIL_PORT"]),
fromaddr="no-reply@" + app.config["MAIL_SERVER"],
toaddrs=app.config["ADMINS"], subject="todos_fs API Failure",
credentials=auth, secure=())
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
return app
#pylint: disable=C0413
import server.routes