forked from JulianFrattini/bsv-chef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (51 loc) · 2.05 KB
/
main.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
# coding=utf-8
import os, json
from dotenv import dotenv_values
from flask import Flask, jsonify
from flask_cors import CORS, cross_origin
# import the blueprints which handle the incoming data
from src.blueprints.itemblueprint import item_blueprint
from src.blueprints.recipeblueprint import recipe_blueprint
#from src.util.dao import getDao
# create the Flask application
app = Flask('chef-backend')
# configure CORS for cross-origin resource sharing (between the frontend and backend)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
# register blueprints
app.register_blueprint(blueprint=item_blueprint, url_prefix='/items')
app.register_blueprint(blueprint=recipe_blueprint, url_prefix='/recipes')
from src.util.dao import getDao
from src.controllers.controller import Controller
@app.route('/')
@cross_origin()
def ping():
"""Heartbeat method to check if the server is running and which version is currently active.
returns:
version -- version string of the current backend version"""
VERSION = dotenv_values('.env').get('VERSION')
return jsonify({'version': VERSION}), 200
# simple population method that adds initial data to the database
@app.route('/populate', methods=['POST'])
@cross_origin()
def populate():
itemcontroller = Controller(dao=getDao(collection_name="item"))
added_items: list[str] = []
for filename in os.listdir('./src/static/dummy_items'):
with open(f'./src/static/dummy_items/{filename}') as itemfile:
item = json.load(itemfile)
itemcontroller.create({
"name": item['name'],
"quantity": float(item['quantity']),
"unit": item['unit']
})
added_items.append(item['name'])
return jsonify({"added": added_items}), 200
if __name__ == '__main__':
# print all available REST endpoints
print(app.url_map)
# if a specific FLASK_BIND_IP is given, run the app on that URL
if (os.environ.get('FLASK_BIND_IP')):
app.run(host=os.environ.get('FLASK_BIND_IP'))
else:
app.run()