Skip to content

bundle docs in the server #618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
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
12 changes: 8 additions & 4 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/.git
/delphi-epidata
/.mypy_cache
/build
/.github
/docs
__pycache__
/node_modules
**/__pycache__
**/.pytest_cache
**/.mypy_cache
**/node_modules
/.env
/.env.example
13 changes: 12 additions & 1 deletion devops/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ WORKDIR /src
COPY . /src
RUN npm ci && npm run build

FROM jekyll/jekyll:stable as docs_builder
# see also https://github.com/envygeeks/jekyll-docker/issues/280
RUN mkdir /src; \
chown -R jekyll:jekyll /src
WORKDIR /src
COPY ./docs /src
RUN chown -R jekyll:jekyll /src; \
sed -i 's#baseurl: "/delphi-epidata"#baseurl: "/docs"#g' /src/_config.yml
RUN jekyll build

FROM tiangolo/meinheld-gunicorn:python3.7
LABEL org.opencontainers.image.source=https://github.com/cmu-delphi/delphi-epidata
# use delphi's timezome
Expand All @@ -17,7 +27,8 @@ ENV PYTHONUNBUFFERED 1
COPY ./devops/gunicorn_conf.py /app
COPY ./devops/start_wrapper.sh /
COPY ./src/server/ /app/app/
COPY --from=builder ./src/build/lib/ /app/app/lib/
COPY --from=builder /src/build/lib/ /app/app/lib/
COPY --from=docs_builder /src/_site /app/app/docs/
RUN rm -rf /app/app/__pycache__ /app/app/*.php \
&& chmod -R o+r /app/app \
&& chmod 755 /start_wrapper.sh
Expand Down
6 changes: 3 additions & 3 deletions src/server/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ def _get_db() -> Connection:

@app.before_request
def connect_db():
if request.path.startswith('/lib'):
if request.path.startswith("/lib") or request.path.startswith("/docs"):
return
# try to get the db
try:
_get_db()
except:
app.logger.error('database connection error', exc_info=True)
app.logger.error("database connection error", exc_info=True)
raise DatabaseErrorException()


Expand All @@ -50,7 +50,7 @@ def is_compatibility_mode() -> bool:
"""
checks whether this request is in compatibility mode
"""
return 'compatibility' in g and g.compatibility
return "compatibility" in g and g.compatibility


def set_compatibility_mode():
Expand Down
15 changes: 13 additions & 2 deletions src/server/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pathlib
import logging
from typing import Dict, Callable
from typing import Dict, Callable, Optional

from flask import request, send_file, Response, send_from_directory, jsonify
from flask import request, send_file, Response, send_from_directory, jsonify, safe_join

from ._config import URL_PREFIX, VERSION
from ._common import app, set_compatibility_mode
Expand Down Expand Up @@ -49,6 +49,17 @@ def send_lib_file(path: str):
return send_from_directory(pathlib.Path(__file__).parent / "lib", path)


@app.route(f"{URL_PREFIX}/docs", defaults=dict(path="/"))
@app.route(f"{URL_PREFIX}/docs/<path:path>")
def send_docs_file(path: Optional[str]):
base_dir = pathlib.Path(__file__).parent / "docs"
fixed_path = path or "index.html"
target_file = pathlib.Path(safe_join(base_dir, fixed_path))
if target_file.exists() and target_file.is_dir():
fixed_path = fixed_path + "/index.html"
return send_from_directory(base_dir, fixed_path)


if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
else:
Expand Down