diff --git a/.env b/.env deleted file mode 100644 index 7fc89a2..0000000 --- a/.env +++ /dev/null @@ -1,3 +0,0 @@ -API_V1_STR="/api/v1" -PROJECT_NAME="FastAPI" -SQLALCHEMY_DATABASE_URI="postgresql://localhost:5432/fastapi_db" diff --git a/.gitignore b/.gitignore index 5f6025a..0da46cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ - +# # Created by https://www.toptal.com/developers/gitignore/api/python,vscode # Edit at https://www.toptal.com/developers/gitignore?templates=python,vscode @@ -152,11 +152,16 @@ Thumbs.db ### vscode ### -.vscode/* +.vscode !.vscode/settings.json !.vscode/tasks.json !.vscode/launch.json !.vscode/extensions.json *.code-workspace -# End of https://www.toptal.com/developers/gitignore/api/python,vscode \ No newline at end of file +# End of https://www.toptal.com/developers/gitignore/api/python,vscode +.idea/ + +.vscode/ + +.env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b03c4a8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.8 + +WORKDIR /code +ADD . /code +COPY ./requirements.txt /code/requirements.txt +RUN pip install -r requirements.txt +COPY . /code +ENV DOCKERIZE_VERSION v0.6.1 + +CMD ["python3", "-m", "app.main"] \ No newline at end of file diff --git a/README.md b/README.md index d2358d4..2c52178 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,42 @@ -# ML-Team-Backend -ML Team Backend \ No newline at end of file +# Studeep API Server✨ +### **내 꿈을 향해 깊은 곳으로, 스터딥** + +학습 모니터링을 통해 몰입환경을 만들어주는 공부 습관 개선 서비스 + +**집에서 집중하기, 잘하고 계신가요?** + +코로나 19로 온라인 개학과 원격 수업이 자리 잡은 환경에서, 집에서 일과 공부를 새로운 방식으로 하는 걸 쉽게 확인할 수 있어요. 이제는 집이라는 공간에서 학업과 업무 그리고 휴식까지 24시간을 함께 하는 공간이 되었고, 그곳에서 각자 공부와 일에 집중하기 위해 큰 노력을 기울이고 있어요. + +그래서 "집이라는 공간에서도 몰입할 수 있는 환경을 어떻게 만들 수 있을까?"라는 질문에서, 스터딥이 시작되었어요. 학습 모니터링을 통해 5초면 몰입할 수 있는 환경을 만들어드릴게요. + +## Live Service +[Studeep](https://www.studeep.com) + +## Getting Started +``` +// 의존성 설치 +pip install -r requirements.txt + +// DB Migration +PYTHONPATH=. alembic revision --autogenerate -m "migration name" +PYTHONPATH=. alembic upgrade head + +// 서버 실행 +python3 -m app.main +``` + + + +## Production +![](./images/studeep_product.png) + +## Architecture +![](./images/studeep_architecture.png) + +## Modeling +![](./images/studeep_modeling.png) + +## API Docs +[Swagger](https://www.studeep.com/docs) + + diff --git a/app/api/__init__.py b/app/api/__init__.py index e69de29..0f85cbc 100644 --- a/app/api/__init__.py +++ b/app/api/__init__.py @@ -0,0 +1,36 @@ +from fastapi import APIRouter, Depends + +from app.api import users, study_rooms, my_studies, reports +from app.core import ( + study_rooms_settings, + user_settings, + my_studies_settings, + report_settings + ) +from app.service import auth_token + + +api_router = APIRouter() +api_router.include_router( + users.router, + prefix=user_settings.API_USER, + tags=['users'] +) +api_router.include_router( + router = study_rooms.router, + prefix = study_rooms_settings.API_STUDY_ROOM, + # dependencies = [ Depends(auth_token) ], + tags = [ 'study_rooms' ] +) +api_router.include_router( + router = my_studies.router, + prefix = my_studies_settings.API_MY_STUDY, + # dependencies = [ Depends(auth_token) ], + tags = [ 'my_studies' ] +) +api_router.include_router( + router = reports.router, + prefix = report_settings.API_REPORT, + # dependencies = [ Depends(auth_token) ], + tags = [ 'reports' ] +) diff --git a/app/api/my_studies.py b/app/api/my_studies.py new file mode 100644 index 0000000..c3fcd54 --- /dev/null +++ b/app/api/my_studies.py @@ -0,0 +1,100 @@ +import traceback + +from fastapi import ( + APIRouter, + Depends, + status + ) +from fastapi.responses import JSONResponse +from sqlalchemy.orm.session import Session + +from app.api.deps import get_db +from app.crud import my_studies +from app.schemas import ( + ErrorResponseBase, + GetMyStudiesResponse, + NotFoundMyStudiesHandling + ) +from app.errors import ( + get_detail, + NoSuchElementException + ) + + +router = APIRouter() + + +@router.get( + '', + responses = { + 200: { + "model": GetMyStudiesResponse, + "description": "마이스터디 조회 성공" + }, + 500: { + "model": ErrorResponseBase, + "description": "서버에서 잡지 못한 에러" + } + } +) +def get_my_studies(date: str, user_id: int, db: Session = Depends(get_db)): + try: + data = my_studies.get(db, date, user_id) + return JSONResponse( + status_code = status.HTTP_200_OK, + content = {'data': data} + ) + + except NoSuchElementException: + return JSONResponse( + status_code = status.HTTP_200_OK, + content = {'data': []} + ) + + except Exception as error: + print(traceback.print_exc()) + return JSONResponse( + status_code = status.HTTP_500_INTERNAL_SERVER_ERROR, + content = {'detail': f'server error: {error}'} + ) + + finally: + db.close() + + +@router.get( + '/{my_study_id}', + responses = { + 200: { + "model": GetMyStudiesResponse, + "description": "마이스터디 조회 성공" + }, + 500: { + "model": ErrorResponseBase, + "description": "서버에서 잡지 못한 에러" + } + } +) +def get_my_studies(my_study_id: int, db: Session = Depends(get_db)): + try: + data = my_studies.get_by_id(db, my_study_id) + return JSONResponse( + status_code = status.HTTP_200_OK, + content = {'data': data} + ) + + except NoSuchElementException: + return JSONResponse( + status_code = status.HTTP_200_OK, + content = {'data': []} + ) + + except Exception as error: + print(traceback.print_exc()) + return JSONResponse( + status_code = status.HTTP_500_INTERNAL_SERVER_ERROR, + content = {'detail': f'server error: {error}'} + ) + + finally: + db.close() diff --git a/app/api/reports.py b/app/api/reports.py new file mode 100644 index 0000000..06a0f05 --- /dev/null +++ b/app/api/reports.py @@ -0,0 +1,63 @@ +import traceback + +from fastapi import ( + APIRouter, + Depends, + status + ) +from fastapi.responses import JSONResponse +from sqlalchemy.orm.session import Session + +from app.api.deps import get_db +from app.crud import reports +from app.schemas import ( + ErrorResponseBase, + GetReportReponse, + NotFoundReportHandling + ) +from app.errors import ( + get_detail, + NoSuchElementException + ) + + +router = APIRouter() + + +@router.get( + '', + responses = { + 200: { + "model": GetReportReponse, + "description": "레포트 조회 성공" + }, + 500: { + "model": ErrorResponseBase, + "description": "서버에서 잡지 못한 에러" + } + } +) +def get_report(date: str, user_id: int, db: Session=Depends(get_db)): + try: + data = reports.get(db, user_id, date) + return JSONResponse( + status_code = status.HTTP_200_OK, + content = {'data': data} + ) + + except NoSuchElementException: + return JSONResponse( + status_code = status.HTTP_200_OK, + content = {'data': []} + ) + + except Exception as error: + print(traceback.print_exc()) + return JSONResponse( + status_code = status.HTTP_500_INTERNAL_SERVER_ERROR, + content = {'detail': f'server error: {error}'} + ) + + finally: + db.close() + diff --git a/app/api/study_rooms.py b/app/api/study_rooms.py new file mode 100644 index 0000000..596457e --- /dev/null +++ b/app/api/study_rooms.py @@ -0,0 +1,317 @@ +import traceback + +from typing import Optional +from fastapi import ( + APIRouter, + Depends, + status + ) +from fastapi.responses import JSONResponse +from sqlalchemy.orm.session import Session + +from app.api.deps import get_db +from app.crud import study_rooms +from app.schemas import ( + SuccessResponseBase, + ErrorResponseBase, + StudyRoomsCreate, + StudyRoomsUpdate, + StudyRoomJoin, + GetStudyRoomResponse, + GetStudyRoomsResponse, + NotFoundStudyRoomHandling, + NotFoundUserHandling, + PasswordNeedyStudyRoomHandling, + BodyNeedyStudyRoomHandling, + QueryNeedyStudyRoomHandling, + MethodNotAllowedHandling, + NoEmptyRoomHandling, + ForbiddenUserHandling, + ForbiddenPasswordHandling, + AlreadyJoinedHandling + ) +from app.errors import ( + get_detail, + NoSuchElementException, + InvalidArgumentException, + RequestConflictException, + RequestInvalidException, + ForbiddenException + ) + + +router = APIRouter() + + +@router.get( + '/{room_id}', + responses = { + 200: { + "model": GetStudyRoomResponse, + "description": "스터디룸 조회 성공" + }, + 500: { + "model": ErrorResponseBase, + "description": "서버에서 잡지 못한 에러" + } + } +) +def get_study_room(room_id: str, db: Session = Depends(get_db)): + try: + data = study_rooms.get(db, room_id) + return JSONResponse(status_code=status.HTTP_200_OK, content={'data': data}) + + except NoSuchElementException: + return JSONResponse(status_code=status.HTTP_200_OK, content={'data': []}) + + except Exception as error: + print(traceback.print_exc()) + return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={'detail': f'server error: {error}'}) + + finally: + db.close() + + +@router.patch( + '/{room_id}', + responses = { + 200: { + "model": SuccessResponseBase, + "description": "스터디룸 수정 성공" + }, + 403: { + "model": ForbiddenUserHandling, + "description": "본인이 만들지 않은 방을 수정하려는 경우" + }, + 404: { + "model": NotFoundStudyRoomHandling, + "description": "수정을 시도한 스터디룸이 이미 존재하지 않는 경우" + }, + 405: { + "model": MethodNotAllowedHandling, + "description": "엔드포인트 경로에 스터디룸 아이디가 넘어오지 않을 경우" + }, + 422: { + "model": PasswordNeedyStudyRoomHandling, + "description": "비공개 스터디룸으로 설정하고 비밀번호를 입력하지 않은 경우" + }, + 500: { + "model": ErrorResponseBase, + "description": "서버에서 잡지 못한 에러" + } + } +) +def update_study_room(room_id: str, room_info: StudyRoomsUpdate, db: Session = Depends(get_db)): + try: + study_rooms.update(db, room_id, room_info) + return JSONResponse(status_code=status.HTTP_200_OK, content={'data': ''}) + + except ForbiddenException as forbidden_err: + message = forbidden_err.message + detail = get_detail(param='database', field='user', message=message, err='invalid') + return JSONResponse(status_code=status.HTTP_403_FORBIDDEN, content={'detail': detail}) + + except InvalidArgumentException as argument_err: + message = argument_err.message + detail = get_detail(param='body', field='password', message=message, err='value_error') + return JSONResponse(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, content={'detail': detail}) + + except NoSuchElementException as element_err: + message = element_err.message + detail = get_detail(param='database', field='study room', message=message, err='database') + return JSONResponse(status_code=status.HTTP_404_NOT_FOUND, content={'detail': detail}) + + except Exception as error: + print(traceback.print_exc()) + return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={'detail': f'server error: {error}'}) + + +@router.delete( + '/{room_id}', + responses = { + 200: { + "model": SuccessResponseBase, + "description": "스터디룸 삭제 성공" + }, + 403: { + "model": ForbiddenUserHandling, + "description": "본인이 만들지 않은 방을 삭제하려는 경우" + }, + 404: { + "model": NotFoundStudyRoomHandling, + "description": "삭제를 시도한 스터디룸이 이미 존재하지 않는 경우" + }, + 405: { + "model": MethodNotAllowedHandling, + "description": "엔드포인트 경로에 스터디룸 아이디가 넘어오지 않을 경우" + }, + 500: { + "model": ErrorResponseBase, + "description": "서버에서 잡지 못한 에러" + } + } +) +def delete_study_room(user_id: int, room_id: str, db: Session = Depends(get_db)): + try: + study_rooms.remove(db, room_id, user_id) + return JSONResponse(status_code=status.HTTP_200_OK, content={'data': ''}) + + except ForbiddenException as forbidden_err: + message = forbidden_err.message + detail = get_detail(param='database', field='user', message=message, err='invalid') + return JSONResponse(status_code=status.HTTP_403_FORBIDDEN, content={'detail': detail}) + + except NoSuchElementException as element_err: + message = element_err.message + detail = get_detail(param='database', field='study room', message=message, err='database') + return JSONResponse(status_code=status.HTTP_404_NOT_FOUND, content={'detail': detail}) + + except Exception as error: + print(traceback.print_exc()) + return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={'detail': f'server error: {error}'}) + + +@router.get( + '', + responses = { + 200: { + "model": GetStudyRoomsResponse, + "description": "스터디룸 조희 성공" + }, + 422: { + "model": QueryNeedyStudyRoomHandling, + "description": "쿼리 파라미터를 제대로 전달하지 않은 경우" + }, + 500: { + "model": ErrorResponseBase, + "description": "서버에서 잡지 못한 에러" + } + } +) +def get_study_rooms( + skip: Optional[int] = None, + limit: Optional[int] = None, + owner_id: Optional[int] = None, + option: Optional[str] = 'created_at', + db: Session = Depends(get_db) +): + try: + data = study_rooms.get_multi(db, skip, limit, owner_id, option) + return JSONResponse(status_code=status.HTTP_200_OK, content={'data': data}) + + except NoSuchElementException: + return JSONResponse(status_code=status.HTTP_200_OK, content={'data': []}) + + except Exception as error: + print(traceback.print_exc()) + return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={'detail': f'server error: {error}'}) + + +@router.post( + '', + responses = { + 200: { + "model": SuccessResponseBase, + "description": "스터디룸 생성 성공" + }, + 404: { + "model": NotFoundUserHandling, + "description": "방을 만드려는 사용자가 존재하지 않는 경우 (Postman 등을 통한 악용 방지)" + }, + 422: { + "model": BodyNeedyStudyRoomHandling, + "description": "제목, 설명과 같이 필요한 스터디룸 정보를 입력하지 않은 경우" + }, + 500: { + "model": ErrorResponseBase, + "description": "서버에서 잡지 못한 에러" + } + } +) +def create_study_room(room_info: StudyRoomsCreate, db: Session = Depends(get_db)): + try: + study_rooms.create(db, room_info) + return JSONResponse(status_code=status.HTTP_200_OK, content={'data': ''}) + + except InvalidArgumentException as argument_err: + message = argument_err.message + detail = get_detail(param='body', field='password', message=message, err='value_error') + return JSONResponse(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, content={'detail': detail}) + + except NoSuchElementException as element_err: + message = element_err.message + detail = get_detail(param='database', field='user', message='not found', err='database') + return JSONResponse(status_code=status.HTTP_404_NOT_FOUND, content={'detail': detail}) + + except Exception as error: + print(traceback.print_exc()) + return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={'detail': f'server error: {error}'}) + + +@router.post( + '/{room_id}/join-check', + responses = { + 200: { + "model": SuccessResponseBase, + "description": "스터디룸 생성 성공" + }, + 400: { + "model": AlreadyJoinedHandling, + "description": "이미 스터디룸에 접속했는데 접속 시도하는 경우" + }, + 403: { + "model": ForbiddenPasswordHandling, + "description": "비공개 방의 비밀번호 오입력 한 경우" + }, + 404: { + "model": NotFoundStudyRoomHandling, + "description": "존재하지 않는 방에 접근하려는 경우" + }, + 409: { + "model": NoEmptyRoomHandling, + "description": "이미 스터디룸의 참가 인원 수가 5명인 경우" + }, + 422: { + "model": PasswordNeedyStudyRoomHandling, + "description": "비공개 스터디룸일 때 비밀번호를 입력하지 않은 경우 \ + 만약 공개된 스터디룸인데 비밀번호와 넘어올 경우 `msg` 부분에 `field not required` 반환" + }, + 500: { + "model": ErrorResponseBase, + "description": "서버에서 잡지 못한 에러" + } + } +) +def join_study_room(room_id: str, room_info: StudyRoomJoin, db: Session = Depends(get_db)): + try: + study_rooms.join(db, room_id, room_info) + return JSONResponse(status_code=status.HTTP_200_OK, content={'data': ''}) + + except RequestInvalidException as invalid_err: + message = invalid_err.message + detail = get_detail(param='database', field='user', message=message, err='invalid') + return JSONResponse(status_code=status.HTTP_400_BAD_REQUEST, content={'detail': detail}) + + except ForbiddenException as forbidden_err: + message = forbidden_err.message + detail = get_detail(param='body', field='password', message=message, err='invalid') + return JSONResponse(status_code=status.HTTP_403_FORBIDDEN, content={'detail': detail}) + + except NoSuchElementException as element_err: + message = element_err.message + detail = get_detail(param='database', field='study room', message=message, err='database') + return JSONResponse(status_code=status.HTTP_404_NOT_FOUND, content={'detail': detail}) + + except RequestConflictException as conflict_err: + message = conflict_err.message + detail = get_detail(param='database', field='study room', message=message, err='database') + return JSONResponse(status_code=status.HTTP_409_CONFLICT, content={'detail': detail}) + + except InvalidArgumentException as argument_err: + message = argument_err.message + detail = get_detail(param='body', field='password', message=message, err='value_error') + return JSONResponse(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, content={'detail': detail}) + + except Exception as error: + print(traceback.print_exc()) + return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={'detail': f'server error: {error}'}) diff --git a/app/api/users.py b/app/api/users.py new file mode 100644 index 0000000..900bbeb --- /dev/null +++ b/app/api/users.py @@ -0,0 +1,202 @@ +import logging +import traceback +from datetime import timedelta +from typing import Optional + +from fastapi import APIRouter, Depends, Header, Response +from fastapi.encoders import jsonable_encoder +from jose import JWTError +from sqlalchemy.orm import Session +from fastapi import status +from fastapi.responses import JSONResponse + +from app.crud import users +from app.api.deps import get_db +from app.errors import get_detail, NoSuchElementException +from app.schemas import ( + SuccessResponseBase, + ErrorResponseBase, + UserDataResponse, + UserCreate, + NotFoundUserHandling, + UnauthorizedHandler, + ForbiddenHandler + ) +from app.service import auth +from app.core import user_settings + +router = APIRouter() + + +@router.post( + "/signup", + responses = { + 200: { + "model": UserDataResponse, + "description": "회원가입 성공" + }, + 401: { + "model": UnauthorizedHandler, + "description": "JWT 토큰 인증에 실패하였을 경우" + }, + 403: { + "model": ForbiddenHandler, + "description": "올바른 유형의 토큰이 아닌 경우(on-board / access)" + }, + 500: { + "model": ErrorResponseBase, + "description": "Generic Error" + } + } +) +def sign_up(*, db: Session = Depends(get_db), + user_in: UserCreate, + authorization: Optional[str] = Header(None)): + try: + email = auth.check_access_token_valid(authorization, on_board=True) + user_in.social_id = email + user = users.create(db, obj_in=user_in) + token = auth.create_access_token({"sub": email}, timedelta(minutes=user_settings.ACCESS_TOKEN_EXPIRE_MINUTES)) + return JSONResponse(status_code=status.HTTP_200_OK, + content={'data': jsonable_encoder(user)}, + headers={'Authorization' : 'bearer ' + token}) + except JWTError: + message = traceback.format_exc() + detail = get_detail(param='token', field='authorize', message=message, err='invalid token') + return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={'detail': detail}) + except NameError: + message = traceback.format_exc() + detail = get_detail(param='token', field='forbidden', message=message, err='This token is not on-boarding token') + return JSONResponse(status_code=status.HTTP_403_FORBIDDEN, content={'detail': detail}) + except Exception as error: + logging.error(traceback.format_exc()) + return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + content={'detail': f'server error: {traceback.format_exc()}'}) + finally: + db.close() + + +@router.get( + "/signin", + responses = { + 200: { + "model": UserDataResponse, + "description": "로그인 성공" + }, + 401: { + "model": UnauthorizedHandler, + "description": "Google 토큰 인증에 실패하였을 경우" + }, + 404: { + "model": NotFoundUserHandling, + "description": "user가 존재하지 않는 경우" + }, + 500: { + "model": ErrorResponseBase, + "description": "Generic Error" + } + } +) +def sign_in(*, db: Session = Depends(get_db), + authorization: Optional[str] = Header(None) + ): + try: + # raise JWT Error + email = auth.auth_google_token(authorization) + user = users.get_one_by_email(db, email) + + token = auth.create_access_token({"sub": email}, timedelta(minutes=user_settings.ACCESS_TOKEN_EXPIRE_MINUTES)) + + if user is None: + token = auth.create_access_token({"on_board": email}, + timedelta(minutes=user_settings.ACCESS_TOKEN_EXPIRE_MINUTES)) + return JSONResponse(status_code=status.HTTP_404_NOT_FOUND, + content={'detail': "user(email: " + email + ") not exist."}, + headers={"Authorization": 'bearer ' + token} + ) + + return JSONResponse(status_code=status.HTTP_200_OK, + content={'data': jsonable_encoder(user)}, + headers={"Authorization": 'bearer ' + token}) + except JWTError: + message = traceback.format_exc() + detail = get_detail(param='token', field='authorize', message=message, err='invalid Google token') + return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={'detail': detail}) + except Exception as error: + logging.error(traceback.format_exc()) + return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + content={'detail': f'server error: {traceback.format_exc()}'}) + finally: + db.close() + + +@router.get( + "/get", + responses = { + 200: { + "model": UserDataResponse, + "description": "정상 Response" + }, + 401: { + "model": UnauthorizedHandler, + "description": "JWT 토큰 인증에 실패하였을 경우" + }, + 403: { + "model": ForbiddenHandler, + "description": "올바른 유형의 토큰이 아닌 경우(on-board / access)" + }, + 404: { + "model": NotFoundUserHandling, + "description": "user가 존재하지 않는 경우" + }, + 500: { + "model": ErrorResponseBase, + "description": "Generic Error" + } + } +) +def get_user( + *, + db: Session = Depends(get_db), + authorization: Optional[str] = Header(None) +): + try: + # raise JWT Error + email = auth.check_access_token_valid(authorization) + user = users.get_one_by_email(db, email) + + if user is None: + raise NoSuchElementException("user(email: " + email + ") not exist.") + + return JSONResponse(status_code=status.HTTP_200_OK, content={'data': jsonable_encoder(user)}) + except JWTError: + message = traceback.format_exc() + detail = get_detail(param='token', field='authorize', message=message, err='invalid Google token') + return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={'detail': detail}) + except NameError: + message = traceback.format_exc() + detail = get_detail(param='token', field='forbidden', message=message, + err='This token is not access token') + return JSONResponse(status_code=status.HTTP_403_FORBIDDEN, content={'detail': detail}) + except KeyError: + message = traceback.format_exc() + detail = get_detail(param='token', field='forbidden', message=message, + err='This token is not access token') + return JSONResponse(status_code=status.HTTP_403_FORBIDDEN, content={'detail': detail}) + except Exception as error: + logging.error(traceback.format_exc()) + return JSONResponse(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + content={'detail': f'server error: {traceback.format_exc()}'}) + finally: + db.close() + + +# todo: 서비스 시작 시, 필히 제거! +@router.get('/test/{email}/{token_type}', response_model=SuccessResponseBase, description='토큰 발급 백도어') +def back_door_access(email: str, token_type: str): + if token_type == 'onboard': + token = auth.create_access_token({"on_board": email}, timedelta(minutes=user_settings.ACCESS_TOKEN_EXPIRE_MINUTES)) + else: + token = auth.create_access_token({"sub": email}, timedelta(minutes=user_settings.ACCESS_TOKEN_EXPIRE_MINUTES)) + + return JSONResponse(status_code=status.HTTP_200_OK, content={'token': token}) diff --git a/app/api/v1/__init__.py b/app/api/v1/__init__.py deleted file mode 100644 index c1f8349..0000000 --- a/app/api/v1/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -from fastapi import APIRouter - -from app.api.v1 import products - -api_router = APIRouter() -api_router.include_router(products.router, prefix="/products", tags=["products"]) diff --git a/app/api/v1/products.py b/app/api/v1/products.py deleted file mode 100644 index 540524c..0000000 --- a/app/api/v1/products.py +++ /dev/null @@ -1,57 +0,0 @@ -from typing import Any, List - -from fastapi import APIRouter, Depends, HTTPException, status -from sqlalchemy.orm import Session - -from app import schemas, crud -from app.api.deps import get_db - -router = APIRouter() - - -@router.get("", response_model=List[schemas.ProductResponse]) -def read_products(db: Session = Depends(get_db), skip: int = 0, limit: int = 100) -> Any: - """ - Retrieve all products. - """ - products = crud.product.get_multi(db, skip=skip, limit=limit) - return products - - -@router.post("", response_model=schemas.ProductResponse) -def create_product(*, db: Session = Depends(get_db), product_in: schemas.ProductCreate) -> Any: - """ - Create new products. - """ - product = crud.product.create(db, obj_in=product_in) - return product - - -@router.put("", response_model=schemas.ProductResponse) -def update_product(*, db: Session = Depends(get_db), product_in: schemas.ProductUpdate) -> Any: - """ - Update existing products. - """ - product = crud.product.get(db, model_id=product_in.id) - if not product: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="The product with this ID does not exist in the system.", - ) - product = crud.product.update(db, db_obj=product, obj_in=product_in) - return product - - -@router.delete("", response_model=schemas.Message) -def delete_product(*, db: Session = Depends(get_db), id: int) -> Any: - """ - Delete existing product. - """ - product = crud.product.get(db, model_id=id) - if not product: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="The product with this ID does not exist in the system.", - ) - crud.product.remove(db, model_id=product.id) - return {"message": f"Product with ID = {id} deleted."} diff --git a/app/core/__init__.py b/app/core/__init__.py index 15f329a..f2223c5 100644 --- a/app/core/__init__.py +++ b/app/core/__init__.py @@ -1 +1,12 @@ -from app.core.config import settings +from app.core.config import ( + common_settings, + develop_settings, + deploy_settings, + study_rooms_settings, + user_settings, + my_studies_settings, + report_settings, + socket_settings, + time_settings, + redis_settings + ) \ No newline at end of file diff --git a/app/core/config.py b/app/core/config.py index eca9651..66d05ad 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -1,16 +1,92 @@ import secrets +from datetime import timedelta from pydantic import BaseSettings -class Settings(BaseSettings): - API_V1_STR: str = "/api/v1" - PROJECT_NAME: str = "FastAPI" +class CommonSettings(BaseSettings): + COMMON_API: str = '/api' + PROJECT_NAME: str = "studeep" SECRET_KEY: str = secrets.token_urlsafe(32) - SQLALCHEMY_DATABASE_URI: str = "postgresql://localhost:5432/fastapi_db" + SQLALCHEMY_DATABASE_URI: str = "postgresql://localhost:5432/studeep" class Config: env_file = ".env" -settings = Settings() +class DevelopSettings(BaseSettings): + ALLOW_ORIGIN: list = [ + 'https://www.studeep.com/', + 'https://api.studeep.com/', + 'https://studeep.com/', + 'http://localhost/', + 'http://localhost:3000/', + 'http://localhost:8000/' + ] + ALLOW_CREDENTIAL: bool = True + ALLOW_METHODS: list = ['*'] + ALLOW_HEADERS: list = ['*'] + ALLOW_HOST: list = ['*'] + ALLOW_EXPOSE_HEADERS: list = ['*'] + + +class DeploySettings(BaseSettings): + ALLOW_ORIGIN: list = [ + # 'https://www.studeep.com/', + # 'https://api.studeep.com/', + # 'https://studeep.com/', + # 'http://localhost/', + # 'http://localhost:3000/', + # 'http://localhost:8000/' + '*' + ] + ALLOW_CREDENTIAL: bool = True + ALLOW_METHODS: list = ['*'] + ALLOW_HEADERS: list = ['*'] + ALLOW_HOST: list = ['*.studeep.com', 'localhost'] + ALLOW_EXPOSE_HEADERS: list = ['Authorization', 'authorization'] + + +class UserSettings(BaseSettings): + API_USER: str = '/user' + SECRET_KEY: str = secrets.token_urlsafe(32) + ALGORITHM = "HS256" + ACCESS_TOKEN_EXPIRE_MINUTES = 300000 + + +class StudyRoomSettings(BaseSettings): + API_STUDY_ROOM: str = '/study-rooms' + MIN_CAPACITY: int = 0 + MAX_CAPACITY: int = 5 + + +class MyStudySettings(BaseSettings): + API_MY_STUDY: str = '/my-studies' + + +class ReportSettings(BaseSettings): + API_REPORT: str = '/reports' + + +class SocketSettings(BaseSettings): + NAMESPACE_URL: str = '/study' + + +class TimeSettings(BaseSettings): + KST = timedelta(hours=9) + +class RedisSettings(BaseSettings): + HOST = "27.96.131.49" + PORT = 6000 + + +common_settings = CommonSettings() +develop_settings = DevelopSettings() +deploy_settings = DeploySettings() +user_settings = UserSettings() +study_rooms_settings = StudyRoomSettings() +my_studies_settings = MyStudySettings() +report_settings = ReportSettings() +socket_settings = SocketSettings() +time_settings = TimeSettings() +redis_settings = RedisSettings() diff --git a/app/crud/__init__.py b/app/crud/__init__.py index e47c4c5..a36396c 100644 --- a/app/crud/__init__.py +++ b/app/crud/__init__.py @@ -1 +1,7 @@ -from app.crud.product import product +from app.crud.users import users +from app.crud.study_rooms import study_rooms +from app.crud.statuses import statuses +from app.crud.my_studies import my_studies +from app.crud.reports import reports +from app.crud.redis_function import redis_function + diff --git a/app/crud/my_studies.py b/app/crud/my_studies.py new file mode 100644 index 0000000..7fe8c10 --- /dev/null +++ b/app/crud/my_studies.py @@ -0,0 +1,146 @@ +from uuid import UUID +from datetime import datetime +from fastapi.encoders import jsonable_encoder +from sqlalchemy import and_, func +from sqlalchemy.orm import Session +from sqlalchemy.exc import IntegrityError + +from app.crud.base import CRUDBase +from app.models import MyStudies, Reports, Statuses, StudyRooms +from app.schemas import MyStudiesCreate, MyStudiesUpdate +from app.errors import NoSuchElementException +from app.core import time_settings, my_studies_settings + + +class CRUDMyStudy(CRUDBase[MyStudies, MyStudiesCreate, MyStudiesUpdate]): + def get(self, db: Session, date: str, user_id: int): + # TODO: 3차 Dev Camp 이후 구현 사항. 관련 쿼리 수정 필요. + date = datetime.strptime(date, '%Y-%m-%d') + instance = db.query( + self.model + ).filter(and_( + Reports.date == date, + Reports.user_id == user_id + )).outerjoin( + StudyRooms, + StudyRooms.id == self.model.study_room_id + ).with_entities( + self.model.id, + self.model.started_at, + self.model.ended_at, + self.model.total_time, + self.model.star_count, + self.model.study_room_id, + StudyRooms.title, + ).all() + + data = jsonable_encoder(instance) + + if data: + for my_study in data: + my_study['statuses'] = jsonable_encoder( + db.query(Statuses).filter( + Statuses.my_study_id == my_study['id'] + ).with_entities( + Statuses.id, + Statuses.type, + Statuses.count, + Statuses.time, + ).all() + ) + return jsonable_encoder(data) + else: + raise NoSuchElementException(message='not found') + + def get_by_id(self, db: Session, my_study_id: int): + try: + data = db.query(self.model).filter( + self.model.id == my_study_id + ).outerjoin( + Reports, + Reports.id == self.model.report_id + ).with_entities( + self.model.id, + Reports.date.label('date'), + self.model.total_time + ).first() + + return jsonable_encoder(data) + + except ValueError: + raise NoSuchElementException(message='not found') + + + + def create(self, db: Session, room_id: str, report_id: int): + try: + if not room_id: + raise NoSuchElementException(message='not found') + + study_room = db.query(StudyRooms).filter( + StudyRooms.id == UUID(room_id) + ).first() + + study_room.current_join_counts += 1 + + instance = self.model( + study_room_id = UUID(room_id), + report_id = report_id, + started_at = datetime.utcnow() + time_settings.KST + ) + db.add(instance) + db.commit() + db.refresh(instance) + return jsonable_encoder(instance) + + except AttributeError: + raise NoSuchElementException(message='not found') + + except ValueError: + raise NoSuchElementException(message='not found') + + except IntegrityError: + raise NoSuchElementException(message='not found') + + finally: + db.close() + + + def update(self, db: Session, id: int): + try: + instance = db.query(self.model).filter( + self.model.id == id + ).first() + + statuses = db.query(Statuses).filter( + Statuses.my_study_id == instance.id + ).with_entities( + func.sum(Statuses.time).label('total_time') + ).first() + + + if instance: + print('statuses', jsonable_encoder(statuses)) + print('before ended_at', jsonable_encoder(instance)) + instance.ended_at = datetime.utcnow() + time_settings.KST + print('after ended_at: ', jsonable_encoder(instance)) + instance.total_time = ( + instance.ended_at - instance.started_at + ).seconds + + if jsonable_encoder(statuses)['total_time']: + instance.total_time -= jsonable_encoder(statuses)['total_time'] + + db.commit() + db.refresh(instance) + return jsonable_encoder(instance) + + except Exception as error: + print(error) + raise Exception + + finally: + db.close() + + +my_studies = CRUDMyStudy(MyStudies) \ No newline at end of file diff --git a/app/crud/product.py b/app/crud/product.py deleted file mode 100644 index 1d77012..0000000 --- a/app/crud/product.py +++ /dev/null @@ -1,15 +0,0 @@ -from typing import Optional, List - -from sqlalchemy.orm import Session - -from app.crud.base import CRUDBase -from app.models.product import Product -from app.schemas import ProductCreate, ProductUpdate - - -class CRUDProduct(CRUDBase[Product, ProductCreate, ProductUpdate]): - # Declare model specific CRUD operation methods. - pass - - -product = CRUDProduct(Product) diff --git a/app/crud/redis_function.py b/app/crud/redis_function.py new file mode 100644 index 0000000..65e9cbd --- /dev/null +++ b/app/crud/redis_function.py @@ -0,0 +1,161 @@ +import time + +import redis +from datetime import timedelta +from collections.abc import MutableMapping + +from app.core import redis_settings,user_settings + + +def __setflat_skeys__( + r: redis.Redis, + obj: dict, + prefix: str, + delim: str = ":", + *, + _autopfix="" +) -> None: + allowed_vtypes = (str, bytes, float, int) + for key, value in obj.items(): + key = _autopfix + key + if isinstance(value, allowed_vtypes): + r.set(f"{prefix}{delim}{key}", value) + elif isinstance(value, MutableMapping): + __setflat_skeys__( + r, value, prefix, delim, _autopfix=f"{key}{delim}" + ) + else: + raise TypeError(f"Unsupported value type: {type(value)}") + + +def study_room_starting(): + return { + "status": "study", + "study_rooms": "", + "last_access": 1, + "sleep": { + "count": 0, + "sec": 0 + }, + "phone": { + "count": 0, + "sec": 0 + }, + "await": { + "count": 0, + "sec": 0 + }, + "rest": { + "count": 0, + "sec": 0 + } + } + + +def __generate_value_string__(user_id, user_monitoring, disturb_info=None): + if disturb_info is None: + return f"{user_id}:{user_monitoring}" + return f"{user_id}:{user_monitoring}:{disturb_info}" + + +class redis_function: + redis = redis.Redis(port=redis_settings.PORT, host=redis_settings.HOST, charset="utf-8", decode_responses=True) + # redis = redis.Redis(port=6000, host='localhost', charset="utf-8", decode_responses=True) + + def __del__(self): + self.redis.close() + + def logout(self, token: str, user_email: str): + self.redis.setex(token, timedelta(minutes=user_settings.ACCESS_TOKEN_EXPIRE_MINUTES), user_email) + + def check_black_list(self, token: str): + self.redis.exists(token) + + def start_study_init(self, user_id: str): + __setflat_skeys__(self.redis, study_room_starting(), user_id) + + def set_study_room(self, user_id: str, study_room_id: str): + self.redis.set(__generate_value_string__(user_id, "study_rooms"), study_room_id) + + def get_user_study_info(self, user_id, user_monitoring, disturb_info=None): + return self.redis.get( + __generate_value_string__(user_id, user_monitoring, disturb_info) + ) + + def check_join(self, user_id: str): + return self.redis.get( + __generate_value_string__(user_id, 'study_rooms') + ) + + def add_current_log(self, user_id, disturb_type, disturb_time): + # status를 업데이트 해야함 + # 이 때, 이전 상태를 저장해야 함. + status_key = __generate_value_string__(user_id, "status") + last_status = self.redis.get(status_key) + self.redis.set(status_key, disturb_type) + + # 이전 상태가 study가 아닐 경우 last_access와 비교하여 시간을 알아내야 함. + last_access_key = __generate_value_string__(user_id, "last_access") + last_access = self.redis.get(last_access_key) + + self.redis.set(last_access_key, disturb_time) + + print(f"User(id : {user_id}) update status({last_status}-> {disturb_type}).") + + if last_status != "study": + status_time = disturb_time - float(last_access) + self.redis.incr(__generate_value_string__(user_id, last_status, 'count'), 1) + self.redis.incr(__generate_value_string__(user_id, last_status, 'sec'), int(status_time)) + print(f"Save Status Log({last_status} {int(status_time)}sec.)") + + def delete_user_study_info(self, user_id: str): + target_key = self.redis.keys(f"{user_id}:*") + for key in target_key: + self.redis.delete(key) + + def end_study(self, user_id: str): + if not self.check_join(user_id): + return None + + self.add_current_log(user_id, "study", time.time()) + result = self.get_user_all_info(user_id) + self.delete_user_study_info(user_id) + + return result + + def get_status_value(self, user_id: str, status: str): + return { + "count": self.redis.get(__generate_value_string__(user_id, status, 'count')), + "sec": self.redis.get(__generate_value_string__(user_id, status, 'sec')) + } + + def get_user_all_info(self, user_id: str): + study_room_id = self.redis.get(__generate_value_string__(user_id, "study_rooms")) + sleep_values = self.get_status_value(user_id, "sleep") + phone_values = self.get_status_value(user_id, "phone") + await_values = self.get_status_value(user_id, "await") + rest_values = self.get_status_value(user_id, "rest") + + return { + "study_room_id": study_room_id, + "sleep": sleep_values, + "smartphone": phone_values, + "await": await_values, + "rest": rest_values + } + + +if __name__ == "__main__": + r = redis_function() + + r.start_study_init(1, "test") + + time.sleep(3) + r.add_current_log(1, "phone", time.time()) + time.sleep(4) + r.add_current_log(1, "sleep", time.time()) + time.sleep(3) + r.add_current_log(1, "study", time.time()) + r.add_current_log(1, "rest", time.time()) + + print(r.end_study(1)) diff --git a/app/crud/reports.py b/app/crud/reports.py new file mode 100644 index 0000000..72eda38 --- /dev/null +++ b/app/crud/reports.py @@ -0,0 +1,109 @@ +from datetime import datetime +from sqlalchemy import and_, func, not_ +from sqlalchemy.orm import Session +from fastapi.encoders import jsonable_encoder + +from app.models import Reports, Statuses +from app.schemas import ReportsCreate, ReportsUpdate +from app.crud.base import CRUDBase +from app.errors import NoSuchElementException +from app.core import time_settings + + +class CRUDReport(CRUDBase[Reports, ReportsCreate, ReportsUpdate]): + def get(self, db: Session, user_id: int, date: str): + date = datetime.strptime(date, '%Y-%m-%d') + instance = db.query( + self.model, + ).filter(and_( + self.model.user_id == user_id, + self.model.date == date, + self.model.total_time != 0 + )).outerjoin( + Statuses, + Statuses.report_id == self.model.id + ).with_entities( + self.model.id, + self.model.date, + self.model.achievement, + self.model.concentration, + self.model.total_time, + self.model.total_star_count, + func.sum(Statuses.time).filter(not_( + Statuses.type == 'rest' + )).label('total_status_time') + ).group_by(self.model.id).first() + + if instance: + report = jsonable_encoder(instance) + statuses = db.query(Statuses).filter( + Statuses.report_id == report['id'] + ).with_entities( + Statuses.type.label('name'), + func.sum(Statuses.count).label('total_count'), + func.sum(Statuses.time).label('value') + ).group_by(Statuses.type).all() + report['statuses'] = jsonable_encoder(statuses) + report['max_status'] = [status['name'] for status in report['statuses'] if status['value'] == max(report['statuses'], key=lambda x: x['value'])['value']] + return [report] + else: + raise NoSuchElementException(message='not found') + + + def get_or_create(self, db: Session, user_id: int): + try: + today = datetime.utcnow() + time_settings.KST + if (today.hour >= 0) and (today.hour < 5): + date = datetime(today.year, today.month, today.day - 1) + else: + date = datetime(today.year, today.month, today.day) + + instance = db.query(self.model).filter(and_( + self.model.user_id == user_id, + self.model.date == date + )).first() + + if instance: + return jsonable_encoder(instance) + else: + instance = self.model(user_id = user_id, date = date) + db.add(instance) + db.commit() + db.refresh(instance) + return jsonable_encoder(instance) + + except Exception as error: + print(error) + raise Exception + + finally: + db.close() + + + def update(self, db: Session, id: int, total_time: int): + try: + instance = db.query(self.model).filter( + self.model.id == id + ).first() + + statuses = db.query(Statuses).filter( + Statuses.report_id == instance.id + ).with_entities( + func.sum(Statuses.time).filter(not_( + Statuses.type == 'rest' + )).label('total_time') + ).first() + + if instance: + instance.total_time += total_time + instance.concentration = 100 - int(jsonable_encoder(statuses)['total_time'] / instance.total_time * 100) + db.commit() + + except: + raise Exception + + finally: + db.close() + + +reports = CRUDReport(Reports) \ No newline at end of file diff --git a/app/crud/statuses.py b/app/crud/statuses.py new file mode 100644 index 0000000..da54e5f --- /dev/null +++ b/app/crud/statuses.py @@ -0,0 +1,65 @@ +from fastapi.encoders import jsonable_encoder +from sqlalchemy import and_ +from sqlalchemy.orm import Session + +from app.crud.base import CRUDBase +from app.models import Statuses +from app.schemas import StatusCreate, StatusUpdate + +class CRUDStatus(CRUDBase[Statuses, StatusCreate, StatusUpdate]): + def create(self, db: Session, statuses: dict): + try: + # instance = db.bulk_insert_mappings(self.model, statuses) + db.commit() + + except Exception as error: + print(error) + raise Exception + + finally: + db.close() + + + def update_or_create( + self, + db: Session, + type: str, + cnt: int, + time: int, + my_study_id: int, + report_id: int + ): + try: + instance = db.query(self.model).filter(and_( + self.model.type == type, + self.model.my_study_id == my_study_id, + self.model.report_id == report_id + )).first() + + if instance: + instance.count += cnt + instance.time += time + + else: + instance = self.model( + type = type, + count = cnt, + time = time, + my_study_id = my_study_id, + report_id = report_id + ) + db.add(instance) + + db.commit() + db.refresh(instance) + + return jsonable_encoder(instance) + + except: + raise Exception + + finally: + db.close() + + +statuses = CRUDStatus(Statuses) \ No newline at end of file diff --git a/app/crud/study_rooms.py b/app/crud/study_rooms.py new file mode 100644 index 0000000..d36847a --- /dev/null +++ b/app/crud/study_rooms.py @@ -0,0 +1,253 @@ +from datetime import datetime +from uuid import UUID +from typing import Union, Optional +from datetime import datetime +from fastapi.encoders import jsonable_encoder +from sqlalchemy import and_ +from sqlalchemy.orm import Session +from sqlalchemy.exc import IntegrityError + +from app.core import study_rooms_settings +from app.crud.base import CRUDBase +from app.models import StudyRooms +from app.schemas import ( + StudyRoomsCreate, + StudyRoomsUpdate, + StudyRoomJoin + ) +from app.errors import ( + NoSuchElementException, + InvalidArgumentException, + RequestConflictException, + RequestInvalidException, + ForbiddenException + ) +from app.core import time_settings + +from app.crud.redis_function import redis_function + + +MAX_CAPACITY = study_rooms_settings.MAX_CAPACITY +redis_session = redis_function() + +def check_password_exist(room_info: Union[StudyRoomsCreate, StudyRoomsUpdate]): + return False if (not room_info.is_public) and (not room_info.password) else True + + +class CRUDStudyRoom(CRUDBase[StudyRooms, StudyRoomsCreate, StudyRoomsUpdate]): + def get(self, db: Session, room_id: UUID): + try: + data = db.query(self.model).filter( + self.model.id == UUID(room_id) + ).with_entities( + self.model.id, + self.model.title, + self.model.style, + self.model.description, + self.model.is_public, + self.model.current_join_counts, + self.model.created_at, + self.model.owner_id + ).first() + + if data: + return [jsonable_encoder(data)] + else: + raise NoSuchElementException(message='not found') + + except ValueError: + raise NoSuchElementException(message='not found') + + + def get_multi( + self, + db: Session, + skip: Optional[int], + limit: Optional[int], + owner_id: Optional[int], + option: Optional[str] + ): + query = db.query(self.model) + if owner_id: + query = query.filter(self.model.owner_id == owner_id) + + data = query.filter( + self.model.current_join_counts < MAX_CAPACITY + ).with_entities( + self.model.id, + self.model.title, + self.model.style, + self.model.description, + self.model.is_public, + self.model.current_join_counts, + self.model.created_at, + self.model.owner_id + ).order_by(f'study_room_{option}').offset(skip).limit(limit).all() + + if data: + return jsonable_encoder(data) + else: + raise NoSuchElementException(message='not found') + + + def create(self, db: Session, room_info: StudyRoomsCreate): + try: + if not check_password_exist(room_info): + raise InvalidArgumentException(message='field required') + + # room_info.current_join_counts += 1 + room_info.created_at = datetime.utcnow() + time_settings.KST + data = self.model(**jsonable_encoder(room_info)) + db.add(data) + db.commit() + + except IntegrityError: + raise NoSuchElementException(message='not found') + + + def update(self, db: Session, room_id: str, room_info: StudyRoomsUpdate): + try: + if not check_password_exist(room_info): + raise InvalidArgumentException(message='field required') + + update_data = room_info.dict(exclude_none=True) + data = db.query(self.model).filter(and_( + self.model.id == UUID(room_id), + self.model.owner_id == room_info.owner_id + )).update(update_data) + + if data: + db.commit() + else: + raise ForbiddenException(message='forbidden') + + except ValueError: + raise NoSuchElementException(message='not found') + + + def remove(self, db: Session, room_id: str, user_id: int): + try: + data = db.query(self.model).filter(and_( + self.model.id == UUID(room_id), + self.model.owner_id == user_id + )).first() + + if data: + db.delete(data) + db.commit() + else: + raise ForbiddenException(message='forbidden') + + except ValueError: + raise NoSuchElementException(message='not found') + + + def join(self, db: Session, room_id: str, room_info: StudyRoomJoin): + try: + if redis_session.check_join(room_info.user_id): + raise RequestInvalidException(message='Already Connect a Study-Room') + + data = db.query(self.model).filter( + self.model.id == UUID(room_id) + ).first() + study_room = jsonable_encoder(data) + + if study_room: + if study_room['current_join_counts'] >= MAX_CAPACITY: + raise RequestConflictException(message='no empty') + + if study_room['owner_id'] != room_info.user_id: + if study_room['is_public']: + if room_info.password: + raise InvalidArgumentException(message='field not required') + else: + if not room_info.password: + raise InvalidArgumentException(message='field required') + elif room_info.password != study_room['password']: + raise ForbiddenException(message='forbidden') + + # data.current_join_counts += 1 + + print(f"join study-room(id : {room_id}. current count : {data.current_join_counts})") + + db.commit() + + else: + raise NoSuchElementException(message='not found') + + except ValueError: + raise NoSuchElementException(message='not found') + + finally: + db.close() + + + def leave(self, db: Session, room_id: str): + try: + if not room_id: + raise NoSuchElementException(message='not found') + + study_room = db.query(self.model).filter( + self.model.id == UUID(room_id) + ) + + print(study_room.first().current_join_counts) + + if (int(study_room.first().current_join_counts) < 1): + raise RequestInvalidException(message='invalid request') + + study_room.update({'current_join_counts': self.model.current_join_counts - 1}) + db.commit() + + except AttributeError: + raise NoSuchElementException(message='not found') + + except ValueError: + raise NoSuchElementException(message='not found') + + except IntegrityError: + raise NoSuchElementException(message='not found') + + finally: + db.close() + + + def current_check(self, db: Session, user_id: int): + try: + """ + TODO + - Redis에 접근해서 user_id가 존재하는지 확인한다. + - 만약 있을 경우 공부 중이던 사람이기 때문에 ABNORMAL의 TIMESTAMP를 확인한다. + - 만약 5분이 지난 경우 삭제하고 Redis Initialize를 종료한다. + 이때 해당 사용자가 포함되어 있던 study_room의 current_join_count도 차감한다. + 이를 수행하기 위해 Redis에 room_id 또한 포함되어야 할 것으로 판단된다. + - 5분이 지나지 않은 경우 재접속 할 것인지 묻는다. + - user_id가 존재하지 않는 경우와 5분이 지난 경우 200 status_code로 응답한다. + - 만약 5분이 지나지 않아서 재접속 여부를 물어봐야 할 경우 409 conflict 응답한다. + """ + + pass + + except Exception: + raise Exception + + + def re_join(self, db: Session, user_id: int, is_re_joined: bool): + try: + """ + TODO + - current_check 엔드포인트에 이어서 만약 재접속하겠다고 한 경우 + data에 해당 room_id를 포함해서 보내준다. + 페이지 랜더링을 통해 해당 방으로 가야하기 때문이다. + - 만약 재접속하지 않겠다고 한 경우 data에 빈 문자열을 보내준다. + - 그리고 재접속하지 않겠다고 한 경우 해당 마이스터디 테이블을 삭제한다. + - 재접속과 그렇지 않은 경우 모두 status_code 200으로 응답한다. + """ + + pass + + except Exception: + raise Exception + + +study_rooms = CRUDStudyRoom(StudyRooms) \ No newline at end of file diff --git a/app/crud/users.py b/app/crud/users.py new file mode 100644 index 0000000..0bd43ba --- /dev/null +++ b/app/crud/users.py @@ -0,0 +1,31 @@ +from sqlalchemy.orm import Session +from fastapi.encoders import jsonable_encoder + +from app.crud.base import CRUDBase +from app.models import User +from app.schemas import UserCreate, UserUpdate +from app.errors import NoSuchElementException + + +class CRUDUser(CRUDBase[User, UserCreate, UserUpdate]): + def get(self, db: Session, user_id: int): + data = db.query(self.model).filter( + self.model.id == user_id + ).with_entities( + self.model.id, + self.model.nickname, + self.model.provider, + self.model.social_id + ).first() + + if data: + return jsonable_encoder(data) + else: + return NoSuchElementException(message='not found') + + + def get_one_by_email(self, db: Session, email: str): + return db.query(User).filter(User.social_id == email).first() + + +users = CRUDUser(User) diff --git a/app/database/__init__.py b/app/database/__init__.py index e69de29..9c85b3f 100644 --- a/app/database/__init__.py +++ b/app/database/__init__.py @@ -0,0 +1,2 @@ +from app.database.base_class import Base +from app.database.session import SessionLocal \ No newline at end of file diff --git a/app/database/base.py b/app/database/base.py index 3f25dd6..7ab4ad9 100644 --- a/app/database/base.py +++ b/app/database/base.py @@ -1,2 +1,2 @@ from app.database.base_class import Base -from app.models.product import Product +from app.models.users import User diff --git a/app/database/base_class.py b/app/database/base_class.py index 366f739..ee344ad 100644 --- a/app/database/base_class.py +++ b/app/database/base_class.py @@ -1,8 +1,9 @@ -from typing import Any - +from typing import Any +from sqlalchemy import inspect from sqlalchemy.ext.declarative import as_declarative, declared_attr + @as_declarative() class Base: id: Any diff --git a/app/database/session.py b/app/database/session.py index 9684b37..22368bb 100644 --- a/app/database/session.py +++ b/app/database/session.py @@ -1,7 +1,8 @@ -from sqlalchemy import create_engine +from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker -from app.core import settings +from app.core import common_settings -engine = create_engine(settings.SQLALCHEMY_DATABASE_URI, pool_pre_ping=True) + +engine = create_engine(common_settings.SQLALCHEMY_DATABASE_URI, pool_pre_ping=True) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) diff --git a/app/errors/__init__.py b/app/errors/__init__.py new file mode 100644 index 0000000..380471a --- /dev/null +++ b/app/errors/__init__.py @@ -0,0 +1,9 @@ +from app.errors.customs import ( + get_detail, + InternalException, + NoSuchElementException, + InvalidArgumentException, + RequestConflictException, + RequestInvalidException, + ForbiddenException + ) \ No newline at end of file diff --git a/app/errors/customs.py b/app/errors/customs.py new file mode 100644 index 0000000..dc42f9f --- /dev/null +++ b/app/errors/customs.py @@ -0,0 +1,66 @@ +def get_detail(param: str, field: str, message: str, err: str): + detail = [ + { + 'loc': [ + f'{param}', # ex. body + f'{field}' # ex. title + ], + "msg": message, # ex. field required, not found + "type": f"{err}.missing" # ex. value_error + } + ] + return detail + + +class InternalException(Exception): + def __init__(self, message: str): + self.message = message + + + def __str__(self): + return self.message + + +class NoSuchElementException(Exception): + def __init__(self, message: str): + self.message = message + + + def __str__(self): + return self.message + + +class InvalidArgumentException(Exception): + def __init__(self, message: str): + self.message = message + + + def __str__(self): + return self.message + + +class RequestConflictException(Exception): + def __init__(self, message: str): + self.message = message + + + def __str__(self): + return self.message + + +class ForbiddenException(Exception): + def __init__(self, message: str): + self.message = message + + + def __str__(self): + return self.message + + +class RequestInvalidException(Exception): + def __init__(self, message: str): + self.message = message + + + def __str__(self): + return self.message \ No newline at end of file diff --git a/app/main.py b/app/main.py index df384be..6fc7211 100644 --- a/app/main.py +++ b/app/main.py @@ -1,13 +1,49 @@ +import socketio import uvicorn -from fastapi import FastAPI +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware +from fastapi.middleware.trustedhost import TrustedHostMiddleware -from app.api.v1 import api_router -from app.core import settings +from app.api import api_router +from app.core import ( + common_settings, + develop_settings, + deploy_settings + ) +from app.service import StudyNamespace -app = FastAPI(title=settings.PROJECT_NAME) +server = FastAPI(title=common_settings.PROJECT_NAME) +sio = socketio.AsyncServer( + async_mode = 'asgi', + cors_allowed_origins = '*', + debug = True +) +sio.register_namespace(StudyNamespace(sio, '/study')) +sio_app = socketio.ASGIApp(socketio_server=sio, other_asgi_app=server) + +server.add_middleware( + CORSMiddleware, + allow_origins = deploy_settings.ALLOW_ORIGIN, + allow_credentials = deploy_settings.ALLOW_CREDENTIAL, + allow_methods = deploy_settings.ALLOW_METHODS, + allow_headers = deploy_settings.ALLOW_HEADERS, + expose_headers = deploy_settings.ALLOW_EXPOSE_HEADERS +) +server.add_middleware( + TrustedHostMiddleware, + allowed_hosts = deploy_settings.ALLOW_HOST, +) +server.add_websocket_route("/socket.io/", sio_app) +server.include_router(api_router, prefix=common_settings.COMMON_API) -app.include_router(api_router, prefix=settings.API_V1_STR) if __name__ == "__main__": - uvicorn.run(app, host="0.0.0.0", port=8000) + uvicorn.run( + 'app.main:server', + host = "0.0.0.0", + port = 8000, + reload = True, + ssl_keyfile = '/etc/letsencrypt/live/api.studeep.com/privkey.pem', + ssl_certfile = '/etc/letsencrypt/live/api.studeep.com/fullchain.pem' + ) diff --git a/app/models/__init__.py b/app/models/__init__.py index e049467..4dd1202 100644 --- a/app/models/__init__.py +++ b/app/models/__init__.py @@ -1 +1,5 @@ -from app.models.product import Product +from app.models.users import User +from app.models.study_rooms import StudyRooms, Style +from app.models.my_studies import MyStudies +from app.models.reports import Reports +from app.models.statuses import Statuses, StatusType diff --git a/app/models/my_studies.py b/app/models/my_studies.py new file mode 100644 index 0000000..de84252 --- /dev/null +++ b/app/models/my_studies.py @@ -0,0 +1,24 @@ +from sqlalchemy import ( + Column, + Integer, + ForeignKey, + TIMESTAMP + ) +from sqlalchemy.orm import relation +from sqlalchemy.dialects.postgresql import UUID + +from app.database import Base + + +class MyStudies(Base): + __tablename__ = 'my_studies' + id = Column('my_study_id', Integer(), primary_key=True, autoincrement=True) + started_at = Column('my_study_started_at', TIMESTAMP, nullable=False) + ended_at = Column('my_study_ended_at', TIMESTAMP, nullable=True) + total_time = Column('my_study_total_time', Integer(), default=0, nullable=True) + star_count = Column('my_study_star_count', Integer(), nullable=True) + report_id = Column(Integer(), ForeignKey('reports.report_id', ondelete='CASCADE')) + study_room_id = Column(UUID(as_uuid=True), ForeignKey('study_rooms.study_room_id', ondelete='CASCADE')) + report = relation('Reports', back_populates='my_studies') + study_room = relation('StudyRooms', back_populates='my_study') + status = relation('Statuses', back_populates='my_study') diff --git a/app/models/product.py b/app/models/product.py deleted file mode 100644 index ab5634e..0000000 --- a/app/models/product.py +++ /dev/null @@ -1,9 +0,0 @@ -from sqlalchemy import Column, Integer, String, Float - -from app.database.base_class import Base - - -class Product(Base): - id = Column(Integer, primary_key=True, index=True) - name = Column(String, nullable=False) - price = Column(Float, nullable=False) diff --git a/app/models/reports.py b/app/models/reports.py new file mode 100644 index 0000000..3f7673d --- /dev/null +++ b/app/models/reports.py @@ -0,0 +1,23 @@ +from sqlalchemy import ( + Column, + ForeignKey, + Integer, + Date + ) +from sqlalchemy.orm import relation + +from app.database import Base + + +class Reports(Base): + __tablename__ = 'reports' + id = Column('report_id', Integer(), primary_key=True, autoincrement=True) + date = Column('report_date', Date, nullable=False) + achievement = Column('report_achievement', Integer(), nullable=True) + concentration = Column('report_concentration', Integer(), nullable=True) + total_time = Column('report_total_time', Integer(), default=0, nullable=True) + total_star_count = Column('report_total_star_count', Integer(), default=0, nullable=True) + user_id = Column(Integer(), ForeignKey('users.user_id', ondelete='CASCADE')) + user = relation('User', back_populates='reports') + my_studies = relation('MyStudies', back_populates='report') + total_status = relation('Statuses', back_populates='report') diff --git a/app/models/statuses.py b/app/models/statuses.py new file mode 100644 index 0000000..3ea43bc --- /dev/null +++ b/app/models/statuses.py @@ -0,0 +1,30 @@ +import enum + +from sqlalchemy import ( + Column, + Enum, + Integer, + ForeignKey + ) +from sqlalchemy.orm import relation + +from app.database import Base + + +class StatusType(str, enum.Enum): + SMARTPHONE = 'smartphone' + AWAIT = 'await' + SLEEP = 'sleep' + REST = 'rest' + + +class Statuses(Base): + __tablename__ = 'statuses' + id = Column('status_id', Integer(), primary_key=True, autoincrement=True) + type = Column('status_type', Enum(StatusType), nullable=False) + count = Column('status_count', Integer(), nullable=False) + time = Column('status_time', Integer(), nullable=False) + my_study_id = Column(Integer(), ForeignKey('my_studies.my_study_id', ondelete='CASCADE')) + report_id = Column(Integer(), ForeignKey('reports.report_id', ondelete='CASCADE')) + my_study = relation('MyStudies', back_populates='status') + report = relation('Reports', back_populates='total_status') \ No newline at end of file diff --git a/app/models/study_rooms.py b/app/models/study_rooms.py new file mode 100644 index 0000000..37cb34d --- /dev/null +++ b/app/models/study_rooms.py @@ -0,0 +1,40 @@ +import enum + +from datetime import datetime +from uuid import uuid4 +from sqlalchemy import ( + Column, + ForeignKey, + String, + Boolean, + Integer, + SmallInteger, + DateTime, + Enum + ) +from sqlalchemy.orm import relation +from sqlalchemy.dialects.postgresql import UUID + +from app.database import Base + + +class Style(str, enum.Enum): + STYLE_1 = 'style_1' + STYLE_2 = 'style_2' + STYLE_3 = 'style_3' + STYLE_4 = 'style_4' + + +class StudyRooms(Base): + __tablename__ = 'study_rooms' + id = Column('study_room_id', UUID(as_uuid=True), primary_key=True, default=uuid4) + title = Column('study_room_title', String(64), nullable=False) + style = Column('study_room_style', Enum(Style), nullable=False) + description = Column('study_room_description', String(256), nullable=True) + is_public = Column('study_room_is_public', Boolean(), nullable=False) + password = Column('study_room_password', String(32), nullable=True) + current_join_counts = Column('study_room_current_join_counts', SmallInteger(), nullable=False, default=0) + created_at = Column('study_room_created_at', DateTime(), nullable=False) + owner_id = Column(Integer(), ForeignKey('users.user_id', ondelete='CASCADE')) + owner = relation('User', back_populates='study_rooms') + my_study = relation('MyStudies', back_populates='study_room') \ No newline at end of file diff --git a/app/models/users.py b/app/models/users.py new file mode 100644 index 0000000..f451d8d --- /dev/null +++ b/app/models/users.py @@ -0,0 +1,23 @@ +import enum + +from sqlalchemy import Column, Integer, Enum, String, JSON +from sqlalchemy.orm import relation +from sqlalchemy.dialects.postgresql import JSON + +from app.database.base_class import Base + + +class Provider(str, enum.Enum): + GOOGLE = 'google' + FACEBOOK = 'facebook' + + +class User(Base): + __tablename__ = 'users' + id = Column('user_id', Integer, primary_key=True, autoincrement=True) + provider = Column('user_provider', Enum(Provider), nullable=False) + social_id = Column('user_social_id', String, nullable=False, unique=True) + nickname = Column('user_nickname', String, nullable=False, unique=True) + goal = Column('user_goal', JSON, nullable=False) + study_rooms = relation('StudyRooms', back_populates = 'owner') + reports = relation('Reports', back_populates = 'user') \ No newline at end of file diff --git a/app/schemas/__init__.py b/app/schemas/__init__.py index fb3c44b..8efe305 100644 --- a/app/schemas/__init__.py +++ b/app/schemas/__init__.py @@ -1,2 +1,45 @@ -from app.schemas.message import Message -from app.schemas.product import ProductBase, ProductCreate, ProductUpdate, ProductResponse +from app.schemas.responses import ( + SuccessResponseBase, + ErrorResponseBase, + MethodNotAllowedHandling + ) +from app.schemas.users import ( + NotFoundUserHandling, + UnauthorizedHandler, + ForbiddenHandler, + UserDataResponse, + UserBase, + UserCreate, + UserUpdate + ) +from app.schemas.study_rooms import ( + StudyRoomsCreate, + StudyRoomsUpdate, + StudyRoomJoin, + GetStudyRoomResponse, + GetStudyRoomsResponse, + NotFoundStudyRoomHandling, + PasswordNeedyStudyRoomHandling, + BodyNeedyStudyRoomHandling, + QueryNeedyStudyRoomHandling, + NoEmptyRoomHandling, + ForbiddenUserHandling, + ForbiddenPasswordHandling, + AlreadyJoinedHandling + ) +from app.schemas.reports import ( + ReportsCreate, + ReportsUpdate, + GetReportReponse, + NotFoundReportHandling + ) +from app.schemas.my_studies import ( + MyStudiesCreate, + MyStudiesUpdate, + GetMyStudiesResponse, + NotFoundMyStudiesHandling + ) +from app.schemas.statuses import ( + StatusCreate, + StatusUpdate + ) diff --git a/app/schemas/message.py b/app/schemas/message.py deleted file mode 100644 index 8b65c80..0000000 --- a/app/schemas/message.py +++ /dev/null @@ -1,5 +0,0 @@ -from pydantic import BaseModel - - -class Message(BaseModel): - message: str diff --git a/app/schemas/my_studies/__init__.py b/app/schemas/my_studies/__init__.py new file mode 100644 index 0000000..6824ab1 --- /dev/null +++ b/app/schemas/my_studies/__init__.py @@ -0,0 +1,3 @@ +from app.schemas.my_studies.crud import MyStudiesCreate, MyStudiesUpdate +from app.schemas.my_studies.success import GetMyStudiesResponse +from app.schemas.my_studies.handling import NotFoundMyStudiesHandling diff --git a/app/schemas/my_studies/crud.py b/app/schemas/my_studies/crud.py new file mode 100644 index 0000000..17a39be --- /dev/null +++ b/app/schemas/my_studies/crud.py @@ -0,0 +1,24 @@ +from uuid import UUID +from datetime import date, datetime +from typing import Optional +from pydantic import BaseModel + + +class MyStudiesBase(BaseModel): + pass + + +class MyStudiesCreate(MyStudiesBase): + date: date + report_id: int + study_room_id: UUID + + class config: + schema_extra = { + + } + + +class MyStudiesUpdate(MyStudiesBase): + total_time: int + ended_at: datetime \ No newline at end of file diff --git a/app/schemas/my_studies/handling.py b/app/schemas/my_studies/handling.py new file mode 100644 index 0000000..bc2f2da --- /dev/null +++ b/app/schemas/my_studies/handling.py @@ -0,0 +1,19 @@ +from app.schemas.responses import ErrorResponseBase + + +class NotFoundMyStudiesHandling(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "database", + "my studies" + ], + "msg": "not found", + "type": "database.missing" + } + ] + } + } diff --git a/app/schemas/my_studies/success.py b/app/schemas/my_studies/success.py new file mode 100644 index 0000000..80f3c26 --- /dev/null +++ b/app/schemas/my_studies/success.py @@ -0,0 +1,51 @@ +from app.schemas.responses import SuccessResponseBase + + +class GetMyStudiesResponse(SuccessResponseBase): + class Config: + schema_extra = { + "example": { + "data": [ + { + "id": 5, + "started_at": "2021-05-25T22:55:16.874569", + "ended_at": "2021-05-25T22:59:16.555907", + "total_time": 239, + "star_count": None, + "study_room_id": "7ce741ef-5f97-46ec-9cf6-6eb6b6a4ee9a", + "title": "스터디룸 생성 테스트", + "disturbances": [ + { + "id": 5, + "type": "smartphone", + "count": 1, + "time": 8 + }, + { + "id": 9, + "type": "sleep", + "count": 1, + "time": 5 + } + ] + }, + { + "id": 6, + "started_at": "2021-05-25T23:00:10.040419", + "ended_at": "2021-05-25T23:02:49.544101", + "total_time": 159, + "star_count": None, + "study_room_id": "7ce741ef-5f97-46ec-9cf6-6eb6b6a4ee9a", + "title": "스터디룸 생성 테스트", + "disturbances": [ + { + "id": 6, + "type": "await", + "count": 1, + "time": 20 + } + ] + } + ] + } + } \ No newline at end of file diff --git a/app/schemas/product.py b/app/schemas/product.py deleted file mode 100644 index bcb4181..0000000 --- a/app/schemas/product.py +++ /dev/null @@ -1,24 +0,0 @@ -from typing import Optional - -from pydantic import BaseModel - - -class ProductBase(BaseModel): - id: Optional[int] - name: Optional[str] - price: Optional[float] - - -class ProductCreate(ProductBase): - name: str - price: float - - -class ProductUpdate(ProductBase): - id: int - pass - - -class ProductResponse(ProductBase): - class Config: - orm_mode = True diff --git a/app/schemas/reports/__init__.py b/app/schemas/reports/__init__.py new file mode 100644 index 0000000..59b6c99 --- /dev/null +++ b/app/schemas/reports/__init__.py @@ -0,0 +1,3 @@ +from app.schemas.reports.crud import ReportsCreate, ReportsUpdate +from app.schemas.reports.success import GetReportReponse +from app.schemas.reports.handling import NotFoundReportHandling diff --git a/app/schemas/reports/crud.py b/app/schemas/reports/crud.py new file mode 100644 index 0000000..3294809 --- /dev/null +++ b/app/schemas/reports/crud.py @@ -0,0 +1,15 @@ +from datetime import date +from pydantic import BaseModel + + +class ReportsBase(BaseModel): + date: date + user_id: int + + +class ReportsCreate(ReportsBase): + pass + + +class ReportsUpdate(ReportsBase): + pass \ No newline at end of file diff --git a/app/schemas/reports/handling.py b/app/schemas/reports/handling.py new file mode 100644 index 0000000..bba11ba --- /dev/null +++ b/app/schemas/reports/handling.py @@ -0,0 +1,19 @@ +from app.schemas.responses import ErrorResponseBase + + +class NotFoundReportHandling(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "database", + "report" + ], + "msg": "not found", + "type": "database.missing" + } + ] + } + } diff --git a/app/schemas/reports/success.py b/app/schemas/reports/success.py new file mode 100644 index 0000000..b0227d5 --- /dev/null +++ b/app/schemas/reports/success.py @@ -0,0 +1,38 @@ +from app.schemas.responses import SuccessResponseBase + + +class GetReportReponse(SuccessResponseBase): + class Config: + schema_extra = { + "example": { + "data": { + "id": 4, + "date": "2021-05-25", + "achievement": None, + "concentration": None, + "total_time": 518, + "total_star_count": 0, + "total_disturbance_counts": 4, + "statuses": [ + { + "name": "smartphone", + "value": 2, + "total_time": 20 + }, + { + "name": "await", + "value": 1, + "total_time": 20 + }, + { + "name": "sleep", + "value": 1, + "total_time": 5 + } + ], + "max_status": [ + "smartphone" + ] + } + } + } \ No newline at end of file diff --git a/app/schemas/responses.py b/app/schemas/responses.py new file mode 100644 index 0000000..2fec137 --- /dev/null +++ b/app/schemas/responses.py @@ -0,0 +1,34 @@ +from typing import Union + +from pydantic import BaseModel + + +class SuccessResponseBase(BaseModel): + data: Union[list, dict, None] + + class Config: + schema_extra = { + 'example': { + 'data': '' + } + } + + +class ErrorResponseBase(BaseModel): + detail: Union[dict, str] + + class Config: + schema_extra = { + 'example': { + 'detail': 'server error' + } + } + + +class MethodNotAllowedHandling(ErrorResponseBase): + class Config: + schema_extra = { + "example": { + "detail": "Method Not Allowed" + } + } \ No newline at end of file diff --git a/app/schemas/statuses/__init__.py b/app/schemas/statuses/__init__.py new file mode 100644 index 0000000..a1f02e6 --- /dev/null +++ b/app/schemas/statuses/__init__.py @@ -0,0 +1 @@ +from app.schemas.statuses.crud import StatusCreate, StatusUpdate \ No newline at end of file diff --git a/app/schemas/statuses/crud.py b/app/schemas/statuses/crud.py new file mode 100644 index 0000000..ba50033 --- /dev/null +++ b/app/schemas/statuses/crud.py @@ -0,0 +1,19 @@ +from pydantic import BaseModel + +from app.models import StatusType + + +class StatusBase(BaseModel): + pass + + +class StatusCreate(StatusBase): + type: StatusType + count: int + time: int + my_study_id: int + report_id: int + + +class StatusUpdate(StatusBase): + pass \ No newline at end of file diff --git a/app/tests/api/v1/__init__.py b/app/schemas/statuses/handling.py similarity index 100% rename from app/tests/api/v1/__init__.py rename to app/schemas/statuses/handling.py diff --git a/app/schemas/statuses/success.py b/app/schemas/statuses/success.py new file mode 100644 index 0000000..e69de29 diff --git a/app/schemas/study_rooms/__init__.py b/app/schemas/study_rooms/__init__.py new file mode 100644 index 0000000..e6c5250 --- /dev/null +++ b/app/schemas/study_rooms/__init__.py @@ -0,0 +1,19 @@ +from app.schemas.study_rooms.crud import ( + StudyRoomsCreate, + StudyRoomsUpdate, + StudyRoomJoin + ) +from app.schemas.study_rooms.success import ( + GetStudyRoomResponse, + GetStudyRoomsResponse + ) +from app.schemas.study_rooms.handling import ( + NotFoundStudyRoomHandling, + PasswordNeedyStudyRoomHandling, + BodyNeedyStudyRoomHandling, + QueryNeedyStudyRoomHandling, + NoEmptyRoomHandling, + ForbiddenPasswordHandling, + ForbiddenUserHandling, + AlreadyJoinedHandling + ) \ No newline at end of file diff --git a/app/schemas/study_rooms/crud.py b/app/schemas/study_rooms/crud.py new file mode 100644 index 0000000..3b7054a --- /dev/null +++ b/app/schemas/study_rooms/crud.py @@ -0,0 +1,58 @@ +from typing import Optional +from pydantic import BaseModel +from datetime import datetime + +from app.models import Style + + +class StudyRoomsBase(BaseModel): + password: Optional[str] + + +class StudyRoomsCreate(StudyRoomsBase): + title: str + description: Optional[str] + style: Style + is_public: bool + current_join_counts: int = 0 + created_at: Optional[datetime] + owner_id: int + + class Config: + schema_extra = { + 'example': { + 'title': '주 4시간 이상 고시 공부방 🔥', + 'style': 'style_2', + 'description': '같이 열심히 공부하실 분들만!', + 'is_public': False, + 'password': 'TestPassword!234', + 'owner_id': 1 + } + } + + +class StudyRoomsUpdate(StudyRoomsBase): + title: Optional[str] + description: Optional[str] + is_public: Optional[bool] + owner_id: int + + class Config: + schema_extra = { + 'example': { + 'description': '매일 매일 캠 스터디 가능하신 분들만!', + 'is_public': False, + 'password': 'TestPassword!234', + 'owner_id': 1 + } + } + + +class StudyRoomJoin(StudyRoomsBase): + user_id: int + class Config: + schema_extra = { + 'example': { + 'password': 'TestPassword!234' + } + } \ No newline at end of file diff --git a/app/schemas/study_rooms/handling.py b/app/schemas/study_rooms/handling.py new file mode 100644 index 0000000..87362dc --- /dev/null +++ b/app/schemas/study_rooms/handling.py @@ -0,0 +1,160 @@ +from app.schemas.responses import ErrorResponseBase + + +class NotFoundStudyRoomHandling(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "database", + "study room" + ], + "msg": "not found", + "type": "database.missing" + } + ] + } + } + + +class PasswordNeedyStudyRoomHandling(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "body", + "password" + ], + "msg": "field required", + "type": "value_error.missing" + } + ] + } + } + + +class BodyNeedyStudyRoomHandling(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "body", + "title" + ], + "msg": "field required", + "type": "value_error.missing" + }, + { + "loc": [ + "body", + "description" + ], + "msg": "field required", + "type": "value_error.missing" + } + ] + } + } + + +class QueryNeedyStudyRoomHandling(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "query", + "skip" + ], + "msg": "field required", + "type": "value_error.missing" + }, + { + "loc": [ + "query", + "limit" + ], + "msg": "field required", + "type": "value_error.missing" + } + ] + } + } + + +class NoEmptyRoomHandling(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "database", + "study room" + ], + "msg": "no empty", + "type": "database" + } + ] + } + } + + +class ForbiddenPasswordHandling(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "body", + "password" + ], + "msg": "forbidden", + "type": "invalid" + } + ] + } + } + + +class ForbiddenUserHandling(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "database", + "user" + ], + "msg": "forbidden", + "type": "invalid" + } + ] + } + } + +class AlreadyJoinedHandling(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "database", + "user" + ], + "msg": "Already Connect a Study-Room", + "type": "invalid" + } + ] + } + } \ No newline at end of file diff --git a/app/schemas/study_rooms/success.py b/app/schemas/study_rooms/success.py new file mode 100644 index 0000000..7ac6f48 --- /dev/null +++ b/app/schemas/study_rooms/success.py @@ -0,0 +1,49 @@ +from app.schemas.responses import SuccessResponseBase + + +class GetStudyRoomResponse(SuccessResponseBase): + class Config: + schema_extra = { + 'example': { + 'data': { + "is_public": False, + "style": "style_1", + "title": "스터디 룸 제목 수정", + "created_at": "2021-05-09T20:50:11.782727", + "id": "3d37627c-d87d-469e-8bf3-db7e796838cf", + "description": "스터디룸 설명", + "current_join_counts": 1, + "owner_id": 1 + } + } + } + + +class GetStudyRoomsResponse(SuccessResponseBase): + class Config: + schema_extra = { + 'example': { + 'data': [ + { + "title": "스터디 룸 제목 수정", + "style": "style_2", + "is_public": False, + "created_at": "2021-05-09T20:50:11.782727", + "description": "스터디룸 설명", + "id": "3d37627c-d87d-469e-8bf3-db7e796838cf", + "current_join_counts": 1, + "owner_id": 1 + }, + { + "title": "스터디룸 생성 제목", + "style": "style_2", + "is_public": True, + "created_at": "2021-05-09T20:50:11.782727", + "description": "스터디룸 설명", + "id": "3b199025-92d7-4214-8f9c-b8224d81fca5", + "current_join_counts": 3, + "owner_id": 1 + } + ] + } + } \ No newline at end of file diff --git a/app/schemas/users/__init__.py b/app/schemas/users/__init__.py new file mode 100644 index 0000000..fcc20f5 --- /dev/null +++ b/app/schemas/users/__init__.py @@ -0,0 +1,12 @@ +from app.schemas.users.handling import ( + NotFoundUserHandling, + UnauthorizedHandler, + ForbiddenHandler + ) +from app.schemas.users.crud import ( + UserBase, + UserCreate, + UserUpdate + ) + +from app.schemas.users.success import UserDataResponse diff --git a/app/schemas/users/crud.py b/app/schemas/users/crud.py new file mode 100644 index 0000000..488c57c --- /dev/null +++ b/app/schemas/users/crud.py @@ -0,0 +1,51 @@ +import enum +import json +from typing import Optional + +from pydantic import BaseModel +from sqlalchemy.dialects.postgresql import JSON + + +class UserBase(BaseModel): + id: Optional[int] + provider: Optional[str] + social_id: Optional[str] + nickname: Optional[str] + goal: Optional[dict] + + +class UserCreate(UserBase): + provider: str + nickname: str + + class Config: + schema_extra = { + 'example': { + 'provider': 'google', + 'nickname': 'Studeep_User' + } + } + + +class UserUpdate(UserBase): + id: int + pass + + class Config: + schema_extra = { + 'example': { + 'id': 1, + 'provider': 'google', + 'social_id': 'example@gmail.com', + 'nickname': 'new Nickname', + 'goal': { + 'MON': 2, + 'TUE': 2, + 'WED': 2, + 'THU': 2, + 'FRI': 2, + 'SAT': 2, + 'SUN': 2 + } + } + } diff --git a/app/schemas/users/handling.py b/app/schemas/users/handling.py new file mode 100644 index 0000000..d8264b0 --- /dev/null +++ b/app/schemas/users/handling.py @@ -0,0 +1,55 @@ +from app.schemas.responses import ErrorResponseBase + + +class NotFoundUserHandling(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "database", + "user" + ], + "msg": "not found", + "type": "database.missing" + } + ] + } + } + + +class UnauthorizedHandler(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "token", + "user" + ], + "msg": "Unauthorized", + "type": "Token Unauthorized" + } + ] + } + } + + +class ForbiddenHandler(ErrorResponseBase): + class Config: + schema_extra = { + 'example': { + "detail": [ + { + "loc": [ + "token", + "user" + ], + "msg": "Forbidden", + "type": "Forbidden Token" + } + ] + } + } \ No newline at end of file diff --git a/app/schemas/users/success.py b/app/schemas/users/success.py new file mode 100644 index 0000000..d8a016a --- /dev/null +++ b/app/schemas/users/success.py @@ -0,0 +1,22 @@ +from app.schemas import SuccessResponseBase + + +class UserDataResponse(SuccessResponseBase): + class Config: + schema_extra = { + 'example': { + 'id': 1, + 'provider': 'google', + 'social_id': 'example@gmail.com', + 'nickname': 'new Nickname', + 'goal': { + 'MON': 2, + 'TUE': 2, + 'WED': 2, + 'THU': 2, + 'FRI': 2, + 'SAT': 2, + 'SUN': 2 + } + } + } \ No newline at end of file diff --git a/app/service/__init__.py b/app/service/__init__.py new file mode 100644 index 0000000..0555554 --- /dev/null +++ b/app/service/__init__.py @@ -0,0 +1,2 @@ +from app.service.auth import auth_token +from app.service.sockets import StudyNamespace \ No newline at end of file diff --git a/app/service/auth.py b/app/service/auth.py new file mode 100644 index 0000000..68c7dd7 --- /dev/null +++ b/app/service/auth.py @@ -0,0 +1,80 @@ +import logging +import traceback + +import requests +from datetime import datetime, timedelta +from typing import Optional + +from fastapi import status, Header +from fastapi.responses import JSONResponse +from jose import jwt, JWTError, ExpiredSignatureError +from jose.exceptions import JWTClaimsError + +from app.core import user_settings +from app.errors import get_detail + + +def parsing_token_decorator(func): + def wrapper(token: str, **kwargs): + try: + return func(token.split(" ")[1], **kwargs) + except IndexError: + raise JWTError() + + return wrapper + + +# DI +def auth_token(authorization: Optional[str] = Header(None)): + try: + check_access_token_valid(authorization) + except JWTError: + message = traceback.format_exc() + detail = get_detail(param='token', field='authorize', message=message, err='invalid Google token') + return JSONResponse(status_code=status.HTTP_401_UNAUTHORIZED, content={'detail': detail}) + + +def create_access_token(data: dict, expires_delta: Optional[timedelta] = None): + to_encode = data.copy() + if expires_delta: + expire = datetime.utcnow() + expires_delta + else: + expire = datetime.utcnow() + timedelta(minutes=15) + to_encode.update({"exp": expire}) + encoded_jwt = jwt.encode(to_encode, user_settings.SECRET_KEY, algorithm=user_settings.ALGORITHM) + return encoded_jwt + + +@parsing_token_decorator +def auth_google_token(token: str): + result = requests.get("https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=" + token) + + if result.status_code == 200: + return result.json()["email"] + + else: + raise JWTError + + +@parsing_token_decorator +def check_access_token_valid(token: str, on_board=False): + try: + decode_token = jwt.decode(token, user_settings.SECRET_KEY, algorithms=[user_settings.ALGORITHM]) + if on_board: + return decode_token["on_board"] + return decode_token["sub"] + except ExpiredSignatureError as err: + logging.info("Token has expired") + # todo: Refresh Token Check + raise JWTError() + except JWTClaimsError: + logging.info("token has any claims") + raise JWTError() + except JWTError: + logging.info("Invalid Signature token") + raise JWTError() + + +def check_refresh_token(param): + # todo : 레디스 연결 이후 + pass diff --git a/app/service/sockets.py b/app/service/sockets.py new file mode 100644 index 0000000..72af03f --- /dev/null +++ b/app/service/sockets.py @@ -0,0 +1,349 @@ +import socketio +import traceback +import time + +from app.crud import ( + users, + study_rooms, + reports, + my_studies, + statuses, + redis_function + ) +from app.database import SessionLocal +from app.errors import NoSuchElementException, RequestInvalidException + + +clients = dict() + + +""" +To Do +- joinRoom, leaveRoom, status, disconnect 때 방에 입장해 있는 사용자 수를 알려줘야 한다. + clients 객체에 접근하여 room_id가 동일한 객체의 수를 세는 방법이 있다. + 또는 Redis에 저장하는 형태를 아예 바꾸는 방법이 있다. + 노동과 시간은 후자가 더 많이 들어가지만 효율성이나 속도를 생각했을 때는 더 좋은 것 같다. + 전자, 후자 모두 효율적으로 코드를 만들 방법을 고려 할 필요가 있다. +- 오류가 발생했을 때 자동으로 disconnect 되게 하는 게 좋을 것 같다. + 오류가 발생한 상황은 다시 말하면 허용되지 않은 방법으로 소켓에 접근하는 것이기도 하다. + response 이벤트로 이를 알려주기 보다는 바로 연결을 해제 시켜버리는 게 더 좋을 것 같다. +""" + + +class StudyNamespace(socketio.AsyncNamespace): + def __init__(self, sio, namespace, *args, **kwargs): + super(socketio.Namespace, self).__init__(namespace) + self.sio = sio + self.db = SessionLocal() + self.redis = redis_function() + + async def get_users(self, room_id): + users = [ + value for value in clients.values() if value['room_id'] == room_id + ] + return users + + async def on_connect(self, sid, environ, auth): + try: + print('connect') + user_instance = users.get( + db = self.db, + user_id = auth['user_id'] + ) + report_instance = reports.get_or_create( + db = self.db, + user_id = auth['user_id'] + ) + clients[sid] = { + 'user_id': auth['user_id'], + 'user_nickname': user_instance['nickname'], + 'status': 'study', + 'room_id': '', + 'report_id': report_instance['id'], + 'my_study_id': '', + } + + # redis init + self.redis.start_study_init(user_id = auth['user_id']) + + await self.emit( + 'response', + { + 'statusCode': 200, + 'message': 'SUCCESS', + 'eventName': 'connect', + 'data': {} + }, + namespace = self.namespace + ) + + print('connect success') + + + except Exception as error: + print(traceback.print_exc()) + await self.emit( + 'response', + { + 'statusCode': 500, + 'message': f'SERVER_ERROR_{error}', + 'eventName': 'connect', + 'data': {} + }, + namespace = self.namespace + ) + + + async def on_joinRoom(self, sid, room_id): + try: + print('join room') + print(room_id) + self.enter_room(sid=sid, room=room_id, namespace=self.namespace) + instance = my_studies.create( + db = self.db, + room_id = room_id, + report_id = clients[sid]['report_id'] + ) + clients[sid]['room_id'] = room_id + clients[sid]['my_study_id'] = instance['id'] + + # redis set study room + self.redis.set_study_room(user_id = clients[sid]['user_id'], study_room_id = room_id) + + # 사용자 수를 알려줄 방법에 대해서 생각해봐야 한다. + users = await self.get_users(room_id = room_id) + + await self.emit( + 'response', + { + 'statusCode': 200, + 'message': 'SUCCESS', + 'eventName': 'joinRoom', + 'data': users + }, + room = room_id, + namespace = self.namespace + ) + + print('join room success') + + except NoSuchElementException: + print('not found') + await self.emit( + 'response', + { + 'statusCode': 404, + 'message': 'NOT_FOUND', + 'eventName': 'joinRoom', + 'data': {} + }, + namespace = self.namespace + ) + + except Exception as error: + print(traceback.print_exc()) + await self.emit( + 'response', + { + 'statusCode': 500, + 'message': f'SERVER_ERROR_{error}', + 'eventName': 'joinRoom', + 'data': {} + }, + namespace = self.namespace + ) + + async def on_leaveRoom(self, sid, room_id): + try: + """ + Todo + - Disturbance 테이블 생성 필요 + - Redis 넘겨 받는 데이터의 형태에 따라 구현 메서드 변경 + - bulk_insert_mappings 사용시 속도는 훨씬 빠르다. + 하지만 배열 형태의 데이터가 들어가야 하며 각 리스트에는 객체로 disturbance 데이터가 들어가야 한다. + 또한 객체 내에 report_id 와 my_study_id 에 대한 정보도 포함되어 있어야 한다. + ex. [ {type: smartphone, time: 124, count: 2, report_id: 1, my_study_id: 2}, ... ] + - update_or_create 사용시 속도는 훨씬 느리다. (개별적인 데이터에 for loop을 돌려야 하기 때문) + 이때 장점은 type과 time만 Redis에서 get 하면 된다는 점이다. + - report_id, my_study_id는 이미 글로벌하게 clients 객체에 저장되어 있다. + """ + print('leave room') + + # 사용자 수를 알려줄 방법에 대해서 생각해봐야 한다. + await self.emit( + 'disconnect', + { + 'statusCode': 200, + 'message': 'SUCCESS', + 'eventName': 'leaveRoom', + 'data': {} + }, + room = room_id, + namespace = self.namespace + ) + await self.disconnect(sid, namespace=self.namespace) + + print('leave room success') + + except RequestInvalidException: + print('invalid request') + await self.emit( + 'response', + { + 'statusCode': 400, + 'message': 'INVALID_REQUEST', + 'eventName': 'leaveRoom', + 'data': {} + } + ) + + except NoSuchElementException: + print('not found') + await self.emit( + 'response', + { + 'statusCode': 404, + 'messgae': 'NOT_FOUND', + 'eventName': 'leaveRoom', + 'data': {} + }, + namespace = self.namespace + ) + + except Exception as error: + print(traceback.print_exc()) + await self.emit( + 'response', + { + 'statusCode': 500, + 'message': f'SERVER_ERROR_{error}', + 'eventName': 'leaveRoom', + 'data': {} + }, + namespace = self.namespace + ) + + + async def on_status(self, sid, status): + try: + """ + TODO: + - Client에서 disturbance 데이터에 room_id 포함하여 줘야 한다. + - response 이벤트로 message에 disturbance 상태를 보내줘야 한다. + - 개별 사용자의 휴식이 다르기 때문에 Redis에 휴식에 대한 것도 저장 할 필요가 있어 보인다. + """ + + self.redis.add_current_log(clients[sid]['user_id'], status, time.time()) + clients[sid]['status'] = status + users = await self.get_users(room_id = clients[sid]['room_id']) + + # 사용자 수를 알려줄 방법에 대해서 생각해봐야 한다. + await self.emit( + 'response', + { + 'statusCode': 200, + 'message': 'SUCCESS', + 'eventName': 'status', + 'data': users + }, + room = clients[sid]['room_id'], + namespace = self.namespace + ) + + except Exception as error: + print(traceback.print_exc()) + await self.emit( + 'response', + { + 'statusCode': 500, + 'message': f'SERVER_ERROR_{error}', + 'eventName': 'status', + 'data': {} + }, + namespace = self.namespace + ) + + + async def on_disconnect(self, sid): + try: + """ + TODO: + - leaveRoom을 발생시켜도 disconnect event가 발생할 것이다. + 비정상적인 종료와 차이점은 leaveRoom일 때는 clients 객체가 빈 객체다. + - Redis에 이미 해당 사용자의 id가 저장되어 있기 때문에 clients 등을 통해 user_id에 접근하여 + 발생 시점의 timstamp와 함께 ABNORMAL 타입을 저장한다. + """ + print('disconnect') + study_rooms.leave(self.db, room_id=clients[sid]['room_id']) + self.leave_room(sid=sid, room=clients[sid]['room_id'], namespace=self.namespace) + + result = self.redis.end_study(clients[sid]['user_id']) + + if result: + for status in ['sleep', 'smartphone', 'await', 'rest']: + await self.__create_status__(sid, status, result[status]) + + my_study = my_studies.update( + db = self.db, + id = clients[sid]['my_study_id'] + ) + reports.update( + db = self.db, + id = clients[sid]['report_id'], + total_time = my_study['total_time'] + ) + room_id = clients[sid]['room_id'] + clients.pop(sid) + print(f'sid: {sid}, clients: {clients}') + print('disconnect success') + users = await self.get_users(room_id = room_id) + + # 사용자 수를 알려줄 방법에 대해서 생각해봐야 한다. + await self.emit( + 'response', + { + 'statusCode': 200, + 'message': 'SUCCESS', + 'eventName': 'disconnect', + 'data': users + } + ) + + except NoSuchElementException: + # 입장 전에 공부방을 종료하는 경우 + # 사용자 수를 알려줄 방법에 대해서 생각해봐야 한다. + await self.emit( + 'response', + { + 'statusCode': 200, + 'messgae': 'SUCCESS', + 'eventName': 'disconnect', + 'data': {} + }, + namespace = self.namespace + ) + + except Exception as error: + print(traceback.print_exc()) + await self.emit( + 'response', + { + 'statusCode': 500, + 'message': f'SERVER_ERROR_{error}', + 'eventName': 'disconnect', + 'data' : {} + }, + namespace = self.namespace + ) + + async def __create_status__(self, sid, type, study_result: dict): + saved_status = statuses.update_or_create( + db = self.db, + type = type, + cnt = study_result['count'], + time = study_result['sec'], + my_study_id = clients[sid]['my_study_id'], + report_id = clients[sid]['report_id'] + ) + + print(saved_status) \ No newline at end of file diff --git a/app/tests/api/study_rooms.py b/app/tests/api/study_rooms.py new file mode 100644 index 0000000..a735048 --- /dev/null +++ b/app/tests/api/study_rooms.py @@ -0,0 +1,54 @@ +from app.core import settings +from app.tests.conftest import client + + +api_address = settings.API_V1_STR + + +def test_get_study_room_success(): + room_id = 'be6172b4-c388-4b5d-832a-b30110e11bd5' + response = client.get( + f'{api_address}/study-rooms/{room_id}' + ) + assert response.status_code == 200 + + +def test_get_study_room_uuid(): + room_id = '7aa4556e-bdc2-4fe9-813a-1a4' + response = client.get( + f'{api_address}/study-rooms/{room_id}' + ) + assert response.status_code == 500 + + +def test_get_study_room_not_found(): + room_id = '7aa4556e-bdc2-4fe9' + response = client.get( + f'{api_address}/study_rooms/{room_id}' + ) + print(response.json()) + assert response.status_code == 404 + + +def test_get_study_rooms(): + response = client.get( + f'{api_address}/study-rooms' + ) + print(response.json()) + assert response.status_code == 200 + + +def test_create_study_room(): + data = { + "title": "스터디 룸 생성 테스트", + "description": "스터디룸 생성 설명", + "is_public": True, + "owner_id": 1 + } + response = client.post( + f'{api_address}/study-rooms', + json = { **data } + ) + print(response.json()) + assert response.status_code == 200 + assert response.json() == None \ No newline at end of file diff --git a/app/tests/api/v1/test_products.py b/app/tests/api/v1/test_products.py deleted file mode 100644 index c69411c..0000000 --- a/app/tests/api/v1/test_products.py +++ /dev/null @@ -1,35 +0,0 @@ -from typing import Dict - -from fastapi.testclient import TestClient - -from app.core import settings - - -def test_create_product(client: TestClient, random_product: Dict[str, str]) -> None: - response = client.post(f"{settings.API_V1_STR}/products", json=random_product) - product = response.json() - assert response.status_code == 200 - assert product.get("name") == random_product.get("name") - assert product.get("price") == random_product.get("price") - - -def test_read_products(client: TestClient) -> None: - response = client.get(f"{settings.API_V1_STR}/products") - products = response.json() - assert response.status_code == 200 - assert len(products) > 0 - - -def test_update_product(client: TestClient, random_product: Dict[str, str]) -> None: - random_product["price"] = 100 - response = client.put(f"{settings.API_V1_STR}/products", json=random_product) - product = response.json() - assert response.status_code == 200 - assert product.get("price") == random_product.get("price") - - -def test_delete_product(client: TestClient, random_product: Dict[str, str]) -> None: - response = client.delete(f"{settings.API_V1_STR}/products?id={random_product.get('id')}") - message = response.json() - assert response.status_code == 200 - assert "message" in message diff --git a/app/tests/conftest.py b/app/tests/conftest.py index 95de03a..db230e6 100644 --- a/app/tests/conftest.py +++ b/app/tests/conftest.py @@ -1,28 +1,38 @@ -from typing import Dict, Generator +from typing import Dict -import pytest - -from fastapi.testclient import TestClient +from fastapi.testclient import TestClient from app.database.session import SessionLocal -from app.main import app +from app.database.base import Base +from app.api.deps import get_db +from app.main import app + + +Base.metadata -@pytest.fixture(scope="session") -def db() -> Generator: - yield SessionLocal() +def overried_get_db(): + try: + db = SessionLocal() + yield db + finally: + db.close() -@pytest.fixture(scope="module") -def client() -> Generator: - with TestClient(app) as c: - yield c +def client(): + with TestClient(app) as client: + yield client -@pytest.fixture(scope="module") -def random_product() -> Dict[str, str]: +app.dependency_overrides[get_db] = overried_get_db + + +def test_user() -> Dict[str, str]: return { "id": 1, - "name": "Test Product", - "price": 80, + "provider": "Google", + "email": "test@test.com", + "nickname": "test" } + +client = TestClient(app) \ No newline at end of file diff --git a/app/tests/service/__init__.py b/app/tests/service/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/tests/service/user/__init__.py b/app/tests/service/user/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/tests/service/user/test_auth.py b/app/tests/service/user/test_auth.py new file mode 100644 index 0000000..2ba8920 --- /dev/null +++ b/app/tests/service/user/test_auth.py @@ -0,0 +1,45 @@ +from datetime import timedelta +from typing import Dict + +from fastapi.testclient import TestClient +from jose import jwt, JWTError + +from app.core import settings +from app.service.user.auth import create_access_token, auth_google_token, check_access_token_valid + + +def test_create_token() -> None: + # 토큰의 생성이 이루어지는 지 테스트 + # payload 테스트 + data = {"body": "test"} + access_token_expires = timedelta(minutes=15) + token = create_access_token(data, access_token_expires) + payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]) + + actual = payload.get("body") + + assert actual == "test" + + +def test_auth_google_token() -> None: + # 잘못된 토큰에 대하여 status 400 반환 + wrong_token = "test" + actual = auth_google_token(wrong_token).status_code + + assert actual == 400 + + +def test_check_access_token_valid() -> None: + test_token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZXN0IjoidGVzdCIsImV4cCI6MTYyMDMwNjQzMH0' \ + '.1c4BevvQ3IvPJSPBYQGMfwArVNpxOwO_qUhGwycYAuc ' + actual = check_access_token_valid(test_token) + + assert actual is False + + +# def test_check_refresh_token() -> None: +# # redis의 ref 토큰을 확인. +# user_id = 15 +# actual = check_refresh_token(15) +# +# assert actual is not None diff --git a/app/utils/logger.py b/app/utils/logger.py new file mode 100644 index 0000000..e69de29 diff --git a/config/redis/redis.conf b/config/redis/redis.conf new file mode 100644 index 0000000..0ed3b36 --- /dev/null +++ b/config/redis/redis.conf @@ -0,0 +1,1861 @@ +# Redis configuration file example. +# +# Note that in order to read the configuration file, Redis must be +# started with the file path as first argument: +# +# ./redis-server /path/to/redis.conf + +# Note on units: when memory size is needed, it is possible to specify +# it in the usual form of 1k 5GB 4M and so forth: +# +# 1k => 1000 bytes +# 1kb => 1024 bytes +# 1m => 1000000 bytes +# 1mb => 1024*1024 bytes +# 1g => 1000000000 bytes +# 1gb => 1024*1024*1024 bytes +# +# units are case insensitive so 1GB 1Gb 1gB are all the same. + +################################## INCLUDES ################################### + +# Include one or more other config files here. This is useful if you +# have a standard template that goes to all Redis servers but also need +# to customize a few per-server settings. Include files can include +# other files, so use this wisely. +# +# Notice option "include" won't be rewritten by command "CONFIG REWRITE" +# from admin or Redis Sentinel. Since Redis always uses the last processed +# line as value of a configuration directive, you'd better put includes +# at the beginning of this file to avoid overwriting config change at runtime. +# +# If instead you are interested in using includes to override configuration +# options, it is better to use include as the last line. +# +# include /path/to/local.conf +# include /path/to/other.conf + +################################## MODULES ##################################### + +# Load modules at startup. If the server is not able to load modules +# it will abort. It is possible to use multiple loadmodule directives. +# +# loadmodule /path/to/my_module.so +# loadmodule /path/to/other_module.so + +################################## NETWORK ##################################### + +# By default, if no "bind" configuration directive is specified, Redis listens +# for connections from all the network interfaces available on the server. +# It is possible to listen to just one or multiple selected interfaces using +# the "bind" configuration directive, followed by one or more IP addresses. +# +# Examples: +# +# bind 192.168.1.100 10.0.0.1 +# bind 127.0.0.1 ::1 +# +# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the +# internet, binding to all the interfaces is dangerous and will expose the +# instance to everybody on the internet. So by default we uncomment the +# following bind directive, that will force Redis to listen only into +# the IPv4 loopback interface address (this means Redis will be able to +# accept connections only from clients running into the same computer it +# is running). +# +# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES +# JUST COMMENT THE FOLLOWING LINE. +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +bind 0.0.0.0 + +# Protected mode is a layer of security protection, in order to avoid that +# Redis instances left open on the internet are accessed and exploited. +# +# When protected mode is on and if: +# +# 1) The server is not binding explicitly to a set of addresses using the +# "bind" directive. +# 2) No password is configured. +# +# The server only accepts connections from clients connecting from the +# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain +# sockets. +# +# By default protected mode is enabled. You should disable it only if +# you are sure you want clients from other hosts to connect to Redis +# even if no authentication is configured, nor a specific set of interfaces +# are explicitly listed using the "bind" directive. +protected-mode yes + +# Accept connections on the specified port, default is 6379 (IANA #815344). +# If port 0 is specified Redis will not listen on a TCP socket. +port 6379 + +# TCP listen() backlog. +# +# In high requests-per-second environments you need an high backlog in order +# to avoid slow clients connections issues. Note that the Linux kernel +# will silently truncate it to the value of /proc/sys/net/core/somaxconn so +# make sure to raise both the value of somaxconn and tcp_max_syn_backlog +# in order to get the desired effect. +tcp-backlog 511 + +# Unix socket. +# +# Specify the path for the Unix socket that will be used to listen for +# incoming connections. There is no default, so Redis will not listen +# on a unix socket when not specified. +# +# unixsocket /tmp/redis.sock +# unixsocketperm 700 + +# Close the connection after a client is idle for N seconds (0 to disable) +timeout 0 + +# TCP keepalive. +# +# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence +# of communication. This is useful for two reasons: +# +# 1) Detect dead peers. +# 2) Take the connection alive from the point of view of network +# equipment in the middle. +# +# On Linux, the specified value (in seconds) is the period used to send ACKs. +# Note that to close the connection the double of the time is needed. +# On other kernels the period depends on the kernel configuration. +# +# A reasonable value for this option is 300 seconds, which is the new +# Redis default starting with Redis 3.2.1. +tcp-keepalive 300 + +################################# TLS/SSL ##################################### + +# By default, TLS/SSL is disabled. To enable it, the "tls-port" configuration +# directive can be used to define TLS-listening ports. To enable TLS on the +# default port, use: +# +# port 0 +# tls-port 6379 + +# Configure a X.509 certificate and private key to use for authenticating the +# server to connected clients, masters or cluster peers. These files should be +# PEM formatted. +# +# tls-cert-file redis.crt +# tls-key-file redis.key + +# Configure a DH parameters file to enable Diffie-Hellman (DH) key exchange: +# +# tls-dh-params-file redis.dh + +# Configure a CA certificate(s) bundle or directory to authenticate TLS/SSL +# clients and peers. Redis requires an explicit configuration of at least one +# of these, and will not implicitly use the system wide configuration. +# +# tls-ca-cert-file ca.crt +# tls-ca-cert-dir /etc/ssl/certs + +# By default, clients (including replica servers) on a TLS port are required +# to authenticate using valid client side certificates. +# +# If "no" is specified, client certificates are not required and not accepted. +# If "optional" is specified, client certificates are accepted and must be +# valid if provided, but are not required. +# +# tls-auth-clients no +# tls-auth-clients optional + +# By default, a Redis replica does not attempt to establish a TLS connection +# with its master. +# +# Use the following directive to enable TLS on replication links. +# +# tls-replication yes + +# By default, the Redis Cluster bus uses a plain TCP connection. To enable +# TLS for the bus protocol, use the following directive: +# +# tls-cluster yes + +# Explicitly specify TLS versions to support. Allowed values are case insensitive +# and include "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3" (OpenSSL >= 1.1.1) or +# any combination. To enable only TLSv1.2 and TLSv1.3, use: +# +# tls-protocols "TLSv1.2 TLSv1.3" + +# Configure allowed ciphers. See the ciphers(1ssl) manpage for more information +# about the syntax of this string. +# +# Note: this configuration applies only to <= TLSv1.2. +# +# tls-ciphers DEFAULT:!MEDIUM + +# Configure allowed TLSv1.3 ciphersuites. See the ciphers(1ssl) manpage for more +# information about the syntax of this string, and specifically for TLSv1.3 +# ciphersuites. +# +# tls-ciphersuites TLS_CHACHA20_POLY1305_SHA256 + +# When choosing a cipher, use the server's preference instead of the client +# preference. By default, the server follows the client's preference. +# +# tls-prefer-server-ciphers yes + +# By default, TLS session caching is enabled to allow faster and less expensive +# reconnections by clients that support it. Use the following directive to disable +# caching. +# +# tls-session-caching no + +# Change the default number of TLS sessions cached. A zero value sets the cache +# to unlimited size. The default size is 20480. +# +# tls-session-cache-size 5000 + +# Change the default timeout of cached TLS sessions. The default timeout is 300 +# seconds. +# +# tls-session-cache-timeout 60 + +################################# GENERAL ##################################### + +# By default Redis does not run as a daemon. Use 'yes' if you need it. +# Note that Redis will write a pid file in /var/run/redis.pid when daemonized. +daemonize no + +# If you run Redis from upstart or systemd, Redis can interact with your +# supervision tree. Options: +# supervised no - no supervision interaction +# supervised upstart - signal upstart by putting Redis into SIGSTOP mode +# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET +# supervised auto - detect upstart or systemd method based on +# UPSTART_JOB or NOTIFY_SOCKET environment variables +# Note: these supervision methods only signal "process is ready." +# They do not enable continuous liveness pings back to your supervisor. +supervised no + +# If a pid file is specified, Redis writes it where specified at startup +# and removes it at exit. +# +# When the server runs non daemonized, no pid file is created if none is +# specified in the configuration. When the server is daemonized, the pid file +# is used even if not specified, defaulting to "/var/run/redis.pid". +# +# Creating a pid file is best effort: if Redis is not able to create it +# nothing bad happens, the server will start and run normally. +pidfile /var/run/redis_6379.pid + +# Specify the server verbosity level. +# This can be one of: +# debug (a lot of information, useful for development/testing) +# verbose (many rarely useful info, but not a mess like the debug level) +# notice (moderately verbose, what you want in production probably) +# warning (only very important / critical messages are logged) +loglevel notice + +# Specify the log file name. Also the empty string can be used to force +# Redis to log on the standard output. Note that if you use standard +# output for logging but daemonize, logs will be sent to /dev/null +logfile "" + +# To enable logging to the system logger, just set 'syslog-enabled' to yes, +# and optionally update the other syslog parameters to suit your needs. +# syslog-enabled no + +# Specify the syslog identity. +# syslog-ident redis + +# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7. +# syslog-facility local0 + +# Set the number of databases. The default database is DB 0, you can select +# a different one on a per-connection basis using SELECT where +# dbid is a number between 0 and 'databases'-1 +databases 16 + +# By default Redis shows an ASCII art logo only when started to log to the +# standard output and if the standard output is a TTY. Basically this means +# that normally a logo is displayed only in interactive sessions. +# +# However it is possible to force the pre-4.0 behavior and always show a +# ASCII art logo in startup logs by setting the following option to yes. +always-show-logo yes + +################################ SNAPSHOTTING ################################ +# +# Save the DB on disk: +# +# save +# +# Will save the DB if both the given number of seconds and the given +# number of write operations against the DB occurred. +# +# In the example below the behaviour will be to save: +# after 900 sec (15 min) if at least 1 key changed +# after 300 sec (5 min) if at least 10 keys changed +# after 60 sec if at least 10000 keys changed +# +# Note: you can disable saving completely by commenting out all "save" lines. +# +# It is also possible to remove all the previously configured save +# points by adding a save directive with a single empty string argument +# like in the following example: +# +# save "" + +save 900 1 +save 300 10 +save 60 10000 + +# By default Redis will stop accepting writes if RDB snapshots are enabled +# (at least one save point) and the latest background save failed. +# This will make the user aware (in a hard way) that data is not persisting +# on disk properly, otherwise chances are that no one will notice and some +# disaster will happen. +# +# If the background saving process will start working again Redis will +# automatically allow writes again. +# +# However if you have setup your proper monitoring of the Redis server +# and persistence, you may want to disable this feature so that Redis will +# continue to work as usual even if there are problems with disk, +# permissions, and so forth. +stop-writes-on-bgsave-error yes + +# Compress string objects using LZF when dump .rdb databases? +# For default that's set to 'yes' as it's almost always a win. +# If you want to save some CPU in the saving child set it to 'no' but +# the dataset will likely be bigger if you have compressible values or keys. +rdbcompression yes + +# Since version 5 of RDB a CRC64 checksum is placed at the end of the file. +# This makes the format more resistant to corruption but there is a performance +# hit to pay (around 10%) when saving and loading RDB files, so you can disable it +# for maximum performances. +# +# RDB files created with checksum disabled have a checksum of zero that will +# tell the loading code to skip the check. +rdbchecksum yes + +# The filename where to dump the DB +dbfilename dump.rdb + +# Remove RDB files used by replication in instances without persistence +# enabled. By default this option is disabled, however there are environments +# where for regulations or other security concerns, RDB files persisted on +# disk by masters in order to feed replicas, or stored on disk by replicas +# in order to load them for the initial synchronization, should be deleted +# ASAP. Note that this option ONLY WORKS in instances that have both AOF +# and RDB persistence disabled, otherwise is completely ignored. +# +# An alternative (and sometimes better) way to obtain the same effect is +# to use diskless replication on both master and replicas instances. However +# in the case of replicas, diskless is not always an option. +rdb-del-sync-files no + +# The working directory. +# +# The DB will be written inside this directory, with the filename specified +# above using the 'dbfilename' configuration directive. +# +# The Append Only File will also be created inside this directory. +# +# Note that you must specify a directory here, not a file name. +dir ./ + +################################# REPLICATION ################################# + +# Master-Replica replication. Use replicaof to make a Redis instance a copy of +# another Redis server. A few things to understand ASAP about Redis replication. +# +# +------------------+ +---------------+ +# | Master | ---> | Replica | +# | (receive writes) | | (exact copy) | +# +------------------+ +---------------+ +# +# 1) Redis replication is asynchronous, but you can configure a master to +# stop accepting writes if it appears to be not connected with at least +# a given number of replicas. +# 2) Redis replicas are able to perform a partial resynchronization with the +# master if the replication link is lost for a relatively small amount of +# time. You may want to configure the replication backlog size (see the next +# sections of this file) with a sensible value depending on your needs. +# 3) Replication is automatic and does not need user intervention. After a +# network partition replicas automatically try to reconnect to masters +# and resynchronize with them. +# +# replicaof + +# If the master is password protected (using the "requirepass" configuration +# directive below) it is possible to tell the replica to authenticate before +# starting the replication synchronization process, otherwise the master will +# refuse the replica request. +# +# masterauth +# +# However this is not enough if you are using Redis ACLs (for Redis version +# 6 or greater), and the default user is not capable of running the PSYNC +# command and/or other commands needed for replication. In this case it's +# better to configure a special user to use with replication, and specify the +# masteruser configuration as such: +# +# masteruser +# +# When masteruser is specified, the replica will authenticate against its +# master using the new AUTH form: AUTH . + +# When a replica loses its connection with the master, or when the replication +# is still in progress, the replica can act in two different ways: +# +# 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will +# still reply to client requests, possibly with out of date data, or the +# data set may just be empty if this is the first synchronization. +# +# 2) if replica-serve-stale-data is set to 'no' the replica will reply with +# an error "SYNC with master in progress" to all the kind of commands +# but to INFO, replicaOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, +# SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, +# COMMAND, POST, HOST: and LATENCY. +# +replica-serve-stale-data yes + +# You can configure a replica instance to accept writes or not. Writing against +# a replica instance may be useful to store some ephemeral data (because data +# written on a replica will be easily deleted after resync with the master) but +# may also cause problems if clients are writing to it because of a +# misconfiguration. +# +# Since Redis 2.6 by default replicas are read-only. +# +# Note: read only replicas are not designed to be exposed to untrusted clients +# on the internet. It's just a protection layer against misuse of the instance. +# Still a read only replica exports by default all the administrative commands +# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve +# security of read only replicas using 'rename-command' to shadow all the +# administrative / dangerous commands. +replica-read-only yes + +# Replication SYNC strategy: disk or socket. +# +# New replicas and reconnecting replicas that are not able to continue the +# replication process just receiving differences, need to do what is called a +# "full synchronization". An RDB file is transmitted from the master to the +# replicas. +# +# The transmission can happen in two different ways: +# +# 1) Disk-backed: The Redis master creates a new process that writes the RDB +# file on disk. Later the file is transferred by the parent +# process to the replicas incrementally. +# 2) Diskless: The Redis master creates a new process that directly writes the +# RDB file to replica sockets, without touching the disk at all. +# +# With disk-backed replication, while the RDB file is generated, more replicas +# can be queued and served with the RDB file as soon as the current child +# producing the RDB file finishes its work. With diskless replication instead +# once the transfer starts, new replicas arriving will be queued and a new +# transfer will start when the current one terminates. +# +# When diskless replication is used, the master waits a configurable amount of +# time (in seconds) before starting the transfer in the hope that multiple +# replicas will arrive and the transfer can be parallelized. +# +# With slow disks and fast (large bandwidth) networks, diskless replication +# works better. +repl-diskless-sync no + +# When diskless replication is enabled, it is possible to configure the delay +# the server waits in order to spawn the child that transfers the RDB via socket +# to the replicas. +# +# This is important since once the transfer starts, it is not possible to serve +# new replicas arriving, that will be queued for the next RDB transfer, so the +# server waits a delay in order to let more replicas arrive. +# +# The delay is specified in seconds, and by default is 5 seconds. To disable +# it entirely just set it to 0 seconds and the transfer will start ASAP. +repl-diskless-sync-delay 5 + +# ----------------------------------------------------------------------------- +# WARNING: RDB diskless load is experimental. Since in this setup the replica +# does not immediately store an RDB on disk, it may cause data loss during +# failovers. RDB diskless load + Redis modules not handling I/O reads may also +# cause Redis to abort in case of I/O errors during the initial synchronization +# stage with the master. Use only if your do what you are doing. +# ----------------------------------------------------------------------------- +# +# Replica can load the RDB it reads from the replication link directly from the +# socket, or store the RDB to a file and read that file after it was completely +# recived from the master. +# +# In many cases the disk is slower than the network, and storing and loading +# the RDB file may increase replication time (and even increase the master's +# Copy on Write memory and salve buffers). +# However, parsing the RDB file directly from the socket may mean that we have +# to flush the contents of the current database before the full rdb was +# received. For this reason we have the following options: +# +# "disabled" - Don't use diskless load (store the rdb file to the disk first) +# "on-empty-self" - Use diskless load only when it is completely safe. +# "swapdb" - Keep a copy of the current self contents in RAM while parsing +# the data directly from the socket. note that this requires +# sufficient memory, if you don't have it, you risk an OOM kill. +repl-diskless-load disabled + +# Replicas send PINGs to server in a predefined interval. It's possible to +# change this interval with the repl_ping_replica_period option. The default +# value is 10 seconds. +# +# repl-ping-replica-period 10 + +# The following option sets the replication timeout for: +# +# 1) Bulk transfer I/O during SYNC, from the point of view of replica. +# 2) Master timeout from the point of view of replicas (data, pings). +# 3) Replica timeout from the point of view of masters (REPLCONF ACK pings). +# +# It is important to make sure that this value is greater than the value +# specified for repl-ping-replica-period otherwise a timeout will be detected +# every time there is low traffic between the master and the replica. +# +# repl-timeout 60 + +# Disable TCP_NODELAY on the replica socket after SYNC? +# +# If you select "yes" Redis will use a smaller number of TCP packets and +# less bandwidth to send data to replicas. But this can add a delay for +# the data to appear on the replica side, up to 40 milliseconds with +# Linux kernels using a default configuration. +# +# If you select "no" the delay for data to appear on the replica side will +# be reduced but more bandwidth will be used for replication. +# +# By default we optimize for low latency, but in very high traffic conditions +# or when the master and replicas are many hops away, turning this to "yes" may +# be a good idea. +repl-disable-tcp-nodelay no + +# Set the replication backlog size. The backlog is a buffer that accumulates +# replica data when replicas are disconnected for some time, so that when a +# replica wants to reconnect again, often a full resync is not needed, but a +# partial resync is enough, just passing the portion of data the replica +# missed while disconnected. +# +# The bigger the replication backlog, the longer the time the replica can be +# disconnected and later be able to perform a partial resynchronization. +# +# The backlog is only allocated once there is at least a replica connected. +# +# repl-backlog-size 1mb + +# After a master has no longer connected replicas for some time, the backlog +# will be freed. The following option configures the amount of seconds that +# need to elapse, starting from the time the last replica disconnected, for +# the backlog buffer to be freed. +# +# Note that replicas never free the backlog for timeout, since they may be +# promoted to masters later, and should be able to correctly "partially +# resynchronize" with the replicas: hence they should always accumulate backlog. +# +# A value of 0 means to never release the backlog. +# +# repl-backlog-ttl 3600 + +# The replica priority is an integer number published by Redis in the INFO +# output. It is used by Redis Sentinel in order to select a replica to promote +# into a master if the master is no longer working correctly. +# +# A replica with a low priority number is considered better for promotion, so +# for instance if there are three replicas with priority 10, 100, 25 Sentinel +# will pick the one with priority 10, that is the lowest. +# +# However a special priority of 0 marks the replica as not able to perform the +# role of master, so a replica with priority of 0 will never be selected by +# Redis Sentinel for promotion. +# +# By default the priority is 100. +replica-priority 100 + +# It is possible for a master to stop accepting writes if there are less than +# N replicas connected, having a lag less or equal than M seconds. +# +# The N replicas need to be in "online" state. +# +# The lag in seconds, that must be <= the specified value, is calculated from +# the last ping received from the replica, that is usually sent every second. +# +# This option does not GUARANTEE that N replicas will accept the write, but +# will limit the window of exposure for lost writes in case not enough replicas +# are available, to the specified number of seconds. +# +# For example to require at least 3 replicas with a lag <= 10 seconds use: +# +# min-replicas-to-write 3 +# min-replicas-max-lag 10 +# +# Setting one or the other to 0 disables the feature. +# +# By default min-replicas-to-write is set to 0 (feature disabled) and +# min-replicas-max-lag is set to 10. + +# A Redis master is able to list the address and port of the attached +# replicas in different ways. For example the "INFO replication" section +# offers this information, which is used, among other tools, by +# Redis Sentinel in order to discover replica instances. +# Another place where this info is available is in the output of the +# "ROLE" command of a master. +# +# The listed IP and address normally reported by a replica is obtained +# in the following way: +# +# IP: The address is auto detected by checking the peer address +# of the socket used by the replica to connect with the master. +# +# Port: The port is communicated by the replica during the replication +# handshake, and is normally the port that the replica is using to +# listen for connections. +# +# However when port forwarding or Network Address Translation (NAT) is +# used, the replica may be actually reachable via different IP and port +# pairs. The following two options can be used by a replica in order to +# report to its master a specific set of IP and port, so that both INFO +# and ROLE will report those values. +# +# There is no need to use both the options if you need to override just +# the port or the IP address. +# +# replica-announce-ip 5.5.5.5 +# replica-announce-port 1234 + +############################### KEYS TRACKING ################################# + +# Redis implements server assisted support for client side caching of values. +# This is implemented using an invalidation table that remembers, using +# 16 millions of slots, what clients may have certain subsets of keys. In turn +# this is used in order to send invalidation messages to clients. Please +# to understand more about the feature check this page: +# +# https://redis.io/topics/client-side-caching +# +# When tracking is enabled for a client, all the read only queries are assumed +# to be cached: this will force Redis to store information in the invalidation +# table. When keys are modified, such information is flushed away, and +# invalidation messages are sent to the clients. However if the workload is +# heavily dominated by reads, Redis could use more and more memory in order +# to track the keys fetched by many clients. +# +# For this reason it is possible to configure a maximum fill value for the +# invalidation table. By default it is set to 1M of keys, and once this limit +# is reached, Redis will start to evict keys in the invalidation table +# even if they were not modified, just to reclaim memory: this will in turn +# force the clients to invalidate the cached values. Basically the table +# maximum size is a trade off between the memory you want to spend server +# side to track information about who cached what, and the ability of clients +# to retain cached objects in memory. +# +# If you set the value to 0, it means there are no limits, and Redis will +# retain as many keys as needed in the invalidation table. +# In the "stats" INFO section, you can find information about the number of +# keys in the invalidation table at every given moment. +# +# Note: when key tracking is used in broadcasting mode, no memory is used +# in the server side so this setting is useless. +# +# tracking-table-max-keys 1000000 + +################################## SECURITY ################################### + +# Warning: since Redis is pretty fast an outside user can try up to +# 1 million passwords per second against a modern box. This means that you +# should use very strong passwords, otherwise they will be very easy to break. +# Note that because the password is really a shared secret between the client +# and the server, and should not be memorized by any human, the password +# can be easily a long string from /dev/urandom or whatever, so by using a +# long and unguessable password no brute force attack will be possible. + +# Redis ACL users are defined in the following format: +# +# user ... acl rules ... +# +# For example: +# +# user worker +@list +@connection ~jobs:* on >ffa9203c493aa99 +# +# The special username "default" is used for new connections. If this user +# has the "nopass" rule, then new connections will be immediately authenticated +# as the "default" user without the need of any password provided via the +# AUTH command. Otherwise if the "default" user is not flagged with "nopass" +# the connections will start in not authenticated state, and will require +# AUTH (or the HELLO command AUTH option) in order to be authenticated and +# start to work. +# +# The ACL rules that describe what an user can do are the following: +# +# on Enable the user: it is possible to authenticate as this user. +# off Disable the user: it's no longer possible to authenticate +# with this user, however the already authenticated connections +# will still work. +# + Allow the execution of that command +# - Disallow the execution of that command +# +@ Allow the execution of all the commands in such category +# with valid categories are like @admin, @set, @sortedset, ... +# and so forth, see the full list in the server.c file where +# the Redis command table is described and defined. +# The special category @all means all the commands, but currently +# present in the server, and that will be loaded in the future +# via modules. +# +|subcommand Allow a specific subcommand of an otherwise +# disabled command. Note that this form is not +# allowed as negative like -DEBUG|SEGFAULT, but +# only additive starting with "+". +# allcommands Alias for +@all. Note that it implies the ability to execute +# all the future commands loaded via the modules system. +# nocommands Alias for -@all. +# ~ Add a pattern of keys that can be mentioned as part of +# commands. For instance ~* allows all the keys. The pattern +# is a glob-style pattern like the one of KEYS. +# It is possible to specify multiple patterns. +# allkeys Alias for ~* +# resetkeys Flush the list of allowed keys patterns. +# > Add this passowrd to the list of valid password for the user. +# For example >mypass will add "mypass" to the list. +# This directive clears the "nopass" flag (see later). +# < Remove this password from the list of valid passwords. +# nopass All the set passwords of the user are removed, and the user +# is flagged as requiring no password: it means that every +# password will work against this user. If this directive is +# used for the default user, every new connection will be +# immediately authenticated with the default user without +# any explicit AUTH command required. Note that the "resetpass" +# directive will clear this condition. +# resetpass Flush the list of allowed passwords. Moreover removes the +# "nopass" status. After "resetpass" the user has no associated +# passwords and there is no way to authenticate without adding +# some password (or setting it as "nopass" later). +# reset Performs the following actions: resetpass, resetkeys, off, +# -@all. The user returns to the same state it has immediately +# after its creation. +# +# ACL rules can be specified in any order: for instance you can start with +# passwords, then flags, or key patterns. However note that the additive +# and subtractive rules will CHANGE MEANING depending on the ordering. +# For instance see the following example: +# +# user alice on +@all -DEBUG ~* >somepassword +# +# This will allow "alice" to use all the commands with the exception of the +# DEBUG command, since +@all added all the commands to the set of the commands +# alice can use, and later DEBUG was removed. However if we invert the order +# of two ACL rules the result will be different: +# +# user alice on -DEBUG +@all ~* >somepassword +# +# Now DEBUG was removed when alice had yet no commands in the set of allowed +# commands, later all the commands are added, so the user will be able to +# execute everything. +# +# Basically ACL rules are processed left-to-right. +# +# For more information about ACL configuration please refer to +# the Redis web site at https://redis.io/topics/acl + +# ACL LOG +# +# The ACL Log tracks failed commands and authentication events associated +# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked +# by ACLs. The ACL Log is stored in memory. You can reclaim memory with +# ACL LOG RESET. Define the maximum entry length of the ACL Log below. +acllog-max-len 128 + +# Using an external ACL file +# +# Instead of configuring users here in this file, it is possible to use +# a stand-alone file just listing users. The two methods cannot be mixed: +# if you configure users here and at the same time you activate the exteranl +# ACL file, the server will refuse to start. +# +# The format of the external ACL user file is exactly the same as the +# format that is used inside redis.conf to describe users. +# +# aclfile /etc/redis/users.acl + +# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatiblity +# layer on top of the new ACL system. The option effect will be just setting +# the password for the default user. Clients will still authenticate using +# AUTH as usually, or more explicitly with AUTH default +# if they follow the new protocol: both will work. +# +# requirepass foobared + +# Command renaming (DEPRECATED). +# +# ------------------------------------------------------------------------ +# WARNING: avoid using this option if possible. Instead use ACLs to remove +# commands from the default user, and put them only in some admin user you +# create for administrative purposes. +# ------------------------------------------------------------------------ +# +# It is possible to change the name of dangerous commands in a shared +# environment. For instance the CONFIG command may be renamed into something +# hard to guess so that it will still be available for internal-use tools +# but not available for general clients. +# +# Example: +# +# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 +# +# It is also possible to completely kill a command by renaming it into +# an empty string: +# +# rename-command CONFIG "" +# +# Please note that changing the name of commands that are logged into the +# AOF file or transmitted to replicas may cause problems. + +################################### CLIENTS #################################### + +# Set the max number of connected clients at the same time. By default +# this limit is set to 10000 clients, however if the Redis server is not +# able to configure the process file limit to allow for the specified limit +# the max number of allowed clients is set to the current file limit +# minus 32 (as Redis reserves a few file descriptors for internal uses). +# +# Once the limit is reached Redis will close all the new connections sending +# an error 'max number of clients reached'. +# +# IMPORTANT: When Redis Cluster is used, the max number of connections is also +# shared with the cluster bus: every node in the cluster will use two +# connections, one incoming and another outgoing. It is important to size the +# limit accordingly in case of very large clusters. +# +# maxclients 10000 + +############################## MEMORY MANAGEMENT ################################ + +# Set a memory usage limit to the specified amount of bytes. +# When the memory limit is reached Redis will try to remove keys +# according to the eviction policy selected (see maxmemory-policy). +# +# If Redis can't remove keys according to the policy, or if the policy is +# set to 'noeviction', Redis will start to reply with errors to commands +# that would use more memory, like SET, LPUSH, and so on, and will continue +# to reply to read-only commands like GET. +# +# This option is usually useful when using Redis as an LRU or LFU cache, or to +# set a hard memory limit for an instance (using the 'noeviction' policy). +# +# WARNING: If you have replicas attached to an instance with maxmemory on, +# the size of the output buffers needed to feed the replicas are subtracted +# from the used memory count, so that network problems / resyncs will +# not trigger a loop where keys are evicted, and in turn the output +# buffer of replicas is full with DELs of keys evicted triggering the deletion +# of more keys, and so forth until the database is completely emptied. +# +# In short... if you have replicas attached it is suggested that you set a lower +# limit for maxmemory so that there is some free RAM on the system for replica +# output buffers (but this is not needed if the policy is 'noeviction'). +# +# maxmemory + +# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory +# is reached. You can select one from the following behaviors: +# +# volatile-lru -> Evict using approximated LRU, only keys with an expire set. +# allkeys-lru -> Evict any key using approximated LRU. +# volatile-lfu -> Evict using approximated LFU, only keys with an expire set. +# allkeys-lfu -> Evict any key using approximated LFU. +# volatile-random -> Remove a random key having an expire set. +# allkeys-random -> Remove a random key, any key. +# volatile-ttl -> Remove the key with the nearest expire time (minor TTL) +# noeviction -> Don't evict anything, just return an error on write operations. +# +# LRU means Least Recently Used +# LFU means Least Frequently Used +# +# Both LRU, LFU and volatile-ttl are implemented using approximated +# randomized algorithms. +# +# Note: with any of the above policies, Redis will return an error on write +# operations, when there are no suitable keys for eviction. +# +# At the date of writing these commands are: set setnx setex append +# incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd +# sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby +# zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby +# getset mset msetnx exec sort +# +# The default is: +# +# maxmemory-policy noeviction + +# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated +# algorithms (in order to save memory), so you can tune it for speed or +# accuracy. For default Redis will check five keys and pick the one that was +# used less recently, you can change the sample size using the following +# configuration directive. +# +# The default of 5 produces good enough results. 10 Approximates very closely +# true LRU but costs more CPU. 3 is faster but not very accurate. +# +# maxmemory-samples 5 + +# Starting from Redis 5, by default a replica will ignore its maxmemory setting +# (unless it is promoted to master after a failover or manually). It means +# that the eviction of keys will be just handled by the master, sending the +# DEL commands to the replica as keys evict in the master side. +# +# This behavior ensures that masters and replicas stay consistent, and is usually +# what you want, however if your replica is writable, or you want the replica +# to have a different memory setting, and you are sure all the writes performed +# to the replica are idempotent, then you may change this default (but be sure +# to understand what you are doing). +# +# Note that since the replica by default does not evict, it may end using more +# memory than the one set via maxmemory (there are certain buffers that may +# be larger on the replica, or data structures may sometimes take more memory +# and so forth). So make sure you monitor your replicas and make sure they +# have enough memory to never hit a real out-of-memory condition before the +# master hits the configured maxmemory setting. +# +# replica-ignore-maxmemory yes + +# Redis reclaims expired keys in two ways: upon access when those keys are +# found to be expired, and also in background, in what is called the +# "active expire key". The key space is slowly and interactively scanned +# looking for expired keys to reclaim, so that it is possible to free memory +# of keys that are expired and will never be accessed again in a short time. +# +# The default effort of the expire cycle will try to avoid having more than +# ten percent of expired keys still in memory, and will try to avoid consuming +# more than 25% of total memory and to add latency to the system. However +# it is possible to increase the expire "effort" that is normally set to +# "1", to a greater value, up to the value "10". At its maximum value the +# system will use more CPU, longer cycles (and technically may introduce +# more latency), and will tollerate less already expired keys still present +# in the system. It's a tradeoff betweeen memory, CPU and latecy. +# +# active-expire-effort 1 + +############################# LAZY FREEING #################################### + +# Redis has two primitives to delete keys. One is called DEL and is a blocking +# deletion of the object. It means that the server stops processing new commands +# in order to reclaim all the memory associated with an object in a synchronous +# way. If the key deleted is associated with a small object, the time needed +# in order to execute the DEL command is very small and comparable to most other +# O(1) or O(log_N) commands in Redis. However if the key is associated with an +# aggregated value containing millions of elements, the server can block for +# a long time (even seconds) in order to complete the operation. +# +# For the above reasons Redis also offers non blocking deletion primitives +# such as UNLINK (non blocking DEL) and the ASYNC option of FLUSHALL and +# FLUSHDB commands, in order to reclaim memory in background. Those commands +# are executed in constant time. Another thread will incrementally free the +# object in the background as fast as possible. +# +# DEL, UNLINK and ASYNC option of FLUSHALL and FLUSHDB are user-controlled. +# It's up to the design of the application to understand when it is a good +# idea to use one or the other. However the Redis server sometimes has to +# delete keys or flush the whole database as a side effect of other operations. +# Specifically Redis deletes objects independently of a user call in the +# following scenarios: +# +# 1) On eviction, because of the maxmemory and maxmemory policy configurations, +# in order to make room for new data, without going over the specified +# memory limit. +# 2) Because of expire: when a key with an associated time to live (see the +# EXPIRE command) must be deleted from memory. +# 3) Because of a side effect of a command that stores data on a key that may +# already exist. For example the RENAME command may delete the old key +# content when it is replaced with another one. Similarly SUNIONSTORE +# or SORT with STORE option may delete existing keys. The SET command +# itself removes any old content of the specified key in order to replace +# it with the specified string. +# 4) During replication, when a replica performs a full resynchronization with +# its master, the content of the whole database is removed in order to +# load the RDB file just transferred. +# +# In all the above cases the default is to delete objects in a blocking way, +# like if DEL was called. However you can configure each case specifically +# in order to instead release memory in a non-blocking way like if UNLINK +# was called, using the following configuration directives. + +lazyfree-lazy-eviction no +lazyfree-lazy-expire no +lazyfree-lazy-server-del no +replica-lazy-flush no + +# It is also possible, for the case when to replace the user code DEL calls +# with UNLINK calls is not easy, to modify the default behavior of the DEL +# command to act exactly like UNLINK, using the following configuration +# directive: + +lazyfree-lazy-user-del no + +################################ THREADED I/O ################################# + +# Redis is mostly single threaded, however there are certain threaded +# operations such as UNLINK, slow I/O accesses and other things that are +# performed on side threads. +# +# Now it is also possible to handle Redis clients socket reads and writes +# in different I/O threads. Since especially writing is so slow, normally +# Redis users use pipelining in order to speedup the Redis performances per +# core, and spawn multiple instances in order to scale more. Using I/O +# threads it is possible to easily speedup two times Redis without resorting +# to pipelining nor sharding of the instance. +# +# By default threading is disabled, we suggest enabling it only in machines +# that have at least 4 or more cores, leaving at least one spare core. +# Using more than 8 threads is unlikely to help much. We also recommend using +# threaded I/O only if you actually have performance problems, with Redis +# instances being able to use a quite big percentage of CPU time, otherwise +# there is no point in using this feature. +# +# So for instance if you have a four cores boxes, try to use 2 or 3 I/O +# threads, if you have a 8 cores, try to use 6 threads. In order to +# enable I/O threads use the following configuration directive: +# +# io-threads 4 +# +# Setting io-threads to 1 will just use the main thread as usually. +# When I/O threads are enabled, we only use threads for writes, that is +# to thread the write(2) syscall and transfer the client buffers to the +# socket. However it is also possible to enable threading of reads and +# protocol parsing using the following configuration directive, by setting +# it to yes: +# +# io-threads-do-reads no +# +# Usually threading reads doesn't help much. +# +# NOTE 1: This configuration directive cannot be changed at runtime via +# CONFIG SET. Aso this feature currently does not work when SSL is +# enabled. +# +# NOTE 2: If you want to test the Redis speedup using redis-benchmark, make +# sure you also run the benchmark itself in threaded mode, using the +# --threads option to match the number of Redis theads, otherwise you'll not +# be able to notice the improvements. + +############################ KERNEL OOM CONTROL ############################## + +# On Linux, it is possible to hint the kernel OOM killer on what processes +# should be killed first when out of memory. +# +# Enabling this feature makes Redis actively control the oom_score_adj value +# for all its processes, depending on their role. The default scores will +# attempt to have background child processes killed before all others, and +# replicas killed before masters. + +oom-score-adj no + +# When oom-score-adj is used, this directive controls the specific values used +# for master, replica and background child processes. Values range -1000 to +# 1000 (higher means more likely to be killed). +# +# Unprivileged processes (not root, and without CAP_SYS_RESOURCE capabilities) +# can freely increase their value, but not decrease it below its initial +# settings. +# +# Values are used relative to the initial value of oom_score_adj when the server +# starts. Because typically the initial value is 0, they will often match the +# absolute values. + +oom-score-adj-values 0 200 800 + +############################## APPEND ONLY MODE ############################### + +# By default Redis asynchronously dumps the dataset on disk. This mode is +# good enough in many applications, but an issue with the Redis process or +# a power outage may result into a few minutes of writes lost (depending on +# the configured save points). +# +# The Append Only File is an alternative persistence mode that provides +# much better durability. For instance using the default data fsync policy +# (see later in the config file) Redis can lose just one second of writes in a +# dramatic event like a server power outage, or a single write if something +# wrong with the Redis process itself happens, but the operating system is +# still running correctly. +# +# AOF and RDB persistence can be enabled at the same time without problems. +# If the AOF is enabled on startup Redis will load the AOF, that is the file +# with the better durability guarantees. +# +# Please check http://redis.io/topics/persistence for more information. + +appendonly no + +# The name of the append only file (default: "appendonly.aof") + +appendfilename "appendonly.aof" + +# The fsync() call tells the Operating System to actually write data on disk +# instead of waiting for more data in the output buffer. Some OS will really flush +# data on disk, some other OS will just try to do it ASAP. +# +# Redis supports three different modes: +# +# no: don't fsync, just let the OS flush the data when it wants. Faster. +# always: fsync after every write to the append only log. Slow, Safest. +# everysec: fsync only one time every second. Compromise. +# +# The default is "everysec", as that's usually the right compromise between +# speed and data safety. It's up to you to understand if you can relax this to +# "no" that will let the operating system flush the output buffer when +# it wants, for better performances (but if you can live with the idea of +# some data loss consider the default persistence mode that's snapshotting), +# or on the contrary, use "always" that's very slow but a bit safer than +# everysec. +# +# More details please check the following article: +# http://antirez.com/post/redis-persistence-demystified.html +# +# If unsure, use "everysec". + +# appendfsync always +appendfsync everysec +# appendfsync no + +# When the AOF fsync policy is set to always or everysec, and a background +# saving process (a background save or AOF log background rewriting) is +# performing a lot of I/O against the disk, in some Linux configurations +# Redis may block too long on the fsync() call. Note that there is no fix for +# this currently, as even performing fsync in a different thread will block +# our synchronous write(2) call. +# +# In order to mitigate this problem it's possible to use the following option +# that will prevent fsync() from being called in the main process while a +# BGSAVE or BGREWRITEAOF is in progress. +# +# This means that while another child is saving, the durability of Redis is +# the same as "appendfsync none". In practical terms, this means that it is +# possible to lose up to 30 seconds of log in the worst scenario (with the +# default Linux settings). +# +# If you have latency problems turn this to "yes". Otherwise leave it as +# "no" that is the safest pick from the point of view of durability. + +no-appendfsync-on-rewrite no + +# Automatic rewrite of the append only file. +# Redis is able to automatically rewrite the log file implicitly calling +# BGREWRITEAOF when the AOF log size grows by the specified percentage. +# +# This is how it works: Redis remembers the size of the AOF file after the +# latest rewrite (if no rewrite has happened since the restart, the size of +# the AOF at startup is used). +# +# This base size is compared to the current size. If the current size is +# bigger than the specified percentage, the rewrite is triggered. Also +# you need to specify a minimal size for the AOF file to be rewritten, this +# is useful to avoid rewriting the AOF file even if the percentage increase +# is reached but it is still pretty small. +# +# Specify a percentage of zero in order to disable the automatic AOF +# rewrite feature. + +auto-aof-rewrite-percentage 100 +auto-aof-rewrite-min-size 64mb + +# An AOF file may be found to be truncated at the end during the Redis +# startup process, when the AOF data gets loaded back into memory. +# This may happen when the system where Redis is running +# crashes, especially when an ext4 filesystem is mounted without the +# data=ordered option (however this can't happen when Redis itself +# crashes or aborts but the operating system still works correctly). +# +# Redis can either exit with an error when this happens, or load as much +# data as possible (the default now) and start if the AOF file is found +# to be truncated at the end. The following option controls this behavior. +# +# If aof-load-truncated is set to yes, a truncated AOF file is loaded and +# the Redis server starts emitting a log to inform the user of the event. +# Otherwise if the option is set to no, the server aborts with an error +# and refuses to start. When the option is set to no, the user requires +# to fix the AOF file using the "redis-check-aof" utility before to restart +# the server. +# +# Note that if the AOF file will be found to be corrupted in the middle +# the server will still exit with an error. This option only applies when +# Redis will try to read more data from the AOF file but not enough bytes +# will be found. +aof-load-truncated yes + +# When rewriting the AOF file, Redis is able to use an RDB preamble in the +# AOF file for faster rewrites and recoveries. When this option is turned +# on the rewritten AOF file is composed of two different stanzas: +# +# [RDB file][AOF tail] +# +# When loading Redis recognizes that the AOF file starts with the "REDIS" +# string and loads the prefixed RDB file, and continues loading the AOF +# tail. +aof-use-rdb-preamble yes + +################################ LUA SCRIPTING ############################### + +# Max execution time of a Lua script in milliseconds. +# +# If the maximum execution time is reached Redis will log that a script is +# still in execution after the maximum allowed time and will start to +# reply to queries with an error. +# +# When a long running script exceeds the maximum execution time only the +# SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be +# used to stop a script that did not yet called write commands. The second +# is the only way to shut down the server in the case a write command was +# already issued by the script but the user doesn't want to wait for the natural +# termination of the script. +# +# Set it to 0 or a negative value for unlimited execution without warnings. +lua-time-limit 5000 + +################################ REDIS CLUSTER ############################### + +# Normal Redis instances can't be part of a Redis Cluster; only nodes that are +# started as cluster nodes can. In order to start a Redis instance as a +# cluster node enable the cluster support uncommenting the following: +# +# cluster-enabled yes + +# Every cluster node has a cluster configuration file. This file is not +# intended to be edited by hand. It is created and updated by Redis nodes. +# Every Redis Cluster node requires a different cluster configuration file. +# Make sure that instances running in the same system do not have +# overlapping cluster configuration file names. +# +# cluster-config-file nodes-6379.conf + +# Cluster node timeout is the amount of milliseconds a node must be unreachable +# for it to be considered in failure state. +# Most other internal time limits are multiple of the node timeout. +# +# cluster-node-timeout 15000 + +# A replica of a failing master will avoid to start a failover if its data +# looks too old. +# +# There is no simple way for a replica to actually have an exact measure of +# its "data age", so the following two checks are performed: +# +# 1) If there are multiple replicas able to failover, they exchange messages +# in order to try to give an advantage to the replica with the best +# replication offset (more data from the master processed). +# Replicas will try to get their rank by offset, and apply to the start +# of the failover a delay proportional to their rank. +# +# 2) Every single replica computes the time of the last interaction with +# its master. This can be the last ping or command received (if the master +# is still in the "connected" state), or the time that elapsed since the +# disconnection with the master (if the replication link is currently down). +# If the last interaction is too old, the replica will not try to failover +# at all. +# +# The point "2" can be tuned by user. Specifically a replica will not perform +# the failover if, since the last interaction with the master, the time +# elapsed is greater than: +# +# (node-timeout * replica-validity-factor) + repl-ping-replica-period +# +# So for example if node-timeout is 30 seconds, and the replica-validity-factor +# is 10, and assuming a default repl-ping-replica-period of 10 seconds, the +# replica will not try to failover if it was not able to talk with the master +# for longer than 310 seconds. +# +# A large replica-validity-factor may allow replicas with too old data to failover +# a master, while a too small value may prevent the cluster from being able to +# elect a replica at all. +# +# For maximum availability, it is possible to set the replica-validity-factor +# to a value of 0, which means, that replicas will always try to failover the +# master regardless of the last time they interacted with the master. +# (However they'll always try to apply a delay proportional to their +# offset rank). +# +# Zero is the only value able to guarantee that when all the partitions heal +# the cluster will always be able to continue. +# +# cluster-replica-validity-factor 10 + +# Cluster replicas are able to migrate to orphaned masters, that are masters +# that are left without working replicas. This improves the cluster ability +# to resist to failures as otherwise an orphaned master can't be failed over +# in case of failure if it has no working replicas. +# +# Replicas migrate to orphaned masters only if there are still at least a +# given number of other working replicas for their old master. This number +# is the "migration barrier". A migration barrier of 1 means that a replica +# will migrate only if there is at least 1 other working replica for its master +# and so forth. It usually reflects the number of replicas you want for every +# master in your cluster. +# +# Default is 1 (replicas migrate only if their masters remain with at least +# one replica). To disable migration just set it to a very large value. +# A value of 0 can be set but is useful only for debugging and dangerous +# in production. +# +# cluster-migration-barrier 1 + +# By default Redis Cluster nodes stop accepting queries if they detect there +# is at least an hash slot uncovered (no available node is serving it). +# This way if the cluster is partially down (for example a range of hash slots +# are no longer covered) all the cluster becomes, eventually, unavailable. +# It automatically returns available as soon as all the slots are covered again. +# +# However sometimes you want the subset of the cluster which is working, +# to continue to accept queries for the part of the key space that is still +# covered. In order to do so, just set the cluster-require-full-coverage +# option to no. +# +# cluster-require-full-coverage yes + +# This option, when set to yes, prevents replicas from trying to failover its +# master during master failures. However the master can still perform a +# manual failover, if forced to do so. +# +# This is useful in different scenarios, especially in the case of multiple +# data center operations, where we want one side to never be promoted if not +# in the case of a total DC failure. +# +# cluster-replica-no-failover no + +# This option, when set to yes, allows nodes to serve read traffic while the +# the cluster is in a down state, as long as it believes it owns the slots. +# +# This is useful for two cases. The first case is for when an application +# doesn't require consistency of data during node failures or network partitions. +# One example of this is a cache, where as long as the node has the data it +# should be able to serve it. +# +# The second use case is for configurations that don't meet the recommended +# three shards but want to enable cluster mode and scale later. A +# master outage in a 1 or 2 shard configuration causes a read/write outage to the +# entire cluster without this option set, with it set there is only a write outage. +# Without a quorum of masters, slot ownership will not change automatically. +# +# cluster-allow-reads-when-down no + +# In order to setup your cluster make sure to read the documentation +# available at http://redis.io web site. + +########################## CLUSTER DOCKER/NAT support ######################## + +# In certain deployments, Redis Cluster nodes address discovery fails, because +# addresses are NAT-ted or because ports are forwarded (the typical case is +# Docker and other containers). +# +# In order to make Redis Cluster working in such environments, a static +# configuration where each node knows its public address is needed. The +# following two options are used for this scope, and are: +# +# * cluster-announce-ip +# * cluster-announce-port +# * cluster-announce-bus-port +# +# Each instruct the node about its address, client port, and cluster message +# bus port. The information is then published in the header of the bus packets +# so that other nodes will be able to correctly map the address of the node +# publishing the information. +# +# If the above options are not used, the normal Redis Cluster auto-detection +# will be used instead. +# +# Note that when remapped, the bus port may not be at the fixed offset of +# clients port + 10000, so you can specify any port and bus-port depending +# on how they get remapped. If the bus-port is not set, a fixed offset of +# 10000 will be used as usually. +# +# Example: +# +# cluster-announce-ip 10.1.1.5 +# cluster-announce-port 6379 +# cluster-announce-bus-port 6380 + +################################## SLOW LOG ################################### + +# The Redis Slow Log is a system to log queries that exceeded a specified +# execution time. The execution time does not include the I/O operations +# like talking with the client, sending the reply and so forth, +# but just the time needed to actually execute the command (this is the only +# stage of command execution where the thread is blocked and can not serve +# other requests in the meantime). +# +# You can configure the slow log with two parameters: one tells Redis +# what is the execution time, in microseconds, to exceed in order for the +# command to get logged, and the other parameter is the length of the +# slow log. When a new command is logged the oldest one is removed from the +# queue of logged commands. + +# The following time is expressed in microseconds, so 1000000 is equivalent +# to one second. Note that a negative number disables the slow log, while +# a value of zero forces the logging of every command. +slowlog-log-slower-than 10000 + +# There is no limit to this length. Just be aware that it will consume memory. +# You can reclaim memory used by the slow log with SLOWLOG RESET. +slowlog-max-len 128 + +################################ LATENCY MONITOR ############################## + +# The Redis latency monitoring subsystem samples different operations +# at runtime in order to collect data related to possible sources of +# latency of a Redis instance. +# +# Via the LATENCY command this information is available to the user that can +# print graphs and obtain reports. +# +# The system only logs operations that were performed in a time equal or +# greater than the amount of milliseconds specified via the +# latency-monitor-threshold configuration directive. When its value is set +# to zero, the latency monitor is turned off. +# +# By default latency monitoring is disabled since it is mostly not needed +# if you don't have latency issues, and collecting data has a performance +# impact, that while very small, can be measured under big load. Latency +# monitoring can easily be enabled at runtime using the command +# "CONFIG SET latency-monitor-threshold " if needed. +latency-monitor-threshold 0 + +############################# EVENT NOTIFICATION ############################## + +# Redis can notify Pub/Sub clients about events happening in the key space. +# This feature is documented at http://redis.io/topics/notifications +# +# For instance if keyspace events notification is enabled, and a client +# performs a DEL operation on key "foo" stored in the Database 0, two +# messages will be published via Pub/Sub: +# +# PUBLISH __keyspace@0__:foo del +# PUBLISH __keyevent@0__:del foo +# +# It is possible to select the events that Redis will notify among a set +# of classes. Every class is identified by a single character: +# +# K Keyspace events, published with __keyspace@__ prefix. +# E Keyevent events, published with __keyevent@__ prefix. +# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ... +# $ String commands +# l List commands +# s Set commands +# h Hash commands +# z Sorted set commands +# x Expired events (events generated every time a key expires) +# e Evicted events (events generated when a key is evicted for maxmemory) +# t Stream commands +# m Key-miss events (Note: It is not included in the 'A' class) +# A Alias for g$lshzxet, so that the "AKE" string means all the events +# (Except key-miss events which are excluded from 'A' due to their +# unique nature). +# +# The "notify-keyspace-events" takes as argument a string that is composed +# of zero or multiple characters. The empty string means that notifications +# are disabled. +# +# Example: to enable list and generic events, from the point of view of the +# event name, use: +# +# notify-keyspace-events Elg +# +# Example 2: to get the stream of the expired keys subscribing to channel +# name __keyevent@0__:expired use: +# +# notify-keyspace-events Ex +# +# By default all notifications are disabled because most users don't need +# this feature and the feature has some overhead. Note that if you don't +# specify at least one of K or E, no events will be delivered. +notify-keyspace-events "" + +############################### GOPHER SERVER ################################# + +# Redis contains an implementation of the Gopher protocol, as specified in +# the RFC 1436 (https://www.ietf.org/rfc/rfc1436.txt). +# +# The Gopher protocol was very popular in the late '90s. It is an alternative +# to the web, and the implementation both server and client side is so simple +# that the Redis server has just 100 lines of code in order to implement this +# support. +# +# What do you do with Gopher nowadays? Well Gopher never *really* died, and +# lately there is a movement in order for the Gopher more hierarchical content +# composed of just plain text documents to be resurrected. Some want a simpler +# internet, others believe that the mainstream internet became too much +# controlled, and it's cool to create an alternative space for people that +# want a bit of fresh air. +# +# Anyway for the 10nth birthday of the Redis, we gave it the Gopher protocol +# as a gift. +# +# --- HOW IT WORKS? --- +# +# The Redis Gopher support uses the inline protocol of Redis, and specifically +# two kind of inline requests that were anyway illegal: an empty request +# or any request that starts with "/" (there are no Redis commands starting +# with such a slash). Normal RESP2/RESP3 requests are completely out of the +# path of the Gopher protocol implementation and are served as usually as well. +# +# If you open a connection to Redis when Gopher is enabled and send it +# a string like "/foo", if there is a key named "/foo" it is served via the +# Gopher protocol. +# +# In order to create a real Gopher "hole" (the name of a Gopher site in Gopher +# talking), you likely need a script like the following: +# +# https://github.com/antirez/gopher2redis +# +# --- SECURITY WARNING --- +# +# If you plan to put Redis on the internet in a publicly accessible address +# to server Gopher pages MAKE SURE TO SET A PASSWORD to the instance. +# Once a password is set: +# +# 1. The Gopher server (when enabled, not by default) will still serve +# content via Gopher. +# 2. However other commands cannot be called before the client will +# authenticate. +# +# So use the 'requirepass' option to protect your instance. +# +# To enable Gopher support uncomment the following line and set +# the option from no (the default) to yes. +# +# gopher-enabled no + +############################### ADVANCED CONFIG ############################### + +# Hashes are encoded using a memory efficient data structure when they have a +# small number of entries, and the biggest entry does not exceed a given +# threshold. These thresholds can be configured using the following directives. +hash-max-ziplist-entries 512 +hash-max-ziplist-value 64 + +# Lists are also encoded in a special way to save a lot of space. +# The number of entries allowed per internal list node can be specified +# as a fixed maximum size or a maximum number of elements. +# For a fixed maximum size, use -5 through -1, meaning: +# -5: max size: 64 Kb <-- not recommended for normal workloads +# -4: max size: 32 Kb <-- not recommended +# -3: max size: 16 Kb <-- probably not recommended +# -2: max size: 8 Kb <-- good +# -1: max size: 4 Kb <-- good +# Positive numbers mean store up to _exactly_ that number of elements +# per list node. +# The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size), +# but if your use case is unique, adjust the settings as necessary. +list-max-ziplist-size -2 + +# Lists may also be compressed. +# Compress depth is the number of quicklist ziplist nodes from *each* side of +# the list to *exclude* from compression. The head and tail of the list +# are always uncompressed for fast push/pop operations. Settings are: +# 0: disable all list compression +# 1: depth 1 means "don't start compressing until after 1 node into the list, +# going from either the head or tail" +# So: [head]->node->node->...->node->[tail] +# [head], [tail] will always be uncompressed; inner nodes will compress. +# 2: [head]->[next]->node->node->...->node->[prev]->[tail] +# 2 here means: don't compress head or head->next or tail->prev or tail, +# but compress all nodes between them. +# 3: [head]->[next]->[next]->node->node->...->node->[prev]->[prev]->[tail] +# etc. +list-compress-depth 0 + +# Sets have a special encoding in just one case: when a set is composed +# of just strings that happen to be integers in radix 10 in the range +# of 64 bit signed integers. +# The following configuration setting sets the limit in the size of the +# set in order to use this special memory saving encoding. +set-max-intset-entries 512 + +# Similarly to hashes and lists, sorted sets are also specially encoded in +# order to save a lot of space. This encoding is only used when the length and +# elements of a sorted set are below the following limits: +zset-max-ziplist-entries 128 +zset-max-ziplist-value 64 + +# HyperLogLog sparse representation bytes limit. The limit includes the +# 16 bytes header. When an HyperLogLog using the sparse representation crosses +# this limit, it is converted into the dense representation. +# +# A value greater than 16000 is totally useless, since at that point the +# dense representation is more memory efficient. +# +# The suggested value is ~ 3000 in order to have the benefits of +# the space efficient encoding without slowing down too much PFADD, +# which is O(N) with the sparse encoding. The value can be raised to +# ~ 10000 when CPU is not a concern, but space is, and the data set is +# composed of many HyperLogLogs with cardinality in the 0 - 15000 range. +hll-sparse-max-bytes 3000 + +# Streams macro node max size / items. The stream data structure is a radix +# tree of big nodes that encode multiple items inside. Using this configuration +# it is possible to configure how big a single node can be in bytes, and the +# maximum number of items it may contain before switching to a new node when +# appending new stream entries. If any of the following settings are set to +# zero, the limit is ignored, so for instance it is possible to set just a +# max entires limit by setting max-bytes to 0 and max-entries to the desired +# value. +stream-node-max-bytes 4096 +stream-node-max-entries 100 + +# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in +# order to help rehashing the main Redis hash table (the one mapping top-level +# keys to values). The hash table implementation Redis uses (see dict.c) +# performs a lazy rehashing: the more operation you run into a hash table +# that is rehashing, the more rehashing "steps" are performed, so if the +# server is idle the rehashing is never complete and some more memory is used +# by the hash table. +# +# The default is to use this millisecond 10 times every second in order to +# actively rehash the main dictionaries, freeing memory when possible. +# +# If unsure: +# use "activerehashing no" if you have hard latency requirements and it is +# not a good thing in your environment that Redis can reply from time to time +# to queries with 2 milliseconds delay. +# +# use "activerehashing yes" if you don't have such hard requirements but +# want to free memory asap when possible. +activerehashing yes + +# The client output buffer limits can be used to force disconnection of clients +# that are not reading data from the server fast enough for some reason (a +# common reason is that a Pub/Sub client can't consume messages as fast as the +# publisher can produce them). +# +# The limit can be set differently for the three different classes of clients: +# +# normal -> normal clients including MONITOR clients +# replica -> replica clients +# pubsub -> clients subscribed to at least one pubsub channel or pattern +# +# The syntax of every client-output-buffer-limit directive is the following: +# +# client-output-buffer-limit +# +# A client is immediately disconnected once the hard limit is reached, or if +# the soft limit is reached and remains reached for the specified number of +# seconds (continuously). +# So for instance if the hard limit is 32 megabytes and the soft limit is +# 16 megabytes / 10 seconds, the client will get disconnected immediately +# if the size of the output buffers reach 32 megabytes, but will also get +# disconnected if the client reaches 16 megabytes and continuously overcomes +# the limit for 10 seconds. +# +# By default normal clients are not limited because they don't receive data +# without asking (in a push way), but just after a request, so only +# asynchronous clients may create a scenario where data is requested faster +# than it can read. +# +# Instead there is a default limit for pubsub and replica clients, since +# subscribers and replicas receive data in a push fashion. +# +# Both the hard or the soft limit can be disabled by setting them to zero. +client-output-buffer-limit normal 0 0 0 +client-output-buffer-limit replica 256mb 64mb 60 +client-output-buffer-limit pubsub 32mb 8mb 60 + +# Client query buffers accumulate new commands. They are limited to a fixed +# amount by default in order to avoid that a protocol desynchronization (for +# instance due to a bug in the client) will lead to unbound memory usage in +# the query buffer. However you can configure it here if you have very special +# needs, such us huge multi/exec requests or alike. +# +# client-query-buffer-limit 1gb + +# In the Redis protocol, bulk requests, that are, elements representing single +# strings, are normally limited ot 512 mb. However you can change this limit +# here, but must be 1mb or greater +# +# proto-max-bulk-len 512mb + +# Redis calls an internal function to perform many background tasks, like +# closing connections of clients in timeout, purging expired keys that are +# never requested, and so forth. +# +# Not all tasks are performed with the same frequency, but Redis checks for +# tasks to perform according to the specified "hz" value. +# +# By default "hz" is set to 10. Raising the value will use more CPU when +# Redis is idle, but at the same time will make Redis more responsive when +# there are many keys expiring at the same time, and timeouts may be +# handled with more precision. +# +# The range is between 1 and 500, however a value over 100 is usually not +# a good idea. Most users should use the default of 10 and raise this up to +# 100 only in environments where very low latency is required. +hz 10 + +# Normally it is useful to have an HZ value which is proportional to the +# number of clients connected. This is useful in order, for instance, to +# avoid too many clients are processed for each background task invocation +# in order to avoid latency spikes. +# +# Since the default HZ value by default is conservatively set to 10, Redis +# offers, and enables by default, the ability to use an adaptive HZ value +# which will temporary raise when there are many connected clients. +# +# When dynamic HZ is enabled, the actual configured HZ will be used +# as a baseline, but multiples of the configured HZ value will be actually +# used as needed once more clients are connected. In this way an idle +# instance will use very little CPU time while a busy instance will be +# more responsive. +dynamic-hz yes + +# When a child rewrites the AOF file, if the following option is enabled +# the file will be fsync-ed every 32 MB of data generated. This is useful +# in order to commit the file to the disk more incrementally and avoid +# big latency spikes. +aof-rewrite-incremental-fsync yes + +# When redis saves RDB file, if the following option is enabled +# the file will be fsync-ed every 32 MB of data generated. This is useful +# in order to commit the file to the disk more incrementally and avoid +# big latency spikes. +rdb-save-incremental-fsync yes + +# Redis LFU eviction (see maxmemory setting) can be tuned. However it is a good +# idea to start with the default settings and only change them after investigating +# how to improve the performances and how the keys LFU change over time, which +# is possible to inspect via the OBJECT FREQ command. +# +# There are two tunable parameters in the Redis LFU implementation: the +# counter logarithm factor and the counter decay time. It is important to +# understand what the two parameters mean before changing them. +# +# The LFU counter is just 8 bits per key, it's maximum value is 255, so Redis +# uses a probabilistic increment with logarithmic behavior. Given the value +# of the old counter, when a key is accessed, the counter is incremented in +# this way: +# +# 1. A random number R between 0 and 1 is extracted. +# 2. A probability P is calculated as 1/(old_value*lfu_log_factor+1). +# 3. The counter is incremented only if R < P. +# +# The default lfu-log-factor is 10. This is a table of how the frequency +# counter changes with a different number of accesses with different +# logarithmic factors: +# +# +--------+------------+------------+------------+------------+------------+ +# | factor | 100 hits | 1000 hits | 100K hits | 1M hits | 10M hits | +# +--------+------------+------------+------------+------------+------------+ +# | 0 | 104 | 255 | 255 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 1 | 18 | 49 | 255 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 10 | 10 | 18 | 142 | 255 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# | 100 | 8 | 11 | 49 | 143 | 255 | +# +--------+------------+------------+------------+------------+------------+ +# +# NOTE: The above table was obtained by running the following commands: +# +# redis-benchmark -n 1000000 incr foo +# redis-cli object freq foo +# +# NOTE 2: The counter initial value is 5 in order to give new objects a chance +# to accumulate hits. +# +# The counter decay time is the time, in minutes, that must elapse in order +# for the key counter to be divided by two (or decremented if it has a value +# less <= 10). +# +# The default value for the lfu-decay-time is 1. A Special value of 0 means to +# decay the counter every time it happens to be scanned. +# +# lfu-log-factor 10 +# lfu-decay-time 1 + +########################### ACTIVE DEFRAGMENTATION ####################### +# +# What is active defragmentation? +# ------------------------------- +# +# Active (online) defragmentation allows a Redis server to compact the +# spaces left between small allocations and deallocations of data in memory, +# thus allowing to reclaim back memory. +# +# Fragmentation is a natural process that happens with every allocator (but +# less so with Jemalloc, fortunately) and certain workloads. Normally a server +# restart is needed in order to lower the fragmentation, or at least to flush +# away all the data and create it again. However thanks to this feature +# implemented by Oran Agra for Redis 4.0 this process can happen at runtime +# in an "hot" way, while the server is running. +# +# Basically when the fragmentation is over a certain level (see the +# configuration options below) Redis will start to create new copies of the +# values in contiguous memory regions by exploiting certain specific Jemalloc +# features (in order to understand if an allocation is causing fragmentation +# and to allocate it in a better place), and at the same time, will release the +# old copies of the data. This process, repeated incrementally for all the keys +# will cause the fragmentation to drop back to normal values. +# +# Important things to understand: +# +# 1. This feature is disabled by default, and only works if you compiled Redis +# to use the copy of Jemalloc we ship with the source code of Redis. +# This is the default with Linux builds. +# +# 2. You never need to enable this feature if you don't have fragmentation +# issues. +# +# 3. Once you experience fragmentation, you can enable this feature when +# needed with the command "CONFIG SET activedefrag yes". +# +# The configuration parameters are able to fine tune the behavior of the +# defragmentation process. If you are not sure about what they mean it is +# a good idea to leave the defaults untouched. + +# Enabled active defragmentation +# activedefrag no + +# Minimum amount of fragmentation waste to start active defrag +# active-defrag-ignore-bytes 100mb + +# Minimum percentage of fragmentation to start active defrag +# active-defrag-threshold-lower 10 + +# Maximum percentage of fragmentation at which we use maximum effort +# active-defrag-threshold-upper 100 + +# Minimal effort for defrag in CPU percentage, to be used when the lower +# threshold is reached +# active-defrag-cycle-min 1 + +# Maximal effort for defrag in CPU percentage, to be used when the upper +# threshold is reached +# active-defrag-cycle-max 25 + +# Maximum number of set/hash/zset/list fields that will be processed from +# the main dictionary scan +# active-defrag-max-scan-fields 1000 + +# Jemalloc background thread for purging will be enabled by default +jemalloc-bg-thread yes + +# It is possible to pin different threads and processes of Redis to specific +# CPUs in your system, in order to maximize the performances of the server. +# This is useful both in order to pin different Redis threads in different +# CPUs, but also in order to make sure that multiple Redis instances running +# in the same host will be pinned to different CPUs. +# +# Normally you can do this using the "taskset" command, however it is also +# possible to this via Redis configuration directly, both in Linux and FreeBSD. +# +# You can pin the server/IO threads, bio threads, aof rewrite child process, and +# the bgsave child process. The syntax to specify the cpu list is the same as +# the taskset command: +# +# Set redis server/io threads to cpu affinity 0,2,4,6: +# server_cpulist 0-7:2 +# +# Set bio threads to cpu affinity 1,3: +# bio_cpulist 1,3 +# +# Set aof rewrite child process to cpu affinity 8,9,10,11: +# aof_rewrite_cpulist 8-11 +# +# Set bgsave child process to cpu affinity 1,10,11 +# bgsave_cpulist 1,10-11 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..95fae6e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,41 @@ +version: "3" +services: + db: + image: postgres:latest + container_name: postgres_db + ports: + - "5433:5432" + volumes: + - ~/WorkSpace/postgres/yapp/data:/var/lib/postgresql/data + environment: + - POSTGRES_USER=user + - POSTGRES_PASSWORD=1234 + privileged: true + +# pgadmin db연결 시, 포워딩한 포트가 아닌 원래포트 사용해야 함 + pgadmin: + image: dpage/pgadmin4 + container_name: pgadmin + ports: + - "5434:80" + volumes: + - ~/WorkSpace/pgadmin/yapp/data:/var/lib/pgadmin + environment: + - PGADMIN_DEFAULT_EMAIL=yappml@pgadmin.com + - PGADMIN_DEFAULT_PASSWORD=1234 + privileged: true + + redis: + image: redis:latest + container_name: redis_db + ports: + - "6000:6379" + volumes: + - ./config/redis:/usr/local/etc/redis + - ~/WorkSpace/redis/yapp/data:/data + environment: + - TZ=Asia/Seoul + command: redis-server /usr/local/etc/redis/redis.conf + restart: always + privileged: true + diff --git a/images/studeep_architecture.png b/images/studeep_architecture.png new file mode 100644 index 0000000..7a8d14c Binary files /dev/null and b/images/studeep_architecture.png differ diff --git a/images/studeep_modeling.png b/images/studeep_modeling.png new file mode 100644 index 0000000..1facaa9 Binary files /dev/null and b/images/studeep_modeling.png differ diff --git a/images/studeep_product.png b/images/studeep_product.png new file mode 100644 index 0000000..e7b39da Binary files /dev/null and b/images/studeep_product.png differ diff --git a/migrations/env.py b/migrations/env.py index ea564ab..ad3d4f4 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -2,7 +2,7 @@ from logging.config import fileConfig from sqlalchemy import engine_from_config, pool -from app.core import settings +from app.core import common_settings from app.database.base import Base # this is the Alembic Config object, which provides @@ -36,7 +36,7 @@ def run_migrations_offline(): script output. """ - url = settings.SQLALCHEMY_DATABASE_URI + url = common_settings.SQLALCHEMY_DATABASE_URI context.configure(url=url, target_metadata=target_metadata, literal_binds=True, dialect_opts={"paramstyle": "named"}, compare_type=True) @@ -52,7 +52,7 @@ def run_migrations_online(): """ configuration = config.get_section(config.config_ini_section) - configuration["sqlalchemy.url"] = settings.SQLALCHEMY_DATABASE_URI + configuration["sqlalchemy.url"] = common_settings.SQLALCHEMY_DATABASE_URI connectable = engine_from_config(configuration, prefix="sqlalchemy.", poolclass=pool.NullPool) with connectable.connect() as connection: diff --git a/migrations/versions/eaae517c1e2f_initialise.py b/migrations/versions/eaae517c1e2f_initialise.py deleted file mode 100644 index d18764f..0000000 --- a/migrations/versions/eaae517c1e2f_initialise.py +++ /dev/null @@ -1,35 +0,0 @@ -"""initialise - -Revision ID: eaae517c1e2f -Revises: -Create Date: 2020-10-01 12:32:07.701738 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = 'eaae517c1e2f' -down_revision = None -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('product', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('name', sa.String(), nullable=False), - sa.Column('price', sa.Float(), nullable=False), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_product_id'), 'product', ['id'], unique=False) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_product_id'), table_name='product') - op.drop_table('product') - # ### end Alembic commands ### diff --git a/requirements.txt b/requirements.txt index 0847dcf..58ca7d0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,10 +1,50 @@ +alembic==1.6.2 +attrs==20.3.0 +bidict==0.21.2 certifi==2020.12.5 +cffi==1.14.5 +chardet==4.0.0 click==7.1.2 -fastapi==0.63.0 +cryptography==3.4.7 +ecdsa==0.14.1 +fastapi==0.65.0 +fastapi-socketio==0.0.6 greenlet==1.0.0 h11==0.12.0 +httptools==0.1.2 +idna==2.10 +iniconfig==1.1.1 +jose==1.0.0 +Mako==1.1.4 +MarkupSafe==1.1.1 +netifaces==0.10.9 +packaging==20.9 +pluggy==0.13.1 +psycopg2==2.8.6 +psycopg2-binary==2.8.6 +py==1.10.0 +pyasn1==0.4.8 +pycparser==2.20 pydantic==1.8.1 +pyparsing==2.4.7 +pytest==6.2.4 +python-dateutil==2.8.1 +python-dotenv==0.17.1 +python-editor==1.0.4 +python-engineio==4.2.0 +python-jose==3.2.0 +python-socketio==5.3.0 +PyYAML==5.4.1 +redis==3.5.3 +requests==2.25.1 +rsa==4.7.2 +six==1.16.0 SQLAlchemy==1.4.6 -starlette==0.13.6 +starlette==0.14.2 +toml==0.10.2 typing-extensions==3.7.4.3 +urllib3==1.26.4 uvicorn==0.13.4 +uvloop==0.15.2 +watchgod==0.7 +websockets==8.1