๐Ÿ‘ฆ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„/TIL(Today I Learned)

TIL_220520_ํŒ€ ํ”„๋กœ์ ํŠธ flask_blueprint

  • -

์ด๋ฒˆ ๋จธ์‹ ๋Ÿฌ๋‹ ํŒ€ ํ”„๋กœ์ ํŠธ ๋ฐœ์ œ ์ค‘ ๋„์ „๊ณผ์ œ์ธ flask-blueprint ๋ฅผ ์ ์šฉ์‹œ์ผœ ๋ณด์•˜๋‹ค.

 

๊ฐ€์žฅ ๋จผ์ € blueprint ๊ฐ€ ๋ญ”์ง€ ์•Œ์•„๋ณด์ž.

 

Blueprint ๋ž€

๊ธฐ๋ณธ์ ์ธ ๋งค์šฐ ๊ฐ„๋‹จํ•œ ๊ตฌ์กฐ์˜ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‹œ์ž‘ํ•ด ์‹ค์ œ๋กœ ์„œ๋น„์Šคํ•  ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•œ๋‹ค๋ฉด ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๋ฉด์„œ ๋ณต์žกํ•ด์ ธ ๊ธฐ๋Šฅ์„ ์ฐพ๊ธฐ ๋‹ค์†Œ ์–ด๋ ค์›Œ์ง„๋‹ค. ๋˜ํ•œ ํ•œ ์„œ๋น„์Šค๊ฐ€ ํ”Œ๋ผ์Šคํฌ ํ™•์žฅ์„ ํฌํ•จํ•ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ”Œ๋ผ์Šคํฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐ€์ง€๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.

โ€‹

์ด๋•Œ ํ”Œ๋ผ์Šคํฌ์—์„œ๋Š” ๋ธ”๋ฃจํ”„๋ฆฐํŠธ(Blueprint) ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์•ž์„œ ์–ธ๊ธ‰ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ธ”๋ฃจํ”„๋ฆฐํŠธ ๊ฐ์ฒด๋Š” ํฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋‹จ์ˆœํ™”์‹œํ‚ค๊ณ  ํ™•์žฅ ํ”„๋กœ๊ทธ๋žจ๊ณผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋“ฑ๋ก์„ ์œ„ํ•œ ์ค‘์‹ฌ ์ˆ˜๋‹จ์œผ๋กœ๋„ ์‚ฌ์šฉ๋œ๋‹ค. ๋””์žฅ๊ณ (Django)์™€ ๋‹ฌ๋ฆฌ url๋“ค์„ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ๋ถ™์—ฌ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ๊ณ„์† ์ถ”๊ฐ€ํ•ด์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€๋„ ํ•ด๊ฒฐํ•ด์ค€๋‹ค.

โ€‹

ํ”Œ๋ผ์Šคํฌ ๋ธ”๋ฃจํ”„๋ฆฐํŠธ์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

- ๋ธ”๋ฃจํ”„๋ฆฐํŠธ๋ณ„ URL ๊ทœ์น™ ์„ค์ • (์ ‘๋‘์–ด ๋ฐ ์„œ๋ธŒ๋„๋ฉ”์ธ)

- ๋ธ”๋ฃจํ”„๋ฆฐํŠธ๋ณ„ URL ํ•ธ๋“ค๋Ÿฌ ์„ค์ • (before_request ๋“ฑ)

- ๋ธ”๋ฃจํ”„๋ฆฐํŠธ๋ณ„ ์ •์  ํŒŒ์ผ ๋ฐ ํ…œํ”Œ๋ฆฟ ํ•„ํ„ฐ์™€ ํด๋” ์„ค์ •

 

๋ผ๊ณ  ํ•œ๋‹ค. 

 

์‰ฝ๊ฒŒ ๋งํ•ด์„œ ์œˆ๋„์šฐ์˜ ๊ฒŒ์ž„์€ ๊ฒŒ์ž„๋ณ„๋กœ ์‚ฌ์ง„์€ ์‚ฌ์ง„๋ณ„๋กœ ๋“ฑ ๊น”๋”ํ•˜๊ฒŒ ํด๋”๋ฅผ ์ •๋ฆฌ ํ•˜๋“ฏ์ด ํ”„๋กœ์ ํŠธ ํŒŒ์ผ๋“ค๋„ ์ •๋ฆฌํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

 

์–ด๋–ป๊ฒŒ ์ ์šฉ์‹œ์ผฐ๋Š”์ง€ ํ•œ ๋ฒˆ ๋ณด์ž.

 

run.py

์‹คํ–‰ํ•˜๋Š” ํฌํŠธ๋ฅผ ์ง€์ •ํ•œ๋‹ค.

from main import app


