-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
71 lines (50 loc) · 1.52 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
import dotenv
import json
import dbOperation
import helper
from os import getenv
from flask import Flask, jsonify, request
from bson.json_util import dumps
from middleWare import *
from db import *
from userUtils import *
from flask_cors import CORS
from flask_jwt_extended import (
JWTManager, jwt_required, create_access_token,
get_jwt_identity
)
# end and final classes are exception class but they meant to
# be used as goto statement for cleaner code
class end(Exception):
pass
class final(Exception):
pass
# Load environment variables
dotenv.load_dotenv(dotenv_path='./.env')
app = Flask(__name__)
CORS(app)
app.config['JWT_SECRET_KEY'] = getenv('SECRET_KEY')
jwt = JWTManager(app)
# Setup Database
app.DB = Mongo(getenv('DB_URI_STRING'),getenv('DB_NAME'))
# Importing other routes
import routes.mainRoutes
import routes.hostRoutes
import routes.serverRoutes
import routes.userAdminRoutes
import routes.explorerRoutes
# Example of protected Route
@app.route('/protected', methods=['GET'])
@jwt_required
def protected():
# Access the identity of the current user with get_jwt_identity
current_user = get_jwt_identity()
return jsonify(logged_in_as=current_user), 200
# Where server starts
if __name__ == '__main__':
# setup server vars
DEBUG = True if getenv('TYPE') == 'dev' else False
# Run server
app.run(debug = DEBUG, host = '0.0.0.0', port = getenv('PORT'), load_dotenv = True)
def create_app():
return app