Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rocknroll17 committed Nov 2, 2024
2 parents aca0c6f + 7a2590d commit 190561f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI/CD for FastAPI

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9' # Use the version you need

- name: Install dependencies
run: |
python -m pip install --upgrade pip
- name: EC2로 SSH 접속하여 git pull
env:
SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }}
EC2_USER: 'ubuntu'
EC2_HOST: 'ec2-3-38-23-48.ap-northeast-2.compute.amazonaws.com'
run: |
# 개인 키를 파일에 저장
echo "${SSH_PRIVATE_KEY}" > private_key
chmod 600 private_key
# EC2 인스턴스에 SSH 접속하여 git pull 실행
ssh -i private_key -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST 'cd ~/mirisa_server && git pull origin main'
- name: Restart the FastAPI application
env:
SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }}
EC2_USER: 'ubuntu'
EC2_HOST: 'your_ec2_ip'
run: |
echo "${SSH_PRIVATE_KEY}" > private_key
chmod 600 private_key
ssh -i private_key $EC2_USER@$EC2_HOST 'sudo lsof -t -i :1500 | xargs sudo kill -9 && screen -S server -X stuff "source myenv/bin/activate\n" && screen -S server -X stuff "cd mirisa_server/\n" && screen -S server -X stuff "python3 main.py\n"'
10 changes: 6 additions & 4 deletions Data/user.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from pydantic import BaseModel

class User(BaseModel):
user_id : int
class UserBase(BaseModel):
name : str
password : str

class UserRead(User):
class UserRead(UserBase):
user_id : str

class UserCreate(UserBase):
pass

class UserCreate(User):
class LoginUser(UserBase):
pass
3 changes: 2 additions & 1 deletion Router/item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import List

from fastapi.responses import JSONResponse

from Data.user import LoginUser, UserCreate
from Database.database import get_db
from fastapi import APIRouter
from starlette.status import *
Expand Down Expand Up @@ -52,4 +54,3 @@ def add_item(request : Request, user_id: str, item_id: str):
@router.post("/{user_id}/receipt")
def add_items(request : Request, user_id: str):
return JSONResponse(status_code=HTTP_200_OK, content={"message": "Item added successfully"})

37 changes: 37 additions & 0 deletions Router/login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from fastapi.responses import JSONResponse

from Data.user import LoginUser, UserCreate
from Database.database import get_db
from fastapi import APIRouter
from starlette.status import *
from fastapi import Query, Request
from Database.models import User
from Service.useritem_service import UserItemService


router = APIRouter(tags=["login"], prefix="/login")


@router.post("/signup")
def signup(request: Request, userCreate: UserCreate):
user = User(
name=userCreate.name,
password=userCreate.password
)
with get_db() as db:
db.add(user)
db.commit()
db.refresh(user)

UserItemService.init_userItem(user.user_id)
return JSONResponse(status_code=HTTP_200_OK, content={"message": "User added successfully"})


@router.post("/signin")
def signin(request: Request, user: LoginUser):
with get_db() as db:
user = db.query(User).filter(User.name == user.name).first()
if user is None:
return JSONResponse(status_code=HTTP_204_NO_CONTENT, content={"message": "User not found"})

return JSONResponse(status_code=HTTP_200_OK, content={"message": "User signed in successfully", "user_id": user.user_id})

0 comments on commit 190561f

Please sign in to comment.