-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
41 lines (29 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
from flask import Flask, jsonify
from controllers.home_controller import home_route_handler
from controllers.products_controller import ProductsRouteHandler, ProductRouteHandler
from controllers.categories_controller import CategoryProductsRouteHandler, CategoriesRouteHandler, \
CategoryRouteHandler
from errors.validation_error import ValidationError
from errors.not_found import NotFound
app = Flask(__name__)
# Configure app
app.config.from_pyfile('config.py')
@app.errorhandler(ValidationError)
def handle_validation_error(err):
return jsonify(err=err.args), 400
@app.errorhandler(NotFound)
def handle_not_found_error(err):
return jsonify(err=err.args), 404
app.add_url_rule("/", view_func=home_route_handler)
app.add_url_rule("/api/products", view_func=ProductsRouteHandler.as_view('products_route_handler'),
methods=['GET', 'POST'])
app.add_url_rule("/api/products/<_id>", view_func=ProductRouteHandler.as_view('product_route_handler'),
methods=["GET", "DELETE", "PATCH"])
app.add_url_rule("/api/categories", view_func=CategoriesRouteHandler.as_view('categories_route_handler'),
methods=['GET', 'POST'])
app.add_url_rule("/api/categories/<_id>", view_func=CategoryRouteHandler.as_view('category_route_handler'),
methods=["GET", "DELETE", "PATCH"])
app.add_url_rule("/api/categories/<_id>/products",
view_func=CategoryProductsRouteHandler.as_view('category_products_route_handler'),
methods=["GET"])
app.run(debug=True)