Skip to content

Commit

Permalink
add reverse proxy and gunicorn to flask app (#5)
Browse files Browse the repository at this point in the history
* add proxy and gunicorn

* update CI for changed folder structure
  • Loading branch information
iulusoy authored Jul 18, 2024
1 parent 9238012 commit 0760401
Show file tree
Hide file tree
Showing 22 changed files with 121 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
env:
FLASK_SECRET_KEY: ${{ secrets.FLASK_SECRET_KEY }}
run: |
cd src
cd src/app
python -m pytest -svv --cov=. --cov-report=xml
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
Expand Down
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python
WORKDIR /app

COPY ./src/app /app
COPY ./requirements.txt /app

RUN pip install --upgrade pip
RUN pip install -r requirements.txt

ENV FLASK_SECRET_KEY "sdfk"
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
nginx:
image: nginx
ports:
- "8080:80"
- "8443:443"
volumes:
- ./src/nginx/conf/:/etc/nginx/conf.d/:ro
- ./src/nginx/keys/:/etc/nginx/ssl/:ro
webapp:
build: .
command: gunicorn --bind 0.0.0.0:8000 wsgi:app
expose:
- "8000"
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
flask
flask-sqlalchemy
sqlalchemy
gunicorn
pytest
pytest-cov
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/website/__init__.py → src/app/website/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def create_app():
app.register_blueprint(donate, url_prefix="/")
app.register_blueprint(about, url_prefix="/")

from .models import RawData
from .models import RawData # noqa

with app.app_context():
db.create_all()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions src/app/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import annotations
from website import create_app

app = create_app()

if __name__ == "__main__":
app.run()
8 changes: 8 additions & 0 deletions src/gunicorn/gunicorn.conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

import multiprocessing

bind = "0.0.0.0:8000"
workers = multiprocessing.cpu_count() * 2 + 1

loglevel = "debug"
1 change: 1 addition & 0 deletions src/gunicorn/gunicorn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gunicorn -c ./gunicorn.conf.py 'wsgi:app'
31 changes: 31 additions & 0 deletions src/nginx/conf/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
upstream hello_flask {
server webapp:8000;
}

server {
listen 80;

server_name localhost;

location / {
return 301 https://$server_name:8443$request_uri;
}
}

server {
listen 443 ssl;

server_name localhost;

ssl_certificate /etc/nginx/ssl/localhost.crt;
ssl_certificate_key /etc/nginx/ssl/localhost.key;

location / {
proxy_pass http://hello_flask;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
}
}
19 changes: 19 additions & 0 deletions src/nginx/keys/localhost.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDDzCCAfegAwIBAgIULnBEtFpdd7D+gcpvj8YjAq+R6D8wDQYJKoZIhvcNAQEL
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI0MDYyNjExMjkyMloXDTI0MDcy
NjExMjkyMlowFDESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF
AAOCAQ8AMIIBCgKCAQEA5ePEgmz6KUOO9ZvwcB3aF68eT3COg1d3KYcCHjBnJhWX
DaYK3y/cFVOtDF2N4SQa6U4Jr3FYpWzbpApFZEKa9lKli6KGrCwbKyZTEyJL0SBt
bj4CirUS4t/iIlRJUs0i0Hu5q/Y4sGsPdziwuWXz2sU9Fh0U+F8gVFhkR8q74LbN
aheZrisk6hjIpLOVNO6UDACWLQlfddI7E7mzbkrX1k6SMKNZkhfESOdIVsGjK+9M
z75U8AJMHnkRcKRzQYBlm2WF5YlMqGTJAENU18a5LwRBpo3VVHWEdP9h1AzlcRbQ
mzjuSoxMCkU4RLSAL4JscnDbUQZLDQBZWYwSH8UxJQIDAQABo1kwVzAUBgNVHREE
DTALgglsb2NhbGhvc3QwCwYDVR0PBAQDAgeAMBMGA1UdJQQMMAoGCCsGAQUFBwMB
MB0GA1UdDgQWBBSsOUZ7RzAKyhfR65lRdnLNU3vgNzANBgkqhkiG9w0BAQsFAAOC
AQEAXrqB98vtH/NcNyL0ABl0Fk65OplJ0TVAgAnZcoTtfYTP1xzL4x1P2XJuqDmJ
mRMzv6eKrslROgfDT9YXmWAnSQqJPV+VE1h9jU6917tLQPlRMLvc/nVMQ94lV6dc
CVebjQTkhfKzCQULzXN5E6cXf3dt9PkVovTmcCnIvJ74/uP2bD0WhPexTnUkk7Qj
lLlgkvud0nFpKqinStXFxrOygkJFt893b6fbHsC9RiFqhxs+NfGGJrb628cQVLUV
ayzkxmt0U6kX4LE93mcoH+8u63IkjdV4PJvd/xU41Dsqp8rOJAzIOp8LTeqFFfU0
/XRlYscYIpdNGlT1SH1FE4e5Vw==
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions src/nginx/keys/localhost.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDl48SCbPopQ471
m/BwHdoXrx5PcI6DV3cphwIeMGcmFZcNpgrfL9wVU60MXY3hJBrpTgmvcVilbNuk
CkVkQpr2UqWLooasLBsrJlMTIkvR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zN2nyIDkZFidHTsEQQvV/qqUuJur0A6qeOjkS3/M+t26x518Y67EOXelAoGBAImY
e0INi6w65XhTjLG9XBBiKPlVCmUalzDPfvBXiA7xK9+FAuN8uhGXxf8t2Y6hhnck
0DCYK1O1xyq8RjtXxsJpfr0Ak2RtsXQD41Iubd9kqFsLbgHr1XqIITVa6Vo41JB9
GR6wnAcNIAQg7+/X/UYRzQJMeqtZeuwucZ0IeD0ZAoGBAPxB+pN01TuBYyzi0Eta
jqWgdhzTbLugMy69q8d/+jz7csuExysGV5gchbcS5p0BrbhjVWajEiddpVI7xaa3
MuCBjXhbnfxeeOWcd7NzEF9GVWDkZGuzpUlCbjJAB37nxCb+5QZ841vArj19qGAn
VR332b7GuTySlRE3HYHstCre
-----END PRIVATE KEY-----

0 comments on commit 0760401

Please sign in to comment.