Skip to content

Commit 35cf6e6

Browse files
Merge pull request #89 from Geode-solutions/feat/ping_route
Feat/ping route
2 parents 1676c23 + 284b98b commit 35cf6e6

File tree

10 files changed

+282
-195
lines changed

10 files changed

+282
-195
lines changed

app.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from werkzeug.exceptions import HTTPException
88

99
from src.opengeodeweb_back.routes import blueprint_routes
10-
from src.opengeodeweb_back.geode_functions import handle_exception
10+
from src.opengeodeweb_back.utils_functions import handle_exception
11+
from src.opengeodeweb_back import app_config
1112

1213

1314
""" Global config """
@@ -17,12 +18,11 @@
1718
FLASK_DEBUG = True if os.environ.get("FLASK_DEBUG", default=None) == "True" else False
1819

1920
if FLASK_DEBUG == False:
20-
app.config.from_object("config.ProdConfig")
21+
app.config.from_object(app_config.ProdConfig)
2122
else:
22-
app.config.from_object("config.DevConfig")
23+
app.config.from_object(app_config.DevConfig)
2324

24-
25-
PORT = int(app.config.get("PORT"))
25+
PORT = int(app.config.get("DEFAULT_PORT"))
2626
ORIGINS = app.config.get("ORIGINS")
2727
SSL = app.config.get("SSL")
2828

config.py

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Standard library imports
2+
import os
3+
import time
4+
5+
# Third party imports
6+
# Local application imports
7+
8+
9+
class Config(object):
10+
FLASK_DEBUG = os.environ.get("FLASK_DEBUG", default=False)
11+
DEFAULT_PORT = "5000"
12+
CORS_HEADERS = "Content-Type"
13+
UPLOAD_FOLDER = "./uploads"
14+
DESKTOP_APP = False
15+
REQUEST_COUNTER = 0
16+
LAST_REQUEST_TIME = time.time()
17+
LAST_PING_TIME = time.time()
18+
19+
20+
class ProdConfig(Config):
21+
SSL = None
22+
ORIGINS = ""
23+
MINUTES_BEFORE_TIMEOUT = "1"
24+
SECONDS_BETWEEN_SHUTDOWNS = "10"
25+
DATA_FOLDER_PATH = "/data/"
26+
27+
28+
class DevConfig(Config):
29+
SSL = None
30+
ORIGINS = "*"
31+
MINUTES_BEFORE_TIMEOUT = "1"
32+
SECONDS_BETWEEN_SHUTDOWNS = "10"
33+
DATA_FOLDER_PATH = "./data/"

src/opengeodeweb_back/geode_functions.py

Lines changed: 4 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
# Standard library imports
22
import os
3-
import time
4-
import threading
5-
import uuid
6-
import zipfile
73

84
# Third party imports
95
import flask
106
import opengeode_geosciences as og_gs
117
import opengeode as og
12-
import pkg_resources
13-
from jsonschema import validate
14-
from jsonschema.exceptions import ValidationError
158

169
# Local application imports
1710
from .geode_objects import geode_objects_dict
11+
from . import utils_functions
1812

1913

2014
def geode_object_value(geode_object: str):
@@ -147,7 +141,9 @@ def list_geode_objects(
147141
key: str = None,
148142
):
149143
return_dict = {}
150-
file_extension = extension_from_filename(os.path.basename(file_absolute_path))
144+
file_extension = utils_functions.extension_from_filename(
145+
os.path.basename(file_absolute_path)
146+
)
151147
geode_objects_filtered_list = filter_geode_objects(key)
152148

153149
for geode_object in geode_objects_filtered_list:
@@ -200,75 +196,6 @@ def get_inspector_children(obj):
200196
return new_object
201197

202198

