Skip to content

Commit 92daa04

Browse files
authored
Merge pull request #37 from Victornguli/dev
Add dockerised nginx, gunicorn and postgres services
2 parents 5cff35a + 894ae81 commit 92daa04

File tree

10 files changed

+168
-9
lines changed

10 files changed

+168
-9
lines changed

.env.dev

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SQLALCHEMY_DATABASE_URI=postgresql+psycopg2://postgres:postgres@db/flashlearn
2+
SQL_HOST=db
3+
SQL_PORT=5432
4+
DATABASE=postgres
5+
POSTGRES_USER=postgres
6+
POSTGRES_PASSWORD=postgres
7+
POSTGRES_DB=flashlearn

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ htmlcov
88
*.sqlite3
99
.vscode
1010
.pytest_cache
11+
postgres-data
12+
.env.prod
13+
.env.prod.db

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ FROM python:3.8-slim-buster
22
ENV FLASK_RUN_HOST=0.0.0.0
33
WORKDIR /flashlearn
44

5+
RUN apt-get update && apt-get -y install libpq-dev gcc netcat
6+
57
COPY requirements.txt requirements.txt
68

79
RUN pip3 install -r requirements.txt

docker-compose.yml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,39 @@ version: "3.9"
22
services:
33
web:
44
build: .
5+
env_file:
6+
- ./.env.prod
7+
depends_on:
8+
- db
9+
expose:
10+
- 5000
511
ports:
6-
- "5000:5000"
12+
- 5000:5000
713
volumes:
8-
- .:/web
14+
- .:/web
915
redis:
1016
image: redis
17+
db:
18+
environment:
19+
POSTGRES_USER: postgres
20+
POSTGRES_PASSWORD: postgres
21+
POSTGRES_DB: flashlearn
22+
image: postgres:latest
23+
networks:
24+
- default
25+
ports:
26+
- 5405:5432
27+
env_file:
28+
- ./.env.prod
29+
volumes:
30+
- ./postgres-data:/var/lib/postgresql/data
31+
nginx:
32+
build: ./nginx
33+
ports:
34+
- 1337:80
35+
depends_on:
36+
- web
37+
38+
volumes:
39+
postgres-data:
40+
external: true

entrypoint.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
#!/usr/bin/env bash
2+
3+
if [ "$DATABASE" = "postgres" ]
4+
then
5+
echo "Waiting for postgres..."
6+
7+
while ! nc -z $SQL_HOST $SQL_PORT; do
8+
sleep 0.1
9+
done
10+
11+
echo "PostgreSQL started"
12+
fi
13+
214
python -m flask db upgrade
3-
python -m flask run --host=0.0.0.0
15+
gunicorn --bind 0.0.0.0:5000 wsgi:app
16+
17+
exec "$@"

instance/config.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
55
ROOT_DIR = os.path.abspath(os.path.dirname(BASE_DIR))
66

7-
87
WIN = sys.platform.startswith("win")
98
if WIN:
109
prefix = "sqlite:///"
@@ -22,7 +21,10 @@ class BaseConfig:
2221
USE_REDIS_CACHE = False
2322
CSRF_ENABLED = True
2423
FLASK_APP = "flashlearn"
25-
SQLALCHEMY_DATABASE_URI = os.getenv("SQLALCHEMY_DATABASE_URI")
24+
sqlite_db_path = os.path.join(BASE_DIR, "dev_db.sqlite3")
25+
SQLALCHEMY_DATABASE_URI = os.getenv(
26+
"SQLALCHEMY_DATABASE_URI", f"{prefix}{sqlite_db_path}"
27+
)
2628
SQLALCHEMY_TRACK_MODIFICATIONS = True
2729
LOG_FILE = os.getenv("LOG_FILE") or os.path.join(ROOT_DIR, "flashlearn.log")
2830
LOG_LEVEL = 20
@@ -32,8 +34,6 @@ class DevelopmentConfig(BaseConfig):
3234
"""Development config class"""
3335

3436
DEBUG = True
35-
db_path = os.path.join(BASE_DIR, "dev_db.sqlite3")
36-
SQLALCHEMY_DATABASE_URI = f"{prefix}{db_path}"
3737

