Skip to content

Commit 15ad3cf

Browse files
committed
feat(flask): add flask app & config
edit pytest config
1 parent 3afc79f commit 15ad3cf

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

app.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
""" Packages """
2+
import os
3+
4+
import flask
5+
import flask_cors
6+
from werkzeug.exceptions import HTTPException
7+
8+
from werkzeug.exceptions import HTTPException
9+
10+
from src.opengeodeweb_back.routes import blueprint_routes
11+
12+
13+
""" Global config """
14+
app = flask.Flask(__name__)
15+
16+
""" Config variables """
17+
FLASK_DEBUG = True if os.environ.get("FLASK_DEBUG", default=None) == "True" else False
18+
19+
if FLASK_DEBUG == False:
20+
app.config.from_object("config.ProdConfig")
21+
else:
22+
app.config.from_object("config.DevConfig")
23+
24+
25+
PORT = int(app.config.get("PORT"))
26+
ORIGINS = app.config.get("ORIGINS")
27+
SSL = app.config.get("SSL")
28+
29+
flask_cors.CORS(app, origins=ORIGINS)
30+
app.register_blueprint(
31+
blueprint_routes.routes,
32+
url_prefix="/",
33+
name="blueprint_routes",
34+
)
35+
36+
37+
@app.errorhandler(HTTPException)
38+
def handle_exception(e):
39+
response = e.get_response()
40+
response.data = flask.json.dumps(
41+
{
42+
"code": e.code,
43+
"name": e.name,
44+
"description": e.description,
45+
}
46+
)
47+
response.content_type = "application/json"
48+
return response
49+
50+
51+
# ''' Main '''
52+
if __name__ == "__main__":
53+
print(f"Python is running in {FLASK_DEBUG} mode")
54+
app.run(debug=FLASK_DEBUG, host="0.0.0.0", port=PORT, ssl_context=SSL)

config.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
""" Flask configuration """
2+
import os
3+
4+
5+
class Config(object):
6+
FLASK_DEBUG = os.environ.get("FLASK_DEBUG", default=False)
7+
ID = os.environ.get("ID", default=None)
8+
PORT = "5000"
9+
CORS_HEADERS = "Content-Type"
10+
UPLOAD_FOLDER = "./uploads"
11+
WORKFLOWS_DATA_FOLDER = "./data_workflows/"
12+
LOCK_FOLDER = "./lock/"
13+
TIME_FOLDER = "./time/"
14+
15+
16+
class ProdConfig(Config):
17+
SSL = None
18+
ORIGINS = ["*"]
19+
MINUTES_BEFORE_TIMEOUT = "5"
20+
SECONDS_BETWEEN_SHUTDOWNS = "150"
21+
DATA_FOLDER = "/data/"
22+
23+
24+
class DevConfig(Config):
25+
SSL = None
26+
ORIGINS = "http://localhost:3000"
27+
MINUTES_BEFORE_TIMEOUT = "1000"
28+
SECONDS_BETWEEN_SHUTDOWNS = "60"
29+
DATA_FOLDER = "./data/"

tests/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
11
import pytest
2+
from app import app
3+
4+
5+
@pytest.fixture
6+
def client():
7+
app.config["TESTING"] = True
8+
app.config["SERVER_NAME"] = "TEST"
9+
app.config["DATA_FOLDER"] = "./data/"
10+
client = app.test_client()
11+
client.headers = {"Content-type": "application/json", "Accept": "application/json"}
12+
yield client

0 commit comments

Comments
 (0)