Skip to content

Commit

Permalink
feat: working on local
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfromyeg committed Jan 10, 2024
1 parent c0edde4 commit 65dfeba
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 69 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ env/
exports/

.editorconfig
.env
.env.example
.gitignore
.markdownlint.json
Expand Down
15 changes: 6 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@ client:
@cd client && npm start

build:
@echo "Building the server..."
@docker-compose -f docker-compose.local.yml build
@echo "Building the server image..."
@docker build -t michaelfromyeg/bereal_wrapped -f docker/Dockerfile.server .
@docker push michaelfromyeg/bereal_wrapped

up:
@echo "Booting up the server..."
@docker-compose -f docker-compose.local.yml up -d
@docker stack deploy -c docker-stack.local.yml bereal-wrapped

logs:
@echo "Showing the server logs..."
@docker-compose -f docker-compose.local.yml logs -f
@docker service logs -f bereal-wrapped_web

down:
@echo "Shutting down the server..."
@docker-compose -f docker-compose.local.yml down

kill:
@echo "Killing the server..."
@docker-compose -f docker-compose.local.yml kill
@docker stack rm bereal-wrapped

start-redis:
@echo "Booting up Redis..."
Expand Down
12 changes: 5 additions & 7 deletions bereal/send.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ def sms(phone: str, link: str) -> None:
try:
client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)

if FLASK_ENV == "development":
logger.info("Skipping SMS in development mode")
return

message_body = f"Here is the link to your BeReal Wrapped!\n\n{link}"
message = client.messages.create(body=message_body, from_=TWILIO_PHONE_NUMBER, to=phone)

logger.info("Sent message to %s: %s", phone, message.sid)
if FLASK_ENV == "development":
logger.info("Skipping SMS in development mode; would send %s", message_body)

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

This expression logs
sensitive data (secret)
as clear text.
This expression logs
sensitive data (secret)
as clear text.
else:
message = client.messages.create(body=message_body, from_=TWILIO_PHONE_NUMBER, to=phone)
logger.info("Sent message %s to %s", message.sid, phone)
except Exception as e:
logger.error("Failed to send SMS: %s", e)
pass
13 changes: 8 additions & 5 deletions bereal/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def validate_otp() -> tuple[Response, int]:
return jsonify({"error": "Bad Request", "message": "Invalid verification code"}), 400

# generate a custom app token; this we can safely save in our DB
bereal_token = secrets.token_urlsafe(20)
bereal_token = secrets.token_hex(10)

insert_bereal_token(phone, bereal_token)

Expand Down Expand Up @@ -317,8 +317,11 @@ def delete_expired_tokens() -> None:
"""
Delete all expired tokens from the database.
"""
expiration_time = datetime.utcnow() - timedelta(hours=24)
BerealToken.query.filter(BerealToken.timestamp < expiration_time).delete()
expiration_time = datetime.utcnow() - timedelta(days=1)

n = BerealToken.query.filter(BerealToken.timestamp < expiration_time).delete()
logger.info("Deleted %d expired tokens", n)

db.session.commit()

return None
Expand All @@ -328,15 +331,15 @@ def delete_old_videos() -> None:
"""
Delete videos that are more than a day old.
"""
time_limit = datetime.now() - timedelta(days=1)
expiration_time = datetime.utcnow() - timedelta(days=1)

for filename in os.listdir(EXPORTS_PATH):
file_path = os.path.join(EXPORTS_PATH, filename)

if os.path.isfile(file_path):
file_mod_time = datetime.fromtimestamp(os.path.getmtime(file_path))

if file_mod_time < time_limit:
if file_mod_time < expiration_time:
try:
os.remove(file_path)
logger.info("Deleted video file %s", file_path)
Expand Down
32 changes: 20 additions & 12 deletions bereal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,30 @@
from datetime import datetime
from enum import StrEnum

from dotenv import load_dotenv

from .logger import logger


# Environment variables
load_dotenv()
def get_secret(secret_name: str) -> str | None:
"""
Get a Docker Swarm secret.
"""
try:
with open(f"/run/secrets/{secret_name}", "r") as secret_file:
return secret_file.read().strip()
except IOError:
return None


SECRET_KEY = os.getenv("SECRET_KEY") or "SECRET_KEY"
FLASK_ENV = os.getenv("FLASK_ENV") or "production"
SECRET_KEY = get_secret("secret_key") or "SECRET_KEY"
FLASK_ENV = get_secret("flask_env") or "production"

if SECRET_KEY == "SECRET_KEY":
raise ValueError("SECRET_KEY environment variable not set or non-unique")