3838

3939
class TestingConfig(BaseConfig):
@@ -52,8 +52,6 @@ class ProductionConfig(BaseConfig):
5252

5353
DEBUG = False
5454
TESTING = False
55-
db_path = os.path.join(BASE_DIR, "dev_db.sqlite3")
56-
SQLALCHEMY_DATABASE_URI = f"{prefix}{db_path}" # Replace with production database..
5755

5856

5957
app_config = {

nginx/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM nginx:1.17-alpine
2+
3+
RUN rm /etc/nginx/nginx.conf
4+
COPY ./nginx.conf /etc/nginx/
5+
RUN rm /etc/nginx/conf.d/default.conf
6+
COPY ./nginx.conf /etc/nginx/conf.d/

nginx/nginx.conf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
upstream web {
2+
server web:5000;
3+
server localhost:5000;
4+
server 127.0.0.1:5000;
5+
}
6+
7+
server {
8+
9+
listen 80;
10+
server_name flashlearn_nginx;
11+
12+
location / {
13+
proxy_pass http://web/;
14+
15+
# Do not change this
16+
proxy_set_header Host $host;
17+
proxy_set_header X-Real-IP $remote_addr;
18+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
19+
}
20+
21+
location /static {
22+
rewrite ^/static(.*) /$1 break;
23+
root /static;
24+
}
25+
}

requirements.txt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,67 @@ wrapt==1.12.1
6060
WTForms==2.3.3
6161
yarg==0.1.9
6262
zipp==3.1.0
63+
alembic==1.4.3
64+
appdirs==1.4.4
65+
astroid==2.4.2
66+
atomicwrites==1.4.0
67+
attrs==19.3.0
68+
bcrypt==3.1.7
69+
black==20.8b1
70+
certifi==2020.6.20
71+
cffi==1.14.0
72+
chardet==3.0.4
73+
click==7.1.2
74+
colorama==0.4.3
75+
coverage==5.2.1
76+
docopt==0.6.2
77+
flake8==3.8.3
78+
Flask==1.1.2
79+
Flask-Bcrypt==0.7.1
80+
Flask-Caching==1.10.0
81+
Flask-Migrate==2.5.3
82+
Flask-SQLAlchemy==2.4.4
83+
Flask-WTF==0.14.3
84+
gunicorn==20.1.0
85+
idna==2.10
86+
importlib-metadata==1.7.0
87+
iniconfig==1.0.1
88+
isort==5.5.0
89+
itsdangerous==1.1.0
90+
Jinja2==2.11.3
91+
lazy-object-proxy==1.4.3
92+
Mako==1.1.3
93+
MarkupSafe==1.1.1
94+
mccabe==0.6.1
95+
more-itertools==8.4.0
96+
mypy-extensions==0.4.3
97+
packaging==20.4
98+
pathspec==0.8.0
99+
pluggy==0.13.1
100+
psycopg2==2.8.6
101+
py==1.10.0
102+
pycodestyle==2.6.0
103+
pycparser==2.20
104+
pyflakes==2.2.0
105+
pylint==2.6.0
106+
pyparsing==2.4.7
107+
pytest==6.0.1
108+
pytest-cov==2.10.0
109+
python-dateutil==2.8.1
110+
python-dotenv==0.14.0
111+
python-editor==1.0.4
112+
redis==3.5.3
113+
regex==2020.7.14
114+
requests==2.24.0
115+
six==1.15.0
116+
SQLAlchemy==1.3.18
117+
toml==0.10.1
118+
typed-ast==1.4.1
119+
typing-extensions==3.7.4.3
120+
urllib3==1.25.11
121+
wcwidth==0.2.5
122+
Werkzeug==1.0.1
123+
wrapt==1.12.1
124+
WTForms==2.3.3
125+
yarg==0.1.9
126+
zipp==3.1.0

wsgi.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sys
2+
import logging
3+
from flashlearn import create_app
4+
5+
logging.basicConfig(stream=sys.stderr)
6+
sys.path.insert(0, "/var/www/")
7+
app = create_app()
8+
9+
if __name__ == "__main__":
10+
app.run()

0 commit comments

Comments
 (0)