feat: setup project for docker deploy

This commit is contained in:
2025-07-23 22:00:35 +00:00
parent 98369ef531
commit 715bc90340
12 changed files with 139 additions and 10 deletions

24
backend/.env.example Normal file
View File

@@ -0,0 +1,24 @@
# Backend environment variables #
# Must be a PostgreSQL db connection string
DATABASE_URL=postgresql://user:password@localhost:5432/mydatabase
# [dev, stage, prod]
ENVIRONMENT=dev
### use the below to generate a good salt
# import secrets
# print(secrets.token_urlsafe(32))
###
HASHIDS_SALT=default-insecure-salt
### use the below to generate alphabet for encoder
# import random
# base61 = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789")
# random.shuffle(base61)
# print("".join(base61))
###
ENCODER_ALPHABET=CnArvIseYhld2BtZipybguVKaMx4QFkcR71DTLJEP65jUGzqmw9fSoXW83HNO
### which origins are allowed to make cross origin requests (CORS)
ALLOW_ORIGINS=http://localhost:3000,http://localhost:3030

10
backend/Dockerfile Normal file
View File

@@ -0,0 +1,10 @@
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

View File

@@ -1,5 +1,6 @@
from pydantic_settings import BaseSettings
from typing import Literal, List
import os
class Settings(BaseSettings):
database_url: str
@@ -9,12 +10,11 @@ class Settings(BaseSettings):
allow_origins: str
class Config:
env_file = ".env"
env_file = os.path.join(os.path.dirname(__file__), ".env")
env_file_encoding = "utf-8"
def get_allow_origins(self) -> List[str]:
return [origin.strip() for origin in self.allow_origins.split(",")]
# Singleton instance of settings
settings = Settings()
settings = Settings()

View File

@@ -19,9 +19,8 @@ app.add_middleware(
async def startup():
await database.connect()
# only create missing tables in dev environment
if settings.environment == "dev":
Base.metadata.create_all(bind=engine)
# create tables if they don't exist
Base.metadata.create_all(bind=engine)
@app.on_event("shutdown")
async def shutdown():

31
backend/requirements.txt Normal file
View File

@@ -0,0 +1,31 @@
alembic==1.16.4
annotated-types==0.7.0
anyio==4.9.0
astroid==3.3.11
asyncpg==0.30.0
click==8.2.1
databases==0.9.0
dill==0.4.0
fastapi==0.116.1
greenlet==3.2.3
h11==0.16.0
hashids==1.3.1
idna==3.10
isort==6.0.1
Mako==1.3.10
MarkupSafe==3.0.2
mccabe==0.7.0
platformdirs==4.3.8
psycopg2-binary==2.9.10
pydantic==2.11.7
pydantic-settings==2.10.1
pydantic_core==2.33.2
pylint==3.3.7
python-dotenv==1.1.1
sniffio==1.3.1
SQLAlchemy==2.0.41
starlette==0.47.2
tomlkit==0.13.3
typing-inspection==0.4.1
typing_extensions==4.14.1
uvicorn==0.35.0

View File

@@ -22,7 +22,7 @@ async def redirect_to_original_url(shortcode: str):
raise HTTPException(status_code=404, detail=str(e)) from e
@router.post("/")
@router.post("/api/shorten")
async def create_url_shortcode(url_payload: UrlPayload):
url = str(url_payload.url)