· 5 years ago · Dec 06, 2020, 10:56 PM
1import os
2from flask import Flask, send_from_directory
3from flask_restful import Api
4from resources import *
5from models.user import UserModel
6
7# ============================================================================================
8# =================================================================== INITIALIZATION =========
9# ============================================================================================
10
11# Create a new Flask application
12app = Flask(__name__)
13
14def authenticate(username, password):
15 #in this method you should return user model after you compare username and password
16 user = username_table.get(username, None)
17 if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')):
18 return user
19
20def identity(payload):
21 # here you should also return user where identity is user_id
22 user_id = payload['identity']
23 return userid_table.get(user_id, None)
24
25jwt = JWT(app, authenticate, identity)
26
27# Add a secret key. Important for hashing (e.g. with login handling)
28app.secret_key = os.getenv('SECRET')
29# Create a new API
30api = Api(app, prefix=os.getenv('API_PREFIX'))
31
32
33# ============================================================================================
34# =============================================================== FRONTEND REDIRECTS =========
35# ============================================================================================
36
37@app.route('/')
38def index():
39 """Redirects the base url '/' to the webshop/index.html file
40
41 Returns:
42 string: file content
43 """
44 return send_from_directory('../webshop', 'index.html')
45
46@app.route('/<path:path>')
47def shop(path):
48 """Redirects all requests to the front end (webshop)
49
50 Args:
51 path (path): path to requested file
52
53 Returns:
54 string: file content
55 """
56 return send_from_directory('../webshop', path)
57
58# ============================================================================================
59# =================================================================== ERROR HANDLING =========
60# ============================================================================================
61
62@app.errorhandler(404)
63def page_not_found(e):
64 """Returns a JSON on 404
65
66 Returns:
67 object: error message
68 """
69 return {'message': 'resource not found'}, 404
70
71# ============================================================================================
72# ======================================================================== ENDPOINTS =========
73# ============================================================================================
74
75api.add_resource(Categories, '/api/categories')
76api.add_resource(Products, '/api/categories/<category_id>/products')
77api.add_resource(Product, '/api/products/<product_id>')
78api.add_resource(ProductSearch, '/api/products')
79api.add_resource(CreateUser, '/api/users')
80api.add_resource(Country, '/api/countries')
81api.add_resource(Authenticate, '/api/auth')
82
83# ============================================================================================
84# ================================================================ START APPLICATION =========
85# ============================================================================================
86
87if __name__ == '__main__':
88 app.run(debug=True)