Skip to content

Commit 71c82cb

Browse files
committed
Initial commit
0 parents  commit 71c82cb

14 files changed

+570
-0
lines changed

.gitignore

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
__pycache__/
2+
*.py[cod]
3+
*$py.class
4+
5+
# C extensions
6+
*.so
7+
8+
# Distribution / packaging
9+
.Python
10+
build/
11+
develop-eggs/
12+
dist/
13+
downloads/
14+
eggs/
15+
.eggs/
16+
lib/
17+
lib64/
18+
parts/
19+
sdist/
20+
var/
21+
wheels/
22+
share/python-wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.nox/
42+
.coverage
43+
.coverage.*
44+
.cache
45+
nosetests.xml
46+
coverage.xml
47+
*.cover
48+
*.py,cover
49+
.hypothesis/
50+
.pytest_cache/
51+
cover/
52+
53+
# Translations
54+
*.mo
55+
*.pot
56+
57+
# Flask stuff:
58+
instance/
59+
.webassets-cache
60+
61+
# Scrapy stuff:
62+
.scrapy
63+
64+
# Sphinx documentation
65+
docs/_build/
66+
67+
# PyBuilder
68+
.pybuilder/
69+
target/
70+
71+
# IPython
72+
profile_default/
73+
ipython_config.py
74+
75+
# pyenv
76+
# For a library or package, you might want to ignore these files since the code is
77+
# intended to run in multiple environments; otherwise, check them in:
78+
# .python-version
79+
80+
# pipenv
81+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
82+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
83+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
84+
# install all needed dependencies.
85+
#Pipfile.lock
86+
87+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
88+
__pypackages__/
89+
90+
# Celery stuff
91+
celerybeat-schedule
92+
celerybeat.pid
93+
94+
# SageMath parsed files
95+
*.sage.py
96+
97+
# Environments
98+
.env
99+
.env_db
100+
.venv
101+
env/
102+
venv/
103+
ENV/
104+
env.bak/
105+
venv.bak/
106+
107+
# Spyder project settings
108+
.spyderproject
109+
.spyproject
110+
111+
# Rope project settings
112+
.ropeproject
113+
114+
# mkdocs documentation
115+
/site
116+
117+
# mypy
118+
.mypy_cache/
119+
.dmypy.json
120+
dmypy.json
121+
122+
# Pyre type checker
123+
.pyre/
124+
125+
# pytype static type analyzer
126+
.pytype/
127+
128+
# Cython debug symbols
129+
cython_debug/
130+
131+
# Visual Studio Code #
132+
.vscode/*
133+
!.vscode/settings.json
134+
!.vscode/tasks.json
135+
!.vscode/launch.json
136+
!.vscode/extensions.json
137+
.history
138+
139+
nginx-conf/
140+
nginx-conf/*
141+
nginx-conf/local/
142+
nginx-conf/local/*
143+
nginx-conf/local/nginx.conf
144+
145+
data/
146+
data/*

Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM python:3.8
2+
3+
ADD . /usr/src/app
4+
5+
WORKDIR /usr/src/app
6+
7+
RUN apt-get update
8+
9+
COPY requirements.txt ./
10+
11+
EXPOSE 5000
12+
13+
RUN pip install -r requirements.txt
14+
15+
# Development (comment production lines and uncomment development lines)
16+
RUN export FLASK_APP=run.py
17+
18+
CMD ["flask", "run"]

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Flask APP project template
2+
3+
> by [Charlie Jouanne](https://github.com/cjjouanne)
4+
5+
## Prerequisites 🐳
6+
7+
* `Docker`
8+
* `docker-compose`
9+
10+
## Config app variables
11+
12+
First, run this command on the terminal inside the project directory to create a `.env` file.
13+
```
14+
touch .env
15+
```
16+
Then add this inside the `.env` file
17+
```
18+
SECRET_KEY="a12b9af2eb5a96ch8rl1e1c6771e525b30fa2c8"
19+
SQLALCHEMY_DATABASE_URI="postgresql://myuser:mypassword@postgres:5432/my_app"
20+
21+
MAIL_SERVER="smtp.gmail.com"
22+
MAIL_PORT=465
23+
MAIL_USERNAME="[email protected]"
24+
MAIL_PASSWORD="********"
25+
MAIL_USE_TLS=False
26+
MAIL_USE_SSL=True
27+
```
28+
## Config Database variables
29+
30+
Run this command on terminal to create `.env_db` file.
31+
```
32+
touch .env_db
33+
```
34+
Then add this inside the `.env_db` file
35+
```
36+
POSTGRES_USER="myuser"
37+
POSTGRES_PASSWORD="mypassword"
38+
POSTGRES_DB="my_app"
39+
```
40+
## Settig up Nginx
41+
42+
First, launch the app with this command
43+
```
44+
docker-compose -f local-docker-compose.yml up -d
45+
```
46+
and then run
47+
```
48+
docker-compose -f local-docker-compose.yml stop
49+
docker-compose -f local-docker-compose.yml down
50+
```
51+
This will stop the app and create a new folder named `./nginx-conf/local`. Then add a`nginx.conf` file inside this folder by running the following commands
52+
53+
```
54+
cd nginx-conf/local
55+
touch nginx.conf
56+
```
57+
Add this inside the `nginx.conf` file
58+
```
59+
server {
60+
listen 80;
61+
server_name localhost; #on production use domian.extension www.domain.extension
62+
location / {
63+
proxy_pass http://web:5000;
64+
}
65+
}
66+
```
67+
Now, you are ready to go!
68+
69+
## Run the app! 🖥
70+
```
71+
docker-compose -f local-docker-compose.yml up
72+
```
73+
Now go to http://localhost:80 and start browsing 😉

docker-compose.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
version: '3'
2+
services:
3+
postgres:
4+
image: postgres
5+
container_name: my_app_psql
6+
ports:
7+
- 5432:5432
8+
env_file:
9+
- ./.env_db
10+
volumes:
11+
- postgresdata:/var/lib/postgresql/data
12+
web:
13+
build: .
14+
container_name: my_app_flask
15+
restart: always
16+
volumes:
17+
- .:/usr/app/
18+
ports:
19+
- "5000:5000"
20+
command:
21+
"gunicorn --bind 0.0.0.0:5000 wsgi:app"
22+
env_file:
23+
- ./.env
24+
depends_on:
25+
- postgres
26+
- redis
27+
nginx:
28+
image: nginx:mainline-alpine
29+
container_name: my_app_nginx
30+
depends_on:
31+
- web
32+
volumes:
33+
- ./nginx-conf/production:/etc/nginx/conf.d
34+
- ./data/certbot/conf:/etc/letsencrypt
35+
- ./data/certbot/www:/var/www/certbot
36+
ports:
37+
- 80:80
38+
- 443:443
39+
logging:
40+
driver: json-file
41+
certbot:
42+
image: certbot/certbot
43+
container_name: my_app_cert
44+
volumes:
45+
- ./data/certbot/conf:/etc/letsencrypt
46+
- ./data/certbot/www:/var/www/certbot
47+
entrypoint:
48+
- "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
49+
redis:
50+
image: "redis:alpine"
51+
container_name: my_app_redis
52+
ports:
53+
- 6379:6379
54+
volumes:
55+
- redis_data:/data
56+
volumes:
57+
postgresdata:
58+
redis_data:

init-letsencrypt.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
3+
if ! [ -x "$(command -v docker-compose)" ]; then
4+
echo 'Error: docker-compose is not installed.' >&2
5+
exit 1
6+
fi
7+
8+
domains=(domain.extension www.domain.extension)
9+
rsa_key_size=4096
10+
data_path="./data/certbot"
11+
email="[email protected]" # Adding a valid address is strongly recommended
12+
staging=0 # Set to 1 if you're testing your setup to avoid hitting request limits
13+
14+
if [ -d "$data_path" ]; then
15+
read -p "Existing data found for $domains. Continue and replace existing certificate? (y/N) " decision
16+
if [ "$decision" != "Y" ] && [ "$decision" != "y" ]; then
17+
exit
18+
fi
19+
fi
20+
21+
22+
if [ ! -e "$data_path/conf/options-ssl-nginx.conf" ] || [ ! -e "$data_path/conf/ssl-dhparams.pem" ]; then
23+
echo "### Downloading recommended TLS parameters ..."
24+
mkdir -p "$data_path/conf"
25+
curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf > "$data_path/conf/options-ssl-nginx.conf"
26+
curl -s https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem > "$data_path/conf/ssl-dhparams.pem"
27+
echo
28+
fi
29+
30+
echo "### Creating dummy certificate for $domains ..."
31+
path="/etc/letsencrypt/live/$domains"
32+
mkdir -p "$data_path/conf/live/$domains"
33+
docker-compose run --rm --entrypoint "\
34+
openssl req -x509 -nodes -newkey rsa:4096 -days 1\
35+
-keyout '$path/privkey.pem' \
36+
-out '$path/fullchain.pem' \
37+
-subj '/CN=localhost'" certbot
38+
echo
39+
40+
41+
echo "### Starting nginx ..."
42+
docker-compose up --force-recreate -d nginx
43+
echo
44+
45+
echo "### Deleting dummy certificate for $domains ..."
46+
docker-compose run --rm --entrypoint "\
47+
rm -Rf /etc/letsencrypt/live/$domains && \
48+
rm -Rf /etc/letsencrypt/archive/$domains && \
49+
rm -Rf /etc/letsencrypt/renewal/$domains.conf" certbot
50+
echo
51+
52+
53+
echo "### Requesting Let's Encrypt certificate for $domains ..."
54+
#Join $domains to -d args
55+
domain_args=""
56+
for domain in "${domains[@]}"; do
57+
domain_args="$domain_args -d $domain"
58+
done
59+
60+
# Select appropriate email arg
61+
case "$email" in
62+
"") email_arg="--register-unsafely-without-email" ;;
63+
*) email_arg="--email $email" ;;
64+
esac
65+
66+
# Enable staging mode if needed
67+
if [ $staging != "0" ]; then staging_arg="--staging"; fi
68+
69+
docker-compose run --rm --entrypoint "\
70+
certbot certonly --webroot -w /var/www/certbot \
71+
$staging_arg \
72+
$email_arg \
73+
$domain_args \
74+
--rsa-key-size $rsa_key_size \
75+
--agree-tos \
76+
--force-renewal" certbot
77+
echo
78+
79+
echo "### Reloading nginx ..."
80+
docker-compose exec nginx nginx -s reload

0 commit comments

Comments
 (0)