-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
90 lines (74 loc) · 1.7 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Imports
import os
from flask import Flask, request, jsonify
from flask_cors import CORS
from server.utils.flask.request import request_body
from server.utils.pathfinding.optimal_path import coordinates, optimal_path
from server.data import MATRIX
# Constants
STATIC_FOLDER = "build"
STATIC_URL_PATH = ""
ORIGINS = [
"http://localhost:3000",
"https://deimos-y.web.app",
"http://deimos-y.web.app",
]
PORT = os.environ.get("PORT", 5000)
HOST = "0.0.0.0"
DEBUG = PORT == 5000
# Initializations
# TODO: Set static_folder and static_url_path as environment variables
app = Flask(
__name__,
static_folder=STATIC_FOLDER,
static_url_path=STATIC_URL_PATH,
)
# TODO: Set origins list items as environment variables
CORS(
app,
resources={
r"/api/*": {
"origins": ORIGINS,
},
},
)
# Routes
@app.route(
"/",
methods=["GET"],
)
def index():
return app.send_static_file("index.html")
@app.route(
"/favicon.ico",
methods=["GET"],
)
def favicon():
return app.send_static_file("favicon.ico")
@app.route(
"/api/paths",
methods=["POST"],
)
def paths():
try:
body = request_body(request)
# TODO: Refactor coordinates invocation into optimal_path
start, end = coordinates(body)
path = optimal_path(
start,
end,
MATRIX,
)
except Exception as error:
# TODO: Create proper error handling and logging
print(error)
path = start, end
finally:
return jsonify(path), 201
if __name__ == "__main__":
# TODO: Set host, port, and debug as environment variables
app.run(
host=HOST,
port=PORT,
debug=DEBUG,
)