if __name__ == '__main__':
    app.run('0.0.0.0', port=9999, debug=True)

 

 

__init__.py

app ์˜ ๊ฐ์ฒด ์„ ์–ธ, ๊ฐ์ข… ๋ชจ๋“ˆ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ๋ธ”๋ฃจํ”„๋ฆฐํŠธ ์„ค์ •

app.register_blueprint() ๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ

# app ๊ฐ์ฒด ์„ ์–ธ, ๊ฐ์ข… ๋ชจ๋“ˆ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ๋ธ”๋ฃจํ”„๋ฆฐํŠธ ์„ค์ •
from datetime import datetime, timedelta
from functools import wraps
import hashlib
import json
import re
from unittest import result
from bson import ObjectId
from flask import Flask, abort, jsonify, request, Response, Blueprint
from flask_cors import CORS  # flask ์—ฐ๊ฒฐ
from pymongo import MongoClient  # DB
import jwt
# from flask import g


from . import member  # member ํ˜ธ์ถœ


# ๊ฐ์ฒด ์„ ์–ธ
app = Flask(__name__)  # app ๊ฐ์ฒด ์„ ์–ธ
cors = CORS(app, resources={r"*": {"origins": "*"}})
client = MongoClient('localhost', 27017)
db = client.ladder


# url ์—ฐ๊ฒฐ
app.register_blueprint(member.blueprint)

 

config.py(DB, ์‹œํฌ๋ฆฟํ‚ค ๋“ฑ)

๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” DB ๋ฅผ ํ•จ์ˆ˜ํ™” ํ•˜์—ฌ return ํ•ด์ค€๋‹ค.

from pymongo import MongoClient  # DB

SECRET_KEY = ''

def get_db():
    client = MongoClient('localhost', 27017)
    return client.ladder

def get_key():
    return SECRET_KEY

 

member.py (์—ฐ๊ฒฐํ•˜๊ณ ์ž ํ•˜๋Š” .py)

์‹คํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ชจ์•„๋‘” ๊ฒฝ๋กœ๋ฅผ @blueprint.route() ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ ,

__init__.py ๋กœ ๋„˜๊ฒจ์ค„ blueprint = Blueprint("member", __name__, url_prefix='') ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•œ๋‹ค.

from main import *
from flask import Blueprint

from . import config


db = config.get_db()
SECRET_KEY = config.get_key()


blueprint = Blueprint("member", __name__, url_prefix='')


@blueprint.route('/')
@authorize  # decorated ํ•จ์ˆ˜ ์ ์šฉ
def index(user):
    # print(user)
    return jsonify({'message': 'success'})

 

 
 

์–ด๋ ค์› ๋˜ ์ 

๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด์„œ ์ ์šฉ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ๋งŽ์ด ์ฐพ์•„๋ณด์•˜๋Š”๋ฐ ์ƒ๊ฐ๋ณด๋‹ค ๋„ˆ๋ฌด ๋‹จ์ˆœํ•˜๊ฒŒ ์•Œ๋ ค์ง„ ๋Š๋‚Œ์ด์˜€๋Š”๋ฐ ๋ง‰์ƒ ์ ์šฉํ•ด๋ณด๋‹ˆ ์–ด๋ ต๋”๋ผ.

 

1) DB ์—ฐ๊ฒฐ ๋ฌธ์ œ & SECRET_KEY

__init__.py ์— ํ•œ๋ฒˆ์— ๋‹ค ๋„ฃ์–ด์ฃผ๋ฉด ๋˜๋Š” ์ค„๋กœ๋งŒ ์•Œ์•˜๋Š”๋ฐ, ์•ˆ๋˜๋”๋ผ. ๊ทธ๋ž˜์„œ ํŠœํ„ฐ๋‹˜๊ป˜ ์งˆ๋ฌธํ•ด๋ณด๋‹ˆ db.py ๋งŒ์˜ ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด ํ•จ์ˆ˜๋กœ ์ด์ฃผ๋ฉด ์–ด๋–จ๊นŒ ํ•˜์…”์„œ ํ•ด๋ดค๋‹ค. ์ž˜ ๋˜์—ˆ๋‹ค ^^

 

2) from & import ํ•  ๋ถ€๋ถ„์„ ๊ผญ๊ผญ ํ™•์ธํ•  ๊ฒƒ

๊ฒฐ๊ตญ ํŒŒ์ผ๋งˆ๋‹ค ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ๋Š” from & import ๋Š” ์ž์นซ ์˜คํƒ€๋ฅผ ๋‚ด์–ด์„œ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š” ์ƒํ™ฉ์„ ๋‚ด์šฉ ํƒ“์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ๊ผญ ์œ„์—๋„ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.