Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dts-validator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
# Flask Server
- name: Start Flask server
run: |
python -m dapytains.app.app &
python -m dapytains.app.wsgi &
- name: Wait for Flask server to start
run: sleep 5 # Adjust as needed

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ WORKDIR /app

RUN make install

ENTRYPOINT exec python -m dapytains.app.app
ENTRYPOINT exec make start
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on this point , i want to note that i don't think it's an urgent change at this stage
also i haven't personnaly used make as entrypoints , but what i'm reading is that it hides the gunicorn process behind make , and for prod it can be problematic for signals in general and shutdowns

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imho there is no problem; make handles signals very well. The goal here is to start the server using the dotenv variables. Without make we would have to create a start script that would duplicate the logic already present in the makefile.

18 changes: 11 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ include .env.local

COMPOSE_FILE ?= compose.yaml
DOCKER_COMPOSE ?= docker compose -f $(COMPOSE_FILE)
GUNICORN ?= gunicorn
PIP ?= pip
PYTEST ?= pytest
PYTHON ?= python
SERVER_ENV ?= prod
SERVER_HOST ?= 0.0.0.0
SERVER_PORT ?= 5000
VERBOSE ?= 0

COLOR_SUPPORT ?= $(shell [ "$$(tput colors 2>/dev/null)" -ge 8 ] && echo 1 || echo 0)
Expand Down Expand Up @@ -40,7 +43,7 @@ help:

install:
$(PIP) install -r requirements.txt
ifneq ($(SERVER_ENV),"prod")
ifneq ("$(SERVER_ENV)","prod")
$(PIP) install -r requirements-dev.txt
endif

Expand All @@ -50,16 +53,17 @@ ifeq ($(VERBOSE),1)
else
@$(MAKE) install > /dev/null
endif
$(PYTHON) -m dapytains.app.app
ifeq ("$(SERVER_ENV)","prod")
$(GUNICORN) -b $(SERVER_HOST):$(SERVER_PORT) dapytains.app.wsgi:app
else
$(PYTHON) -m dapytains.app.wsgi
endif

test:
ifeq ($(SERVER_ENV),"prod")
$(error "Tests cannot run in 'prod' environment")
endif
ifeq ($(VERBOSE),1)
$(MAKE) install
$(MAKE) SERVER_ENV=test install
else
@$(MAKE) install > /dev/null
@$(MAKE) SERVER_ENV=test install > /dev/null
endif
$(PYTEST) --doctest-modules --cov=dapytains --verbose

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This library will:

## WebApp

You can try the webapp using `python -m dapitains.app.app`. It uses test files at the moment.
You can try the webapp using `python -m dapytains.app.wsgi`. It uses test files at the moment.

## Guidelines

Expand Down
31 changes: 1 addition & 30 deletions dapytains/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@

import json
import lxml.etree as ET
import os
from dapytains.tei.document import Document
from dapytains.errors import InvalidRangeOrder
from dapytains.app.database import db, Collection, Navigation
from dapytains.app.navigation import get_nav, get_member_by_path
from dapytains.app.transformer import Transformer, GeneralisticXSLTransformer
from dotenv_flow import dotenv_flow

dotenv_flow(os.getenv("SERVER_ENV", "prod"))


def inject_json(collection: Collection, templates) -> Dict:
if collection.resource:
Expand Down Expand Up @@ -271,28 +266,4 @@ def document_route():

return app, db


if __name__ == "__main__":
import os
from dapytains.app.ingest import store_catalog
from dapytains.metadata.xml_parser import parse

app = Flask(__name__)
_, db = create_app(app)

app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("DATABASE_URI", "sqlite:///../app.db")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db.init_app(app)
with app.app_context():
db.drop_all()
db.create_all()

catalog, _ = parse(os.getenv("DTSCATALOG", "tests/catalog/example-collection.xml"))
store_catalog(catalog)

if "prod" != os.getenv("SERVER_ENV", "prod"):
app.run(debug=True, host=os.getenv("SERVER_HOST", "0.0.0.0"), port=os.getenv("SERVER_PORT", 5000))
else:
from waitress import serve
serve(app, host=os.getenv("SERVER_HOST", "0.0.0.0"), port=os.getenv("SERVER_PORT", 5000))
app, db = create_app(Flask(__name__))
21 changes: 21 additions & 0 deletions dapytains/app/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
from dapytains.app.app import app, db
from dapytains.app.ingest import store_catalog
from dapytains.metadata.xml_parser import parse
from dotenv_flow import dotenv_flow

dotenv_flow(os.getenv("SERVER_ENV", "prod"))

if __name__ == "__main__":
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv("DATABASE_URI", "sqlite:///../app.db")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db.init_app(app)
with app.app_context():
db.drop_all()
db.create_all()
Comment on lines +15 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i may be wrong here , but wouldn't it be better to conditionnaly control whenever to drop all and recreate the db or not with a flag/arugment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely agree with that comment, but that's not really the purpose of this pull request. This logic should probably be extracted into a command, but I would prefer that to be done in another pull request.


catalog, _ = parse(os.getenv("DTSCATALOG", "tests/catalog/example-collection.xml"))
store_catalog(catalog)

app.run(debug=("prod" != os.getenv("SERVER_ENV", "prod")), host=os.getenv("SERVER_HOST", "0.0.0.0"), port=os.getenv("SERVER_PORT", 5000))
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ click
uritemplate
tqdm
dotenv_flow
waitress
gunicorn