Skip to content

Commit

Permalink
feat: sarabwayu mvp 제작 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
taewan2002 committed Feb 15, 2024
1 parent a2a1642 commit 6154e62
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SERVER_TYPE=local
ROOT_PATH=
DB_URL=localhost
HOUSE_REC_URL=https://sarabwayu5.hackathon.sparcs.net/
HOUSE_REC_URL=https://sarabwayu6.hackathon.sparcs.net/
2 changes: 1 addition & 1 deletion .env-prod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SERVER_TYPE=prod
ROOT_PATH=/api
DB_URL=mysql-container
HOUSE_REC_URL=https://sarabwayu5.hackathon.sparcs.net/
HOUSE_REC_URL=https://sarabwayu6.hackathon.sparcs.net/
7 changes: 7 additions & 0 deletions .idea/dataSources.xml

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

1 change: 0 additions & 1 deletion app/core/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pydantic import BaseSettings


class Settings(BaseSettings):
SERVER_TYPE: str
ROOT_PATH: str
Expand Down
40 changes: 30 additions & 10 deletions app/db/database.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import json

import jwt
from fastapi import HTTPException, status, Depends
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session

from app.core.config import settings
from app.db.models import get_Base, User
import aioredis
from fastapi.security import OAuth2PasswordBearer
from fastapi.security.api_key import APIKeyHeader
API_KEY_NAME = "Authorization"
api_key_header_auth = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
api_key_header_auth = APIKeyHeader(name="Authorization", auto_error=False)


DB_URL = f'mysql+pymysql://root:0000@{settings.DB_URL}/sarabwayu'
Expand All @@ -25,12 +24,12 @@ def get_Base():
def get_SessionLocal():
return SessionLocal

# async def get_redis_client() -> aioredis.Redis:
# redis = aioredis.from_url(f"redis://localhost:6379/0", encoding="utf-8", decode_responses=True)
# try:
# yield redis
# finally:
# await redis.close()
async def get_redis_client() -> aioredis.Redis:
redis = aioredis.from_url(f"redis://{settings.DB_URL}:6379/0", encoding="utf-8", decode_responses=True)
try:
yield redis
finally:
await redis.close()

def get_db() -> Session:
db = SessionLocal()
Expand All @@ -51,9 +50,22 @@ def save_db(data, db):
detail="데이터베이스에 오류가 발생했습니다."
)


async def user_to_json(user):
return json.dumps(
{
"id": user.id,
"nickname": user.nickname,
"phone": user.phone,
"is_deleted": user.is_deleted,
}
)


async def get_current_user(
api_key: str = Depends(api_key_header_auth),
db: Session = Depends(get_db),
redis: aioredis.Redis = Depends(get_redis_client)
) -> User:

if api_key is None:
Expand All @@ -79,7 +91,15 @@ async def get_current_user(
headers={"WWW-Authenticate": "Bearer"},
)



nickname: str = payload.get("sub")
user_info = await redis.get(f"user:{nickname}")
if user_info:
return User(**json.loads(user_info))

user = db.query(User).filter(User.nickname == nickname).first()

await redis.set(f"user:{nickname}", await user_to_json(user), ex=3600)

return user
1 change: 0 additions & 1 deletion app/db/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from sqlalchemy import Column, Integer, Text, ForeignKey, String, Boolean, DateTime, func, JSON, Date, FLOAT
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
import pytz
Expand Down
10 changes: 9 additions & 1 deletion app/router/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from fastapi import APIRouter, Depends

from app.db.database import get_current_user
from app.schemas.request import Auth
from app.schemas.response import ApiResponse
from app.service.auth import AuthService
Expand All @@ -15,4 +16,11 @@ async def get_auth_login(
):
return ApiResponse(
data=await auth_service.login(auth_data)
)
)

@router.get("/info", response_model=ApiResponse, tags=["Auth"])
async def get_auth_info(
user: Annotated[get_current_user, Depends()]
):
user.hashed_password = None
return ApiResponse(data=user)
2 changes: 1 addition & 1 deletion app/router/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

router = APIRouter(prefix="/chat")

@router.post("/", response_model=ApiResponse, tags=["Chat"])
@router.post("", response_model=ApiResponse, tags=["Chat"])
async def post_chat(
chat_data: Chat,
chat_service: Annotated[ChatService, Depends()]
Expand Down
18 changes: 13 additions & 5 deletions app/router/house.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Annotated

from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, BackgroundTasks

from app.schemas.request import House
from app.schemas.response import ApiResponse
Expand All @@ -20,25 +20,33 @@ async def post_house_create(
house_data: House,
house_service: Annotated[HouseService, Depends()]
):
print(house_data.house_info)
return ApiResponse()
return ApiResponse(data=await house_service.create(house_data))
@router.patch("/like/{house_id}", response_model=ApiResponse, tags=["House"])
async def patch_house_like(
house_id: int,
house_service: Annotated[HouseService, Depends()]
):
return ApiResponse(data=await house_service.like(house_id))

@router.get("/detail/{house_id}", response_model=ApiResponse, tags=["House"])
async def patch_house_detail(
house_id: int,
house_service: Annotated[HouseService, Depends()]
):
return ApiResponse(data=await house_service.detail(house_id))

@router.get("/recommendation/list/{page}", response_model=ApiResponse, tags=["House"])
async def get_house_recommendation(
page: int,
background_tasks: BackgroundTasks,
house_service: Annotated[HouseService, Depends()]
):
return ApiResponse(data=await house_service.recommendation_list(page))
return ApiResponse(data=await house_service.recommendation_list(background_tasks, page))

@router.get("/list/{page}", response_model=ApiResponse, tags=["House"])
async def get_house_list(
page: int,
background_tasks: BackgroundTasks,
house_service: Annotated[HouseService, Depends()]
):
return ApiResponse(data=await house_service.list(page))
return ApiResponse(data=await house_service.list(background_tasks, page))
1 change: 0 additions & 1 deletion app/service/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import aioredis
from fastapi import Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.db.database import get_db, save_db
Expand Down
2 changes: 1 addition & 1 deletion app/service/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ async def check_format(data):
)
save_db(recommendation, self.db)

return return_data
return {"rank": rank_data, "reason": return_data}
Loading

0 comments on commit 6154e62

Please sign in to comment.