-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
48 lines (36 loc) · 1.58 KB
/
Copy pathapp.py
File metadata and controls
48 lines (36 loc) · 1.58 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
import datetime, configparser
from flask import Flask, jsonify
from flask_restful import Api
from flask_jwt import JWT
from flask_cors import CORS
from security import authenticate, identity as identity_function
from resources.user import UserRegister
from resources.messages import Message, MessageList
from resources.stats import Stats
config = configparser.ConfigParser()
config.read('config.ini')
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = config['DEFAULT']['SQLALCHEMY_DATABASE_URI']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = config['DEFAULT'].getboolean('SQLALCHEMY_TRACK_MODIFICATIONS')
app.config['JWT_AUTH_URL_RULE'] = config['DEFAULT']['JWT_AUTH_URL_RULE']
app.config['JWT_AUTH_USERNAME_KEY'] = config['DEFAULT']['JWT_AUTH_USERNAME_KEY']
app.config['JWT_EXPIRATION_DELTA'] = datetime.timedelta(days=config.getint('DEFAULT', 'JWT_EXPIRATION_DELTA'))
app.secret_key = config['DEFAULT']['SECRET_KEY']
CORS(app)
api = Api(app)
jwt = JWT(app, authenticate, identity_function)
@jwt.auth_response_handler
def customized_response_handler(access_token, identity):
return jsonify({
'access_token': access_token.decode('utf-8'),
'user_id': identity.id,
'user_email': identity.email
})
api.add_resource(Stats, '/stats/<string:type>')
api.add_resource(Message, '/message/<string:name>')
api.add_resource(MessageList, '/messages')
api.add_resource(UserRegister, '/user/register')
if __name__ == '__main__':
from db import db
db.init_app(app)
app.run(port=config.getint('DEFAULT', 'APP_PORT'), debug=config['DEFAULT'].getboolean('APP_DEBUG'))