Skip to content

Commit

Permalink
dockerise
Browse files Browse the repository at this point in the history
  • Loading branch information
dxstiny committed May 22, 2024
1 parent 4b91f86 commit 5e66921
Show file tree
Hide file tree
Showing 24 changed files with 79 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
__pycache__
.vscode
.ruff_cache
.env
token
token
postgres/
36 changes: 36 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
services:
stan:
build:
context: service
dockerfile: Dockerfile
depends_on:
- postgres
networks:
- default
- db
env_file:
- .env
postgres:
image: postgres:16-alpine
volumes:
- ./postgres:/var/lib/postgresql/data
networks:
- db
env_file:
- .env
nginx:
image: nginx:alpine
ports:
- 80:80
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
depends_on:
- stan
networks:
- default
restart: always

networks:
default:
db:
internal: true
22 changes: 22 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
events {
worker_connections 1024;
}

http {
server {
listen 80 default_server;
listen [::]:80 default_server;

server_name _;

# api proxy
location /api {
proxy_pass http://stan:8080;
}

# static files
location / {
try_files $uri $uri/ /index.html;
}
}
}
File renamed without changes.
9 changes: 9 additions & 0 deletions service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python:3.12-alpine

WORKDIR /opt/stan

ENTRYPOINT [ "python", "main.py" ]

EXPOSE 8080
COPY . /opt/stan
RUN pip3 install --no-cache-dir -r requirements.txt
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.
20 changes: 8 additions & 12 deletions db/db.py → service/db/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__email__ = "[email protected]"

import logging
import os
from dataclasses import asdict
import dataset # type: ignore
from common.singleton import Singleton
Expand All @@ -14,9 +15,10 @@
VERIFIED_USERS_TABLE = "Verified_Users"
HSLU_STUDENTS_TABLE = "HSLU_Students"

DB_USERNAME = "postgres"
DB_PASSWORD = "postgres"
DB_HOST = "localhost:5432"
DB_USERNAME = os.getenv("POSTGRES_USER")
DB_PASSWORD = os.getenv("POSTGRES_PASSWORD")
DB_DATABASE = os.getenv("POSTGRES_DB")
DB_HOST = "postgres:5432"
DB_PROTOCOL = "postgresql"


Expand All @@ -25,7 +27,7 @@ class Database(metaclass=Singleton):

def __init__(self) -> None:
self._db = dataset.connect(
f"{DB_PROTOCOL}://{DB_USERNAME}:{DB_PASSWORD}@{DB_HOST}/"
f"{DB_PROTOCOL}://{DB_USERNAME}:{DB_PASSWORD}@{DB_HOST}/{DB_DATABASE}"
)
self._logger = logging.getLogger("Database")

Expand All @@ -49,9 +51,7 @@ def update_students(
"""
current_members = self.all_verified()
previous_students = [x for x in current_members if x.state == UserState.STUDENT]
previous_graduates = [
x for x in current_members if x.state == UserState.GRADUATE
]
previous_graduates = [x for x in current_members if x.state == UserState.GRADUATE]

self._hslu_students_table.drop()
# make unique
Expand Down Expand Up @@ -104,9 +104,5 @@ def all_verified(self) -> list[VerifiedUser]:
def verify_member(self, discord_id: int, email: str) -> None:
"""Verify a member by their Discord ID and email."""
self._users_table.insert(
asdict(
VerifiedUser(
discord_id=discord_id, email=email, state=UserState.STUDENT
)
)
asdict(VerifiedUser(discord_id=discord_id, email=email, state=UserState.STUDENT))
)
3 changes: 0 additions & 3 deletions email_client.py → service/email_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
import os
import logging
from msal import PublicClientApplication # type: ignore
from dotenv import load_dotenv
import aiohttp

load_dotenv()

AD_APP_ID = os.getenv("AD_APP_ID")
EMAIL_ADDRESS = os.getenv("EMAIL_ADDRESS")
EMAIL_NAME = os.getenv("EMAIL_NAME")
Expand Down
File renamed without changes.
0 lint.sh → service/lint.sh
100755 → 100644
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions requirements.txt → service/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
dataset==1.6.2
aiohttp==3.9.5
discord.py==2.3.2
psycopg2==2.9.9
psycopg2-binary==2.9.9
pyaddict==1.0.7
uvloop==0.19.0
python-dotenv==1.0.1
msal==1.28.0
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 5e66921

Please sign in to comment.