mirror of
https://github.com/Rainyy21/URL_shortener.git
synced 2026-10-10 23:50:14 -04:00
added docker and modify the file so it work
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
.venv
|
||||||
|
__pycache__
|
||||||
|
.pytest_cache
|
||||||
|
.git
|
||||||
|
test/
|
||||||
|
*.db
|
||||||
|
server.log
|
||||||
+20
@@ -0,0 +1,20 @@
|
|||||||
|
FROM python:3.11-slim
|
||||||
|
|
||||||
|
# Set the working directory
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy the requirements file and install dependencies
|
||||||
|
COPY requirements.txt .
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
# Copy the rest of the application code
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# Ensure the database directory exists for SQLite persistence
|
||||||
|
RUN mkdir -p /app/db
|
||||||
|
|
||||||
|
# Expose the application port
|
||||||
|
EXPOSE 8000
|
||||||
|
|
||||||
|
# Command to start the application
|
||||||
|
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||||
Vendored
Vendored
+5
-1
@@ -1,3 +1,7 @@
|
|||||||
import redis
|
import redis
|
||||||
|
import os
|
||||||
|
|
||||||
redis_client = redis.Redis(host="localhost", port=6379, decode_responses=True)
|
redis_host = os.getenv("REDIS_HOST", "localhost")
|
||||||
|
redis_port = int(os.getenv("REDIS_PORT", 6379))
|
||||||
|
|
||||||
|
redis_client = redis.Redis(host=redis_host, port=redis_port, decode_responses=True)
|
||||||
|
|||||||
BIN
Binary file not shown.
@@ -0,0 +1,17 @@
|
|||||||
|
services:
|
||||||
|
web:
|
||||||
|
build: .
|
||||||
|
ports:
|
||||||
|
- "8000:8000"
|
||||||
|
volumes:
|
||||||
|
- ./db:/app/db
|
||||||
|
environment:
|
||||||
|
- REDIS_HOST=redis
|
||||||
|
- REDIS_PORT=6379
|
||||||
|
depends_on:
|
||||||
|
- redis
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: "redis:alpine"
|
||||||
|
ports:
|
||||||
|
- "6379:6379"
|
||||||
@@ -37,19 +37,24 @@ def shorten_url(request: schemas.URLCreate, db: Session = Depends(get_db)):
|
|||||||
|
|
||||||
crud.create_url(db, short_code, request.long_url)
|
crud.create_url(db, short_code, request.long_url)
|
||||||
|
|
||||||
# set it in cache for a hour
|
# set it in cache for a hour, but don't fail if redis is down
|
||||||
redis_client.set(short_code, request.long_url, ex=3600)
|
try:
|
||||||
|
redis_client.set(short_code, request.long_url, ex=3600)
|
||||||
|
except Exception:
|
||||||
|
print("Redis is unavailable, skipping cache set")
|
||||||
|
|
||||||
return {"short_url": f"http://localhost:8000/{short_code}"}
|
return {"short_url": f"http://localhost:8000/{short_code}"}
|
||||||
|
|
||||||
|
|
||||||
@app.get("/{short_code}")
|
@app.get("/{short_code}")
|
||||||
def redirect_url(short_code: str, db: Session = Depends(get_db)):
|
def redirect_url(short_code: str, db: Session = Depends(get_db)):
|
||||||
# check the redis
|
# check the redis, but don't fail if redis is down
|
||||||
check_url = redis_client.get(short_code)
|
try:
|
||||||
|
check_url = redis_client.get(short_code)
|
||||||
if check_url:
|
if check_url:
|
||||||
return RedirectResponse(url=check_url)
|
return RedirectResponse(url=check_url)
|
||||||
|
except Exception:
|
||||||
|
print("Redis is unavailable, skipping cache check")
|
||||||
|
|
||||||
# get url from the db
|
# get url from the db
|
||||||
db_url = crud.get_url(db, short_code)
|
db_url = crud.get_url(db, short_code)
|
||||||
@@ -57,5 +62,10 @@ def redirect_url(short_code: str, db: Session = Depends(get_db)):
|
|||||||
if not db_url:
|
if not db_url:
|
||||||
raise HTTPException(status_code=404, detail="URL not found")
|
raise HTTPException(status_code=404, detail="URL not found")
|
||||||
|
|
||||||
redis_client.set(short_code, db_url.long_url)
|
# update cache if possible
|
||||||
|
try:
|
||||||
|
redis_client.set(short_code, db_url.long_url)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
return RedirectResponse(url=db_url.long_url)
|
return RedirectResponse(url=db_url.long_url)
|
||||||
|
|||||||
@@ -2,3 +2,7 @@ fastapi
|
|||||||
uvicorn
|
uvicorn
|
||||||
sqlalchemy
|
sqlalchemy
|
||||||
redis
|
redis
|
||||||
|
pydantic
|
||||||
|
pytest
|
||||||
|
httpx
|
||||||
|
pytest-mock
|
||||||
|
|||||||
Reference in New Issue
Block a user