Skip to content

Commit

Permalink
Merge pull request #89 from Geode-solutions/feat/ping_route
Browse files Browse the repository at this point in the history
Feat/ping route
  • Loading branch information
JulienChampagnol authored Aug 19, 2024
2 parents 1676c23 + 284b98b commit 35cf6e6
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 195 deletions.
10 changes: 5 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from werkzeug.exceptions import HTTPException

from src.opengeodeweb_back.routes import blueprint_routes
from src.opengeodeweb_back.geode_functions import handle_exception
from src.opengeodeweb_back.utils_functions import handle_exception
from src.opengeodeweb_back import app_config


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

if FLASK_DEBUG == False:
app.config.from_object("config.ProdConfig")
app.config.from_object(app_config.ProdConfig)
else:
app.config.from_object("config.DevConfig")
app.config.from_object(app_config.DevConfig)


PORT = int(app.config.get("PORT"))
PORT = int(app.config.get("DEFAULT_PORT"))
ORIGINS = app.config.get("ORIGINS")
SSL = app.config.get("SSL")

Expand Down
31 changes: 0 additions & 31 deletions config.py

This file was deleted.

33 changes: 33 additions & 0 deletions src/opengeodeweb_back/app_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Standard library imports
import os
import time

# Third party imports
# Local application imports


class Config(object):
FLASK_DEBUG = os.environ.get("FLASK_DEBUG", default=False)
DEFAULT_PORT = "5000"
CORS_HEADERS = "Content-Type"
UPLOAD_FOLDER = "./uploads"
DESKTOP_APP = False
REQUEST_COUNTER = 0
LAST_REQUEST_TIME = time.time()
LAST_PING_TIME = time.time()


class ProdConfig(Config):
SSL = None
ORIGINS = ""
MINUTES_BEFORE_TIMEOUT = "1"
SECONDS_BETWEEN_SHUTDOWNS = "10"
DATA_FOLDER_PATH = "/data/"


class DevConfig(Config):
SSL = None
ORIGINS = "*"
MINUTES_BEFORE_TIMEOUT = "1"
SECONDS_BETWEEN_SHUTDOWNS = "10"
DATA_FOLDER_PATH = "./data/"
119 changes: 4 additions & 115 deletions src/opengeodeweb_back/geode_functions.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
# Standard library imports
import os
import time
import threading
import uuid
import zipfile

# Third party imports
import flask
import opengeode_geosciences as og_gs
import opengeode as og
import pkg_resources
from jsonschema import validate
from jsonschema.exceptions import ValidationError

# Local application imports
from .geode_objects import geode_objects_dict
from . import utils_functions


def geode_object_value(geode_object: str):
Expand Down Expand Up @@ -147,7 +141,9 @@ def list_geode_objects(
key: str = None,
):
return_dict = {}
file_extension = extension_from_filename(os.path.basename(file_absolute_path))
file_extension = utils_functions.extension_from_filename(
os.path.basename(file_absolute_path)
)
geode_objects_filtered_list = filter_geode_objects(key)

for geode_object in geode_objects_filtered_list:
Expand Down Expand Up @@ -200,75 +196,6 @@ def get_inspector_children(obj):
return new_object


def versions(list_packages: list):
list_with_versions = []
for package in list_packages:
list_with_versions.append(
{
"package": package,
"version": pkg_resources.get_distribution(package).version,
}
)
return list_with_versions


def create_lock_file(
folder_absolute_path,
):
if not os.path.exists(folder_absolute_path):
os.mkdir(folder_absolute_path)
id = uuid.uuid4()
file_absolute_path = f"{folder_absolute_path}/{str(id)}.txt"
f = open(file_absolute_path, "a")
f.close()
flask.g.UUID = id


def create_time_file(folder_absolute_path):
if not os.path.exists(folder_absolute_path):
os.mkdir(folder_absolute_path)
file_path = f"{folder_absolute_path}/time.txt"
if not os.path.isfile(file_path):
f = open(file_path, "w")
f.close()

f = open(folder_absolute_path + "/time.txt", "w")
f.write(str(time.time()))
f.close()


def remove_lock_file(folder_absolute_path):
id = flask.g.UUID
os.remove(f"{folder_absolute_path}/{str(id)}.txt")


def set_interval(func, sec):
def func_wrapper():
set_interval(func, sec)
func()

t = threading.Timer(sec, func_wrapper)
t.daemon = True
t.start()
return t


def extension_from_filename(filename):
return os.path.splitext(filename)[1][1:]


def validate_request(request, schema):
json_data = request.get_json(force=True, silent=True)

if json_data is None:
json_data = {}

try:
validate(instance=json_data, schema=schema)
except ValidationError as e:
flask.abort(400, f"Validation error: {e.message}")


