Skip to content

Commit

Permalink
Merge pull request #23 from GatorEducator/refactor/flask-improvements
Browse files Browse the repository at this point in the history
Add Gunicorn
  • Loading branch information
Michionlion authored Apr 28, 2019
2 parents bc7d597 + 59e5a35 commit 10b8e2a
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Dockerfile

# Ignore git's ignore

# Config python file
config.py

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Config python file
config.py

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ FROM python:3.7.3-alpine
MAINTAINER gkapfham@allegheny.edu

ENV APP_DIR /quizagator
ENV FLASK_PORT 5000

EXPOSE ${FLASK_PORT}

# create and use the quizagator directory
WORKDIR ${APP_DIR}
Expand All @@ -17,5 +14,7 @@ COPY . ${APP_DIR}
# (Don't use pipenv run to run things)
RUN set -ex && pip install pipenv && pipenv install --deploy --system

EXPOSE 80

# the start command will run the production server
CMD python run.py --host 0.0.0.0 --port ${FLASK_PORT}
CMD ["gunicorn", "--workers", "3", "--access-logfile", "-", "--bind", "0.0.0.0:80", "application.wsgi:app"]
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ name = "pypi"

[packages]
flask = "*"
gunicorn = "*"

[dev-packages]
black = "*"
Expand Down
10 changes: 9 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ project, inspect the `[scripts]` tag in `Pipfile`:
cat Pipfile
```

Finally, to run the project locally:
Finally, to run the project locally in development mode:

```
pipenv run server
Expand All @@ -80,6 +80,23 @@ Or use the following to see all the options:
pipenv run python run.py --help
```

## Docker

There is a docker image published to
[gatoreducator/quizagator](https://hub.docker.com/r/gatoreducator/quizagator).
There are two main parts of configuration: specifying a secret in the
`FLASK_SECRET_KEY` environment variable, and forwarding the desired outer port
to `80` inside the container. The following command does both:

```
docker run --name quizagator -p 5000:80 -e FLASK_SECRET_KEY=d2dbb3 gatoreducator/quizagator:0.0.1-dev
```

Additionally, developers can use `pipenv run create-image` and `pipenv run
image` to run a development container -- this is not to be deployed to
production.


## Contributors

Check out our contributors!
Expand Down
File renamed without changes.
17 changes: 10 additions & 7 deletions application/db_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@

# from flask import g, session, escape
from flask import current_app as app
from flask import g as context_globals


DATABASE = "database.db"


def get_db():
""" Get database """
db = getattr(flask.g, "_database", None)
if db is None:
db = flask.g._database = sqlite3.connect(DATABASE)
return db
if "db" not in context_globals:
context_globals.db = sqlite3.connect(DATABASE)

return context_globals.db


@app.teardown_appcontext
# pylint: disable=unused-argument
def close_connection(exception):
def teardown_db(exception=None):
""" Close database connection """
db = getattr(flask.g, "_database", None)
if exception is not None:
print(f"ERROR: {exception}")
db = context_globals.pop("db", None)

if db is not None:
db.close()

Expand Down
7 changes: 7 additions & 0 deletions application/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""WSGI entry point for use with gunicorn in production"""
import os

from application import appfactory

app = appfactory.create_app()
app.secret_key = os.environ["FLASK_SECRET_KEY"]
6 changes: 3 additions & 3 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Run this file to run the application"""
"""Run this file to run the development server"""

import argparse

from application import app as appfactory
from application import appfactory

app = appfactory.create_app()

app.secret_key = "$JLmL!eCQXyajbdu2LCJ&Vwqs2JGagg3B&FRfexCmKBV"
app.secret_key = "dev"


parser = argparse.ArgumentParser()
Expand Down
2 changes: 1 addition & 1 deletion scripts/create-docker-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ if ! test -z "$TAG"; then
NAME="$NAME:$TAG"
fi

docker image rm --force "$NAME"
#docker image rm --force "$NAME"

docker build -t "$NAME" .
2 changes: 1 addition & 1 deletion scripts/run-docker-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ OUTER_PORT="4201"
docker stop "$NAME"
docker rm "$NAME"

docker run -d --name "$NAME" -p "${OUTER_PORT}:${INNER_PORT}" "$IMAGE"
docker run --name "$NAME" -e "FLASK_SECRET_KEY=dev" -p "${OUTER_PORT}:${INNER_PORT}" "$IMAGE"
2 changes: 1 addition & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Basic Tests"""
import pytest

from application import app as appfactory
from application import appfactory


@pytest.fixture()
Expand Down

0 comments on commit 10b8e2a

Please sign in to comment.