203-
def versions(list_packages: list):
204-
list_with_versions = []
205-
for package in list_packages:
206-
list_with_versions.append(
207-
{
208-
"package": package,
209-
"version": pkg_resources.get_distribution(package).version,
210-
}
211-
)
212-
return list_with_versions
213-
214-
215-
def create_lock_file(
216-
folder_absolute_path,
217-
):
218-
if not os.path.exists(folder_absolute_path):
219-
os.mkdir(folder_absolute_path)
220-
id = uuid.uuid4()
221-
file_absolute_path = f"{folder_absolute_path}/{str(id)}.txt"
222-
f = open(file_absolute_path, "a")
223-
f.close()
224-
flask.g.UUID = id
225-
226-
227-
def create_time_file(folder_absolute_path):
228-
if not os.path.exists(folder_absolute_path):
229-
os.mkdir(folder_absolute_path)
230-
file_path = f"{folder_absolute_path}/time.txt"
231-
if not os.path.isfile(file_path):
232-
f = open(file_path, "w")
233-
f.close()
234-
235-
f = open(folder_absolute_path + "/time.txt", "w")
236-
f.write(str(time.time()))
237-
f.close()
238-
239-
240-
def remove_lock_file(folder_absolute_path):
241-
id = flask.g.UUID
242-
os.remove(f"{folder_absolute_path}/{str(id)}.txt")
243-
244-
245-
def set_interval(func, sec):
246-
def func_wrapper():
247-
set_interval(func, sec)
248-
func()
249-
250-
t = threading.Timer(sec, func_wrapper)
251-
t.daemon = True
252-
t.start()
253-
return t
254-
255-
256-
def extension_from_filename(filename):
257-
return os.path.splitext(filename)[1][1:]
258-
259-
260-
def validate_request(request, schema):
261-
json_data = request.get_json(force=True, silent=True)
262-
263-
if json_data is None:
264-
json_data = {}
265-
266-
try:
267-
validate(instance=json_data, schema=schema)
268-
except ValidationError as e:
269-
flask.abort(400, f"Validation error: {e.message}")
270-
271-
272199
def geographic_coordinate_systems(geode_object: str):
273200
if is_3D(geode_object):
274201
return og_gs.GeographicCoordinateSystem3D.geographic_coordinate_systems()
@@ -329,41 +256,3 @@ def create_coordinate_system(
329256
create_crs(
330257
geode_object, data, name, input_coordiante_system, output_coordiante_system
331258
)
332-
333-
334-
def send_file(upload_folder, saved_files, new_file_name):
335-
if len(saved_files) == 1:
336-
mimetype = "application/octet-binary"
337-
else:
338-
mimetype = "application/zip"
339-
new_file_name = os.path.splitext(new_file_name)[0] + ".zip"
340-
with zipfile.ZipFile(os.path.join(upload_folder, new_file_name), "w") as zipObj:
341-
for saved_file_path in saved_files:
342-
zipObj.write(
343-
saved_file_path,
344-
os.path.basename(saved_file_path),
345-
)
346-
347-
response = flask.send_from_directory(
348-
directory=upload_folder,
349-
path=new_file_name,
350-
as_attachment=True,
351-
mimetype=mimetype,
352-
)
353-
response.headers["new-file-name"] = new_file_name
354-
response.headers["Access-Control-Expose-Headers"] = "new-file-name"
355-
356-
return response
357-
358-
359-
def handle_exception(e):
360-
response = e.get_response()
361-
response.data = flask.json.dumps(
362-
{
363-
"code": e.code,
364-
"name": e.name,
365-
"description": e.description,
366-
}
367-
)
368-
response.content_type = "application/json"
369-
return response

src/opengeodeweb_back/routes/blueprint_routes.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
# Standard library imports
22
import json
33
import os
4+
import time
45

56
# Third party imports
67
import flask
7-
import flask_cors
8-
from .. import geode_functions
8+
from .. import geode_functions, utils_functions
99
import werkzeug
1010
import uuid
1111

12-
1312
routes = flask.Blueprint("routes", __name__)
14-
flask_cors.CORS(routes)
13+
14+
15+
@routes.before_request
16+
def before_request():
17+
if "ping" not in flask.request.path:
18+
utils_functions.increment_request_counter(flask.current_app)
19+
20+
21+
@routes.teardown_request
22+
def teardown_request(exception):
23+
if "ping" not in flask.request.path:
24+
utils_functions.decrement_request_counter(flask.current_app)
25+
utils_functions.update_last_request_time(flask.current_app)
1526

1627

1728
schemas = os.path.join(os.path.dirname(__file__), "schemas")
@@ -28,7 +39,7 @@
2839
methods=allowed_files_json["methods"],
2940
)
3041
def allowed_files():
31-
geode_functions.validate_request(flask.request, allowed_files_json)
42+
utils_functions.validate_request(flask.request, allowed_files_json)
3243
extensions = geode_functions.list_input_extensions(
3344
flask.request.json["supported_feature"]
3445
)
@@ -75,7 +86,7 @@ def allowed_objects():
7586
return flask.make_response({}, 200)
7687