def geographic_coordinate_systems(geode_object: str):
if is_3D(geode_object):
return og_gs.GeographicCoordinateSystem3D.geographic_coordinate_systems()
Expand Down Expand Up @@ -329,41 +256,3 @@ def create_coordinate_system(
create_crs(
geode_object, data, name, input_coordiante_system, output_coordiante_system
)


def send_file(upload_folder, saved_files, new_file_name):
if len(saved_files) == 1:
mimetype = "application/octet-binary"
else:
mimetype = "application/zip"
new_file_name = os.path.splitext(new_file_name)[0] + ".zip"
with zipfile.ZipFile(os.path.join(upload_folder, new_file_name), "w") as zipObj:
for saved_file_path in saved_files:
zipObj.write(
saved_file_path,
os.path.basename(saved_file_path),
)

response = flask.send_from_directory(
directory=upload_folder,
path=new_file_name,
as_attachment=True,
mimetype=mimetype,
)
response.headers["new-file-name"] = new_file_name
response.headers["Access-Control-Expose-Headers"] = "new-file-name"

return response


def handle_exception(e):
response = e.get_response()
response.data = flask.json.dumps(
{
"code": e.code,
"name": e.name,
"description": e.description,
}
)
response.content_type = "application/json"
return response
52 changes: 39 additions & 13 deletions src/opengeodeweb_back/routes/blueprint_routes.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
# Standard library imports
import json
import os
import time

# Third party imports
import flask
import flask_cors
from .. import geode_functions
from .. import geode_functions, utils_functions
import werkzeug
import uuid


routes = flask.Blueprint("routes", __name__)
flask_cors.CORS(routes)


@routes.before_request
def before_request():
if "ping" not in flask.request.path:
utils_functions.increment_request_counter(flask.current_app)


@routes.teardown_request
def teardown_request(exception):
if "ping" not in flask.request.path:
utils_functions.decrement_request_counter(flask.current_app)
utils_functions.update_last_request_time(flask.current_app)


schemas = os.path.join(os.path.dirname(__file__), "schemas")
Expand All @@ -28,7 +39,7 @@
methods=allowed_files_json["methods"],
)
def allowed_files():
geode_functions.validate_request(flask.request, allowed_files_json)
utils_functions.validate_request(flask.request, allowed_files_json)
extensions = geode_functions.list_input_extensions(
flask.request.json["supported_feature"]
)
Expand Down Expand Up @@ -75,7 +86,7 @@ def allowed_objects():
return flask.make_response({}, 200)

UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
geode_functions.validate_request(flask.request, allowed_objects_json)
utils_functions.validate_request(flask.request, allowed_objects_json)
file_absolute_path = os.path.join(UPLOAD_FOLDER, flask.request.json["filename"])
allowed_objects = geode_functions.list_geode_objects(
file_absolute_path, flask.request.json["supported_feature"]
Expand All @@ -96,7 +107,7 @@ def allowed_objects():
)
def missing_files():
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
geode_functions.validate_request(flask.request, missing_files_json)
utils_functions.validate_request(flask.request, missing_files_json)

missing_files = geode_functions.missing_files(
flask.request.json["input_geode_object"],
Expand Down Expand Up @@ -134,13 +145,11 @@ def missing_files():
methods=geographic_coordinate_systems_json["methods"],
)
def crs_converter_geographic_coordinate_systems():
geode_functions.validate_request(flask.request, geographic_coordinate_systems_json)
utils_functions.validate_request(flask.request, geographic_coordinate_systems_json)
infos = geode_functions.geographic_coordinate_systems(
flask.request.json["input_geode_object"]
)
crs_list = []
print(infos)
print(flask.request.json["input_geode_object"])
for info in infos:
crs = {}
crs["name"] = info.name
Expand All @@ -164,7 +173,7 @@ def crs_converter_geographic_coordinate_systems():
)
def inspect_file():
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
geode_functions.validate_request(flask.request, inspect_file_json)
utils_functions.validate_request(flask.request, inspect_file_json)

secure_filename = werkzeug.utils.secure_filename(flask.request.json["filename"])
file_path = os.path.abspath(os.path.join(UPLOAD_FOLDER, secure_filename))
Expand All @@ -189,7 +198,7 @@ def inspect_file():
)
def geode_objects_and_output_extensions():
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
geode_functions.validate_request(
utils_functions.validate_request(
flask.request, geode_objects_and_output_extensions_json
)
data = geode_functions.load(
Expand Down Expand Up @@ -221,7 +230,7 @@ def geode_objects_and_output_extensions():
def save_viewable_file():
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
DATA_FOLDER_PATH = flask.current_app.config["DATA_FOLDER_PATH"]
geode_functions.validate_request(flask.request, save_viewable_file_json)
utils_functions.validate_request(flask.request, save_viewable_file_json)

secure_filename = werkzeug.utils.secure_filename(flask.request.json["filename"])
file_path = os.path.abspath(os.path.join(UPLOAD_FOLDER, secure_filename))
Expand Down Expand Up @@ -260,3 +269,20 @@ def save_viewable_file():
},
200,
)


with open(
os.path.join(schemas, "ping.json"),
"r",
) as file:
ping_json = json.load(file)


@routes.route(
ping_json["route"],
methods=ping_json["methods"],
)
def ping():
utils_functions.validate_request(flask.request, ping_json)
flask.current_app.config.update(LAST_PING_TIME=time.time())
return flask.make_response({"message": "Flask server is running"}, 200)
10 changes: 10 additions & 0 deletions src/opengeodeweb_back/routes/schemas/ping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"route": "/ping",
"methods": [
"POST"
],
"type": "object",
"properties": {},
"required": [],
"additionalProperties": false
}
Loading

0 comments on commit 35cf6e6

Please sign in to comment.