1.    Python์œผ๋กœ API ๊ฐœ๋ฐœ

Python์˜ Flask์„ ์ด์šฉํ•˜์—ฌ API์ œ์ž‘์„ ํ•ด๋ณด์ž.

์šฐ์„  PostgreSQL์—์„œ DB ์„ค๊ณ„์— ๋งž๊ฒŒ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•œ๋‹ค.

(DB์ƒ์„ฑ ๋ฐฉ๋ฒ•์€ ๋ณ„๋„ ์„ค๋ช…ํ•˜์ง€ ์•Š๋Š”๋‹ค.)

 

1.1.  Python Flask APIํ™˜๊ฒฝ ์„ค์ •

โŒ˜ config.py ( ์œ„์น˜: linkservice/server/config/config.py )

--------------------------------------------------------------------------------------------------

config.py์€ ๊ฐœ๋ฐœ๊ณ„(Development)์™€ ์šด์˜๊ณ„(Production) ํ™˜๊ฒฝ์„ค์ •์„ ์œ„ํ•œ ๊ฒƒ์œผ๋กœ ์„œ๋ฒ„(server.py) ๊ตฌ๋™ ์‹œ ํ˜ธ์ถœํ•œ๋‹ค.

 

SQLALCHEMY_DATABASE_URI์ •๋ณด๋ฅผ ์—ฌ๋Ÿฌ๋ถ„์˜ DB์ •๋ณด๋กœ ๋ณ€๊ฒฝํ•˜๊ณ , ๋‹ค๋ฅธ ์˜ต์…˜๋“ค์€ ์ฐธ๊ณ ํ•˜์—ฌ ํ•„์š”์‹œ ์ถ”๊ฐ€/์‚ญ์ œ ํ•˜๋ฉด ๋œ๋‹ค.

 

์ด๋ฏธ์ง€ ์ €์žฅ ์œ„์น˜์„ค์ •์€ CDNํ˜น์€ NASํ˜•ํƒœ๋กœ ์ œ์ž‘ํ•˜๊ณ ์ž ๋ณ„๋„ ํด๋”๋กœ ๋ถ„๋ฆฌํ–ˆ์œผ๋ฉฐ, ๋งํฌ์ •๋ณด์™€ ์ด๋ฏธ์ง€ ์ •๋ณด ์ €์žฅ์€ IMAGE_URLํ˜•ํƒœ๋กœ ์ €์žฅ๋œ๋‹ค. 

ํ™˜๊ฒฝํŒŒ์ผ ๋‚ด์šฉ์€ ์•ž์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์„ค๋ช…๋  ๊ฒƒ์ด๋‹ค

 

import os
dir = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))

class Development(object):
    DEBUG = True
    FLASK_APP = 'linkservice_dev'
    ENV = 'development'
    
## DB URL FOR DEVELOPMENT
    SQLALCHEMY_DATABASE_URI = 'postgresql://์‚ฌ์šฉ์ž:์‚ฌ์šฉ์žํŒจ์Šค์›Œ๋“œ@localhost:5432/๋ฐ์ดํƒ€๋ฒ ์ด์Šค๋ช…'

# ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์‚ฌํ•ญ์— ๋Œ€ํ•ด ์ปค๋ฐ‹ ์ „ํ›„๋กœ ์•Œ๋ฆผ ์—ฌ๋ถ€
    SQLALCHEMY_TRACK_MODIFICATIONS = True

# Query Debug ์—ฌ๋ถ€ ๋ฐ DB POOL
    SQLALCHEMY_ECHO = True
    SQLALCHEMY_POOL_SIZE = 20
    SQLALCHEMY_POOL_RECYCLE = 3600

#JSON์—์„œ ํ•œ๊ธ€ ํ‘œํ˜„์„ ์œ„ํ•ด์„œ ๋ฐ˜์˜
    JSON_AS_ASCII = False
    
#์ด๋ฏธ์ง€ ์œ„์น˜ ์„ค์ •
    image_folder = os.path.normpath(os.path.join(dir, os.pardir)) + '/client/public/images/'
    COVER_IMAGE_FORDER = image_folder
    IMAGE_FORDER = image_folder
    IMAGE_URL = 'http://127.0.0.1:3000/images/'

class Production(object):
    DEBUG = False
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_ECHO = False
    FLASK_APP = 'linkservice'
    ENV = 'production'
    
## DB URL FOR PRODUCTION
    SQLALCHEMY_DATABASE_URI = 'postgresql://์‚ฌ์šฉ์ž:์‚ฌ์šฉ์žํŒจ์Šค์›Œ๋“œ@localhost:5432/๋ฐ์ดํƒ€๋ฒ ์ด์Šค๋ช…'

#JSON์—์„œ ํ•œ๊ธ€ ํ‘œํ˜„์„ ์œ„ํ•ด์„œ ๋ฐ˜์˜
    JSON_AS_ASCII = False    

#์ด๋ฏธ์ง€ ์œ„์น˜ ์„ค์ •
    image_folder = os.path.normpath(os.path.join(dir, os.pardir)) + '/images/'
    COVER_IMAGE_FORDER = image_folder
    IMAGE_FORDER = image_folder 

#์šด์˜๊ณ„ 
    IMAGE_URL = 'https://www.linkservice.com/img/'

app_config = {
    'development': Development(),
    'production': Production(),
}

โŒ˜ create_app.py ( ์œ„์น˜: linkservice/server/config/create_app.py)

---------------------------------------------------------------------------------------------------

create_app.py๋Š” ํ™˜๊ฒฝํŒŒ์ผ์„ ์ฝ์–ด Flask App์„ ์‹คํ–‰ํ•œ๋‹ค.

from . import config
from flask import Flask

def create_app(environment):
    # configํŒŒ์ผ์—์„œ ๋ถˆ๋ ค์˜ค๊ธฐ
    config_map = {
        'development': config.Development(),
        'production': config.Production(),
    }
    config_obj = config_map[environment.lower()]   
    
    app = Flask(__name__)
    app.config.from_object(config_obj)

    return app

+ Recent posts