7788
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
78-
geode_functions.validate_request(flask.request, allowed_objects_json)
89+
utils_functions.validate_request(flask.request, allowed_objects_json)
7990
file_absolute_path = os.path.join(UPLOAD_FOLDER, flask.request.json["filename"])
8091
allowed_objects = geode_functions.list_geode_objects(
8192
file_absolute_path, flask.request.json["supported_feature"]
@@ -96,7 +107,7 @@ def allowed_objects():
96107
)
97108
def missing_files():
98109
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
99-
geode_functions.validate_request(flask.request, missing_files_json)
110+
utils_functions.validate_request(flask.request, missing_files_json)
100111

101112
missing_files = geode_functions.missing_files(
102113
flask.request.json["input_geode_object"],
@@ -134,13 +145,11 @@ def missing_files():
134145
methods=geographic_coordinate_systems_json["methods"],
135146
)
136147
def crs_converter_geographic_coordinate_systems():
137-
geode_functions.validate_request(flask.request, geographic_coordinate_systems_json)
148+
utils_functions.validate_request(flask.request, geographic_coordinate_systems_json)
138149
infos = geode_functions.geographic_coordinate_systems(
139150
flask.request.json["input_geode_object"]
140151
)
141152
crs_list = []
142-
print(infos)
143-
print(flask.request.json["input_geode_object"])
144153
for info in infos:
145154
crs = {}
146155
crs["name"] = info.name
@@ -164,7 +173,7 @@ def crs_converter_geographic_coordinate_systems():
164173
)
165174
def inspect_file():
166175
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
167-
geode_functions.validate_request(flask.request, inspect_file_json)
176+
utils_functions.validate_request(flask.request, inspect_file_json)
168177

169178
secure_filename = werkzeug.utils.secure_filename(flask.request.json["filename"])
170179
file_path = os.path.abspath(os.path.join(UPLOAD_FOLDER, secure_filename))
@@ -189,7 +198,7 @@ def inspect_file():
189198
)
190199
def geode_objects_and_output_extensions():
191200
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
192-
geode_functions.validate_request(
201+
utils_functions.validate_request(
193202
flask.request, geode_objects_and_output_extensions_json
194203
)
195204
data = geode_functions.load(
@@ -221,7 +230,7 @@ def geode_objects_and_output_extensions():
221230
def save_viewable_file():
222231
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
223232
DATA_FOLDER_PATH = flask.current_app.config["DATA_FOLDER_PATH"]
224-
geode_functions.validate_request(flask.request, save_viewable_file_json)
233+
utils_functions.validate_request(flask.request, save_viewable_file_json)
225234

226235
secure_filename = werkzeug.utils.secure_filename(flask.request.json["filename"])
227236
file_path = os.path.abspath(os.path.join(UPLOAD_FOLDER, secure_filename))
@@ -260,3 +269,20 @@ def save_viewable_file():
260269
},
261270
200,
262271
)
272+
273+
274+
with open(
275+
os.path.join(schemas, "ping.json"),
276+
"r",
277+
) as file:
278+
ping_json = json.load(file)
279+
280+
281+
@routes.route(
282+
ping_json["route"],
283+
methods=ping_json["methods"],
284+
)
285+
def ping():
286+
utils_functions.validate_request(flask.request, ping_json)
287+
flask.current_app.config.update(LAST_PING_TIME=time.time())
288+
return flask.make_response({"message": "Flask server is running"}, 200)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"route": "/ping",
3+
"methods": [
4+
"POST"
5+
],
6+
"type": "object",
7+
"properties": {},
8+
"required": [],
9+
"additionalProperties": false
10+
}

0 commit comments

Comments
 (0)