Skip to content

Commit

Permalink
user routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kylehtran committed Nov 2, 2024
1 parent e19cb6c commit 1a43e58
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 20 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ RUN poetry config virtualenvs.create false
WORKDIR /app
COPY . /app

ENV PYTHONUNBUFFERED 1

RUN poetry install --no-interaction --with prod
RUN rm -rf /root/.cache/pypoetry

CMD [ "gunicorn", "-w", "4", "--bind", "0.0.0.0:8000", "fishsense_services.app:app" ]
CMD ["gunicorn", "-w", "4", "--worker-class", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000", "fishsense_services.app:app"]
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
git clone with --recurse-submodules
git clone with --recurse-submodules
remeber to git submodule update!!
62 changes: 62 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
version: '3.8'

services:
db:
container_name: postgres
image: postgres
expose:
- "5432"
environment:
POSTGRES_USER: placeholder_superuser
POSTGRES_PASSWORD: placeholder_pw
PGDATA: /data/postgres
POSTGRES_DB: fishsense_db
volumes:
- db:/data/postgres
ports:
- "5332:5432"
networks:
- fishsense_network
restart: always
healthcheck:
test: ["CMD-SHELL", "pg_isready -d $$POSTGRES_DB -U $$POSTGRES_USER"]
interval: 30s
timeout: 10s
retries: 5

fishsense-database:
build:
context: ./fishsense-database
environment:
DB_HOST: db
DB_NAME: fishsense_db
DB_USER: placeholder_superuser
DB_PASSWORD: placeholder_pw
networks:
- fishsense_network
depends_on:
- db

fishsense-services:
build:
context: ./
environment:
DB_HOST: db
DB_NAME: fishsense_db
DB_USER: placeholder_superuser
DB_PASSWORD: placeholder_pw
PYTHONPATH: /app:/app/fishsense-database:/app/fishsense-database/fishsense_database
ports:
- "8000:8000" # fishsense-services port
networks:
- fishsense_network
depends_on:
- db
- fishsense-database

networks:
fishsense_network:
driver: bridge

volumes:
db:
2 changes: 1 addition & 1 deletion fishsense-database
4 changes: 4 additions & 0 deletions fishsense_services/app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from fastapi import FastAPI
from fishsense_services.routes.usr.login import login_router
from fishsense_services.routes.usr.usr import usr_router
from fishsense_services.routes.img.img import img_router


def create_app():
app = FastAPI()

app.include_router(login_router, prefix="/login")
app.include_router(usr_router, prefix="/usr")
app.include_router(img_router, prefix="/img")

return app

Expand Down
12 changes: 3 additions & 9 deletions fishsense_services/routes/img/img.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def upload_img():
raise HTTPException(status_code=404, detail="User does not exist")


@img_router.get("/img/{username}/{img_id}", status_code=200)
@img_router.get("/{username}/{img_id}", status_code=200)
async def get_img(username: str, img_id: str):

try:
Expand All @@ -45,21 +45,15 @@ async def get_img(username: str, img_id: str):
raise HTTPException(status_code=404, detail=e.args)


@img_router.delete("/img/{username}/{img_name}", status_code=200)
@img_router.delete("/{username}/{img_name}", status_code=200)
async def delete_img(username: str, img_name: str):

try:
delete_img(username, img_name)
return

except ValueError as e:
raise HTTPException(status_code=404, detail="User does not exist")

except FileNotFoundError as e:
raise HTTPException(status_code=404, detail="Image does not exist")

except Exception as e:
raise HTTPException(status_code=500, detail="Internal server error")
raise HTTPException(status_code=404, detail=e.args)



2 changes: 1 addition & 1 deletion fishsense_services/routes/usr/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
login_router = APIRouter()


# possible routes: reset passwrod, need to check if there is account associated with it
# possible routes: need to check if there is account associated with it

@login_router.post("/api/login")
def verify_token():
Expand Down
76 changes: 76 additions & 0 deletions fishsense_services/routes/usr/usr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from fishsense_database.user import create_user, get_user, update_user, delete_user, get_all_users
from fastapi import APIRouter, Request, HTTPException
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder

usr_router = APIRouter()

@usr_router.post("/create", status_code=200)
async def create_usr(request: Request):

data = await request.json()
username = data["username"]
email = data["email"]

try:
id = create_user(username, email)

if not id:
raise ValueError("User with this username or email already exists.")

return JSONResponse(
status_code=200,
content={"message": "User created successfully."}
)

except ValueError as e:
print(f"ValueError caught in FastAPI: {e}") # Log the error for debugging
return JSONResponse(
status_code=400,
content={"detail": str(e)} # Send the error message as string
)




@usr_router.get("/user={username}", status_code=200)
async def get_usr(username: str):

try:
if not username:
raise ValueError("Must provide a username.")
user = get_user(username)

if not user:
raise ValueError("User does not exist.")

return JSONResponse(
status_code=200,
content={"user details": jsonable_encoder(user)}
)

except ValueError as e:

return JSONResponse(
status_code=400,
content={"detail": str(e)} # Send the error message as string
)

@usr_router.get("/", status_code=200)
async def all_usrs():

try:
users = get_all_users()

return JSONResponse(
status_code=200,
content={"users": jsonable_encoder(users)}
)

except ValueError as e:

return JSONResponse(
status_code=400,
content={"detail": str(e)} # Send the error message as string
)

86 changes: 79 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ fastapi = "^0.115.3"
google-auth = "^2.35.0"
requests = "^2.32.3"
uvicorn = "^0.32.0"
psycopg2 = "^2.9.10"
backoff = "^2.2.1"
gitpython = "^3.1.43"

[tool.poetry.group.dev.dependencies]
black = "^24.3.0"
Expand Down

0 comments on commit 1a43e58

Please sign in to comment.