FLASK ์‹œ์ž‘ํ•˜๊ธฐ๐ŸŒท

pingpong endpoint ๊ตฌํ˜„ํ•˜๊ธฐ!

from flask import Flask

app = Flask(__name__)

@app.route('/ping', methods=['GET']
def ping():
	return 'pong'

๐ŸŒธ app ์ด๋ผ๋Š” ๋ณ€์ˆ˜์— Flask ํด๋ž˜์Šค๋ฅผ ๊ฐ์ฒดํ™” ์‹œ์ผœ์„œ ์ €์žฅํ•œ๋‹ค. app ๋ณ€์ˆ˜๊ฐ€ API ๊ฐ€ ๋˜๋Š” ๊ฒƒ!
๐ŸŒธ route ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—”๋“œํฌ์ธํŠธ๋ฅผ ๋“ฑ๋กํ•œ๋‹ค. ์ฃผ์†Œ ๋ฐ method ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
๐ŸŒธ ์—”๋“œํฌ์ธํŠธ์—์„œ ์‹คํ–‰ํ•  ํ•จ์ˆ˜๋งŒ์„ ์ž‘์„ฑํ•˜๋ฉด flask ๊ฐ€ ์•Œ์•„์„œ HTTP ์‘๋‹ต์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ๋กœ ์ „์†กํ•œ๋‹ค.
๐ŸŒธ ์žฅ๊ณ ์—์„œ๋ผ๋ฉด app/urls.py , app/views.py ์— ๋”ฐ๋กœ ์ž‘์„ฑํ•ด์•ผ ํ–ˆ๋˜ ๋‚ด์šฉ์„ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค! ์‘๋‹ต์„ ์œ„ํ•ด HttpResponse ๋‚˜ JsonReponse ๋ฅผ import ํ•  ํ•„์š”๋„ ์—†๋‹ค.

์—”๋“œํฌ์ธํŠธ ์‹คํ–‰!

FLASK_APP=app.py FLASK_DEBUG=1 flask run

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