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