RestFul API

instargram 사진 포스팅 API (1) 기본 세팅(Setting) 하기

567Rabbit 2024. 5. 30. 17:39

API 만들기 전에, 기본 셋팅 하기

 

 

(1). serverless로 aws-posting-server를 만들고 vscode로 열어주었다.

 

https://codebunny99.tistory.com/105

 

RestFul API를 Serverless Framework로 연결하기

*** 아나콘다 프롬프트에서 가상환경 만들면서 flask에 필요한 라이브러리를 다운 받아준다 *** conda create -n lambda_310 python=3.10pip install flask flask-restful mysql-connector-python psycopg2-binary passlib flask-jwt-exte

codebunny99.tistory.com

 

 

 

(2). 도커 설정을 해준다.

 

https://codebunny99.tistory.com/108

 

AWS 서버 연결 시, 라이브러리 용량 줄여주는 Docker(도커) 설치하기

https://www.docker.com/products/docker-desktop/ Docker Desktop: The #1 Containerization Tool for Developers | DockerDocker Desktop is collaborative containerization software for developers. Get started and download Docker Desktop today on Mac, Windows, or

codebunny99.tistory.com

 

 

 

 

(3). 깃허브와 연결해준다.

 

https://codebunny99.tistory.com/106

 

Serverless로 만든 프로젝트 폴더를 깃허브(Github)와 연결하기

cmd(명령프롬프트)   serverless 라고 입력한다 serverless   방향키로 Flask API로 이동한다         깃허브(Github)   New repository하기    VSCode  VSCode Serverless로 아까 만든 폴더 열고, 가상환경

codebunny99.tistory.com

 

 

 

 

(4). 깃허브 액션즈 설정을 한다.

 

https://codebunny99.tistory.com/107

 

Serverless로 만든 프로젝트를 깃허브 액션즈(Github actions)로 자동화하기

사용할 repository를 클릭하고 Settings로 이동한다.     Settings에서 Security -> secrets and variables -> Actions로 이동하고 New repository secret 버튼을 클릭한다.      https://codebunny99.tistory.com/104 완료    

codebunny99.tistory.com

 

 

 

 

(5). MySQL DB와 API 연결하기

 

https://codebunny99.tistory.com/87

 

MySQL DB를 RestFul API와 연결하고 Postman으로 실행하기

Postman   GET의 Request는 Query String(Query Parameters)을 사용한다- offset : 0- limit : 25     DB 설계하기 코드 입력하고 실행하기 (비밀번호 부분 수정해서 입력한다)-- recipe_db 에만 전용으로 접속할 수

codebunny99.tistory.com

 

 

 

 

 

 

 

utils.py

from passlib.hash import pbkdf2_sha256

from config import Config

# 원문 비밀번호를, 단방향으로 암호화 하는 함수
def hash_password(original_password) :
    original_password = original_password + Config.SALT
    password = pbkdf2_sha256.hash(original_password)
    return password

# 유저가 로그인할때, 입력한 비밀번호가 맞는지 체크하는 함수.
def check_password(original_password, hashed_password) :
    original_password = original_password + Config.SALT
    return pbkdf2_sha256.verify(original_password, hashed_password)

# hashed_password = hash_password('1234')
# print(hashed_password)

# check = check_password('1234', '$pbkdf2-sha256$29000$oFQqRcgZo/SeUwoBACCktA$jpiZlfySj8H1cKSLuExVlTpbzBPnbzd8ZGRH2bEEejk')
# print(check)

 

 

 

serverless.yml

service: aws-posting-server

frameworkVersion: '3'

custom:
  wsgi:
    app: app.app

provider:
  name: aws
  region : ap-northeast-2
  # 컨테이너 사용위한 추가 문구
  ecr:
    images:
      appimage:
        path: ./

functions:
  api:
    # 핸들러도 삭제 하고 iamge, timeout 추가, events 는 그냥 냅둔다
    image:
      name: appimage
    timeout: 120
    events:
      - httpApi: '*'

 

 

 

requirements.txt

Flask==1.1.4
Werkzeug==1.0.1
markupsafe==2.0.1

flask-restful

serverless-wsgi

mysql-connector-python
psycopg-binary
passlib
flask-jwt-extended
email-validator

 

 

 

mysql_connection.py

import mysql.connector
from config import Config

# mysql db에 접속하는 함수

def get_connection() :
    
    connection = mysql.connector.connect(
        host = Config.HOST, 
        database = Config.DATABASE,
        user = Config.DB_USER, 
        password = Config.DB_PASSWORD
    )
    return connection

 

 

 

 

 

config.py