Add shorten endpoint and venv documentation
This commit is contained in:
@@ -1,9 +1,18 @@
|
|||||||
# URL-shortener
|
# URL-shortener
|
||||||
URL-shortener project to showcase python use
|
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
|
## Run locally
|
||||||
```bash
|
```bash
|
||||||
uvicorn app.main:app --reload
|
uvicorn app.main:app --reload
|
||||||
```
|
```
|
||||||
Then check:
|
Then check:
|
||||||
http://127.0.0.1:8000
|
http://127.0.0.1:8000
|
||||||
|
http://127.0.0.1:8000/docs
|
||||||
|
|
||||||
|
Test Short URL Endpoint
|
||||||
|
http://127.0.0.1:8000/shorten
|
||||||
+17
@@ -1,9 +1,26 @@
|
|||||||
from fastapi import FastAPI, HTTPException
|
from fastapi import FastAPI, HTTPException
|
||||||
from fastapi.responses import RedirectResponse
|
from fastapi.responses import RedirectResponse
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from app.utils import generate_short_code
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
url_store = {}
|
||||||
|
|
||||||
|
class URLRequest(BaseModel):
|
||||||
|
url: str
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
def home():
|
def home():
|
||||||
return {"message": "URL Shortener API"}
|
return {"message": "URL Shortener API"}
|
||||||
|
|
||||||
|
@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}"}
|
||||||
Reference in New Issue
Block a user