TWILIO_PHONE_NUMBER = os.getenv("TWILIO_PHONE_NUMBER")
TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
TWILIO_ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
TWILIO_PHONE_NUMBER = get_secret("twilio_phone_number")
TWILIO_AUTH_TOKEN = get_secret("twilio_auth_token")
TWILIO_ACCOUNT_SID = get_secret("twilio_account_sid")

if TWILIO_PHONE_NUMBER is None or TWILIO_AUTH_TOKEN is None or TWILIO_ACCOUNT_SID is None:
raise ValueError("TWILIO environment variables not set")
Expand Down Expand Up @@ -90,14 +98,14 @@ def str2mode(s: str | None) -> Mode:

config.read("config.ini")

HOST: str | None = os.getenv("HOST") or "localhost"
PORT: str | None = os.getenv("PORT") or "5000"
HOST: str | None = get_secret("host") or "localhost"
PORT: str | None = get_secret("port") or "5000"
PORT = int(PORT) if PORT is not None else None

TRUE_HOST = f"http://{HOST}:{PORT}" if FLASK_ENV == "development" else "https://api.bereal.michaeldemar.co"

REDIS_HOST: str | None = os.getenv("REDIS_HOST") or "redis"
REDIS_PORT: str | None = os.getenv("REDIS_PORT") or "6379"
REDIS_HOST: str | None = get_secret("redis_host") or "redis"
REDIS_PORT: str | None = get_secret("redis_port") or "6379"
REDIS_PORT = int(REDIS_PORT) if REDIS_PORT is not None else None

TIMEOUT = config.getint("bereal", "timeout", fallback=10)
Expand Down
Empty file modified content/.gitkeep
100644 → 100755
Empty file.
14 changes: 6 additions & 8 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ services:
build:
context: .
dockerfile: docker/Dockerfile.server
image: bereal_web
volumes:
- ./exports:/app/exports
- ./content:/app/content
user: thekid
command: web
ports:
- "5000:5000"
environment:
Expand All @@ -17,30 +19,26 @@ services:
- redis

celery:
build:
context: .
dockerfile: docker/Dockerfile.celery
image: bereal_web
volumes:
- ./exports:/app/exports
- ./content:/app/content
user: thekid
command: celery -A bereal.celery worker --loglevel=INFO --logfile=celery.log -E
command: celery
environment:
- FLASK_APP=bereal.server
depends_on:
- web
- redis

flower:
build:
context: .
dockerfile: docker/Dockerfile.flower
image: bereal_web
volumes:
- ./exports:/app/exports
user: thekid
ports:
- "5555:5555"
command: celery -A bereal.celery flower --address=0.0.0.0 --inspect --enable-events --loglevel=DEBUG --logfile=flower.log
command: flower
environment:
- FLASK_APP=bereal.server
depends_on:
Expand Down
106 changes: 106 additions & 0 deletions docker-stack.local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
version: "3.8"

services:
web:
image: michaelfromyeg/bereal_wrapped
volumes:
- exports:/app/exports
- content:/app/content
command: web
ports:
- "5000:5000"
environment:
- FLASK_APP=bereal.server
secrets:
- flask_env
- secret_key
- host
- port
- redis_host
- redis_port
- twilio_phone_number
- twilio_auth_token
- twilio_account_sid
deploy:
replicas: 1

celery:
image: michaelfromyeg/bereal_wrapped
volumes:
- exports:/app/exports
- content:/app/content
command: celery
environment:
- FLASK_APP=bereal.server
secrets:
- flask_env
- secret_key
- host
- port
- redis_host
- redis_port
- twilio_phone_number
- twilio_auth_token
- twilio_account_sid
deploy:
replicas: 1

flower:
image: michaelfromyeg/bereal_wrapped
volumes:
- exports:/app/exports
- content:/app/content
ports:
- "5555:5555"
command: flower
environment:
- FLASK_APP=bereal.server
secrets:
- flask_env
- secret_key
- host
- port
- redis_host
- redis_port
- twilio_phone_number
- twilio_auth_token
- twilio_account_sid
deploy:
replicas: 1

redis:
image: "redis:alpine"
configs:
- source: redis_conf
target: /usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
deploy:
replicas: 1

volumes:
exports:
content:

configs:
redis_conf:
file: ./redis/redis.conf

secrets:
flask_env:
external: true
secret_key:
external: true
host:
external: true
port:
external: true
redis_host:
external: true
redis_port:
external: true
twilio_phone_number:
external: true
twilio_auth_token:
external: true
twilio_account_sid:
external: true
Loading

0 comments on commit 65dfeba

Please sign in to comment.