diff --git a/README.md b/README.md index 9fb714a..e73055c 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,18 @@ # URL-shortener URL-shortener project to showcase python use +## Create venv +cd /opt/kjc/int/URL-shortener +python3 -m venv venv +source venv/bin/activate + ## Run locally ```bash uvicorn app.main:app --reload ``` Then check: -http://127.0.0.1:8000 \ No newline at end of file +http://127.0.0.1:8000 +http://127.0.0.1:8000/docs + +Test Short URL Endpoint +http://127.0.0.1:8000/shorten \ No newline at end of file diff --git a/app/main.py b/app/main.py index d855f15..4cf96c9 100644 --- a/app/main.py +++ b/app/main.py @@ -1,9 +1,26 @@ from fastapi import FastAPI, HTTPException from fastapi.responses import RedirectResponse +from pydantic import BaseModel +from app.utils import generate_short_code app = FastAPI() +url_store = {} + +class URLRequest(BaseModel): + url: str + @app.get("/") def home(): return {"message": "URL Shortener API"} - \ No newline at end of file + +@app.post("/shorten") +def shorten_url(request: URLRequest): + short_code = generate_short_code() + + url_store[short_code] = { + "original_url": request.url, + "clicks": 0 + } + + return {"short_url": f"http://localhost:8000/{short_code}"} \ No newline at end of file