-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
165 lines (126 loc) · 4.48 KB
/
app.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from dotenv import load_dotenv
load_dotenv()
from flask import Flask, request
from flask_restful import Api
from flask_cors import CORS
from flask import render_template
import os
from models import db, User, ApiNavigator
from views import bookmarks, comments, followers, following, \
posts, profile, stories, suggestions, post_likes
# new import statements:
import flask_jwt_extended
import decorators
# new views:
from views import authentication, token
app = Flask(__name__)
# CORS: allows anyone from anywhere to use your API:
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DB_URL')
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
#JWT config variables and manager (add after app object created):
app.config["JWT_SECRET_KEY"] = os.environ.get('JWT_SECRET')
app.config["JWT_TOKEN_LOCATION"] = ["headers", "cookies"]
app.config["JWT_COOKIE_SECURE"] = False
jwt = flask_jwt_extended.JWTManager(app)
db.init_app(app)
api = Api(app)
# Initialize routes of 2 new views
authentication.initialize_routes(app)
token.initialize_routes(api)
# defines the function for retrieving a user from the database
@jwt.user_lookup_loader
def user_lookup_callback(_jwt_header, jwt_data):
# print('JWT data:', jwt_data)
# https://flask-jwt-extended.readthedocs.io/en/stable/automatic_user_loading/
user_id = jwt_data["sub"]
return User.query.filter_by(id=user_id).one_or_none()
# set logged in user
with app.app_context():
app.current_user = User.query.filter_by(id=12).one()
# Initialize routes for all of your API endpoints:
bookmarks.initialize_routes(api)
comments.initialize_routes(api)
followers.initialize_routes(api)
following.initialize_routes(api)
posts.initialize_routes(api)
post_likes.initialize_routes(api)
profile.initialize_routes(api)
stories.initialize_routes(api)
suggestions.initialize_routes(api)
# Server-side template for the homepage:
@app.route('/')
@decorators.jwt_or_login
def home():
return render_template(
'starter-client.html',
user=flask_jwt_extended.current_user
)
# Updated API endpoint includes a reference to
# access_token and csrf token.
@app.route('/api')
@decorators.jwt_or_login
def api_docs():
access_token = request.cookies.get('access_token_cookie')
csrf = request.cookies.get('csrf_access_token')
navigator = ApiNavigator(flask_jwt_extended.current_user)
return render_template(
'api/api-docs.html',
user=flask_jwt_extended.current_user, #TODO: change to flask_jwt_extended.current_user
endpoints=navigator.get_endpoints(),
access_token=access_token,
csrf=csrf,
url_root=request.url_root[0:-1] # trim trailing slash
)
# enables flask app to run using "python3 app.py"
if __name__ == '__main__':
app.run()
# from dotenv import load_dotenv
# load_dotenv()
# from flask import Flask, request
# from flask_restful import Api
# from flask_cors import CORS
# from flask import render_template
# import os
# from models import db, User, ApiNavigator
# from views import bookmarks, comments, followers, following, \
# posts, profile, stories, suggestions, post_likes
# app = Flask(__name__)
# # CORS: allows anyone from anywhere to use your API:
# cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
# app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DB_URL')
# app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# db.init_app(app)
# api = Api(app)
# # set logged in user
# with app.app_context():
# app.current_user = User.query.filter_by(id=12).one()
# # Initialize routes for all of your API endpoints:
# bookmarks.initialize_routes(api)
# comments.initialize_routes(api)
# followers.initialize_routes(api)
# following.initialize_routes(api)
# posts.initialize_routes(api)
# post_likes.initialize_routes(api)
# profile.initialize_routes(api)
# stories.initialize_routes(api)
# suggestions.initialize_routes(api)
# # Server-side template for the homepage:
# @app.route('/')
# def home():
# return render_template(
# 'starter-client.html',
# user=app.current_user
# )
# @app.route('/api')
# def api_docs():
# navigator = ApiNavigator(app.current_user)
# return render_template(
# 'api/api-docs.html',
# user=app.current_user,
# endpoints=navigator.get_endpoints(),
# url_root=request.url_root[0:-1] # trim trailing slash
# )
# # enables flask app to run using "python3 app.py"
# if __name__ == '__main__':
# app.run()