Python flask_restplus의 Swagger와 Static 폴더 설정 충돌 문제

2020. 3. 13. 19:40서버 프로그래밍

기존에 Flask로 만들어진 백엔드에 Swagger UI를 적용하려 했더니, static 폴더 설정을 위해 Blueprint 사용하던 것과 뭔가 충돌이 있는 것으로 보였다. 한참을 삽질 끝에 static 폴더용 Blueprint와 REST api용 Blueprint를 각각 만들고 app에 등록하면 된다는 것을 알게 되었다.

from flask import Blueprint, render_template
from flask_restplus import Api, Resource, apidoc

app_blueprint = Blueprint(
    'app',
    __name__,
    url_prefix='',
    static_url_path='',
    static_folder='static',
    template_folder='static',
)

@app_blueprint.route('/')
def index():
    return render_template('index.html')

api_blueprint = Blueprint(
    'api',
    __name__,
    url_prefix='/api'
)

api = Api(api_blueprint, title='API', version='1.0', description='API')
api.add_namespace(user_api, path='/user')
api.add_namespace(group_api, path='/group')

@client_blueprint.route('/apidoc/', endpoint='apidoc')
def swagger_ui():
   return apidoc.ui_for(api)

위처럼 두개의 Blueprint 객체를 만들고, 하나는 루트에 연결하고 api는 /api에 연결한다. 

app = Flask(__name__)
app.register_blueprint(app_blueprint)
app.register_blueprint(api_blueprint)
# Calling Api.init_app() is not required here because registering 
# the blueprint with the app takes care of setting up the routing for the application.

app 생성 후, 각각의 Blueprint 객체를 등록하면 웹브라우저로 루트에 접근하면 HTML 파일이 보이고, /api로 접근하면 Swagger UI 페이지가 뜬다. 물론 API들은 /api/user 또는 /api/group 으로 접근해야 한다.

https://flask-restplus.readthedocs.io/en/stable/scaling.html

 

Scaling your project — Flask-RESTPlus 0.13.0 documentation

Scaling your project This page covers building a slightly more complex Flask-RESTPlus app that will cover out some best practices when setting up a real-world Flask-RESTPlus-based API. The Quick start section is great for getting started with your first Fl

flask-restplus.readthedocs.io

https://flask-restplus.readthedocs.io/en/stable/swagger.html#swagger-ui

 

Swagger documentation — Flask-RESTPlus 0.13.0 documentation

Swagger documentation Swagger API documentation is automatically generated and available from your API’s root URL. You can configure the documentation using the @api.doc() decorator. Documenting with the @api.doc() decorator The api.doc() decorator allows

flask-restplus.readthedocs.io

 

그냥 참고용 레퍼런스들. static 폴더를 사용하는 부분이 없기에 큰 도움은 안되었다.

http://michal.karzynski.pl/blog/2016/06/19/building-beautiful-restful-apis-using-flask-swagger-ui-flask-restplus/

 

Building beautiful REST APIs using Flask, Swagger UI and Flask-RESTPlus - Michał Karzyński

This article outlines steps needed to create a REST API using Flask and Flask-RESTPlus. These tools combine into a framework, which automates common tasks: API input validation formatting output (as JSON) generating interactive documentation (with Swagger

michal.karzynski.pl

https://towardsdatascience.com/working-with-apis-using-flask-flask-restplus-and-swagger-ui-7cf447deda7f

 

Working with APIs using Flask, Flask RESTPlus and Swagger UI

An introduction to Flask and Flask-RESTPlus

towardsdatascience.com

 

https://exchangeinfo.tistory.com/64

 

jinja2.exceptions.TemplateNotFound 해결법

jinja2.exceptions.TemplateNotFound 웹도 처음 손대봤는데 처음 보는 flask로 뭔가를 하려니 하나도 안된다.. 하하하핳 저 에러로 검색해보면 가장 먼저 뜨는게 stackoverflow 글이다. 누군가 친절히 링크를 해..

exchangeinfo.tistory.com