| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
- ftp
- Jupyter Notebook
- STF_PortForwarding
- insert
- PYTHON
- Jupyter
- STF
- appium server
- nGrinder
- Materials
- 실행권한
- nohup
- openpyxl
- perfect
- kitura
- postgres
- rethinkdb
- SWIFT
- mysql
- postgresql
- ssh
- port forwarding
- nmap
- 28015
- sshpass
- appium
- GoCD
- ubuntu
- centos
- create table
- Today
- Total
don't stop believing
Flask로 API 서버 만들기 (2) - config 와 실행 확인 본문
Developing API Sample Server by Flask
Original Post: How to structure a Flask-RESTPlus web service for production builds
Github: https://github.com/cosmic-byte/flask-restplus-boilerplate
Flask로 API 서버 만들기 (1) - 개발 환경 준비
Flask로 API 서버 만들기 (2) - config 와 실행 확인
Flask로 API 서버 만들기 (3) - User 테이블 만들기
Flask로 API 서버 만들기 (4) - Testing
Flask로 API 서버 만들기 (5) - User Operations
Flask로 API 서버 만들기 (6) - Security and Authentication
Flask를 이용한 개발환경 준비를 완료했다면 이제 개발을 시작해 봅시다.
main directory 안에 config.py를 만들고 API 서버의 설정 정보들을 구성하는 코드를 작성합니다.
$ sudo vim ./app/main/config.py
config.py 파일안에 아래와 같이 작성합니다.
import os
# uncomment the line below for postgres database url from environment variable
# postgres_local_base = os.environ['DATABASE_URL']
basedir = os.path.abspath(os.path.dirname(__file__))
class Config:
SECRET_KEY = os.getenv('SECRET_KEY', 'ngle_api_tongchun')
DEBUG = False
class DevelopmentConfig(Config):
# uncomment the line below to use postgres
# SQLALCHEMY_DATABASE_URI = postgres_local_base
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'flask_boilerplate_main.db')
SQLALCHEMY_TRACK_MODIFICATIONS = False
class TestingConfig(Config):
DEBUG = True
TESTING = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'flask_boilerplate_test.db')
PRESERVE_CONTEXT_ON_EXCEPTION = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
class ProductionConfig(Config):
DEBUG = False
# uncomment the line below to use postgres
# SQLALCHEMY_DATABASE_URI = postgres_local_base
config_by_name = dict(
dev=DevelopmentConfig,
test=TestingConfig,
prod=ProductionConfig
)
key = Config.SECRET_KEY
config.py 파일에는 API 서버의 3가지 환경을 설정할 수 있는 class 가 있습니다.
DevelopmentConfig, TestingConfig, ProductionConfig
main 폴더아래 있는 __init__.py 파일에 아래와 같이 작성합니다.
$ sudo vim ./app/main/__init__.py
아래 script는 config 정보를 받아 flask object를 생성하게 됩니다.
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_bcrypt import Bcrypt from .config import config_by_name db = SQLAlchemy() flask_bcrypt = Bcrypt() def create_app(config_name): app = Flask(__name__) app.config.from_object(config_by_name[config_name]) db.init_app(app) flask_bcrypt.init_app(app) return app
이제 API 서버의 entry point를 만들 차례입니다.
$ sudo vim manage.py
manage.py 파알안에 아래 script를 추가합니다.
import os
import unittest
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from app.main import create_app, db
app = create_app(os.getenv('BOILERPLATE_ENV') or 'dev')
app.app_context().push()
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
@manager.command
def run():
app.run()
@manager.command
def test():
"""Runs the unit tests."""
tests = unittest.TestLoader().discover('app/test', pattern='test*.py')
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
return 0
return 1
if __name__ == '__main__':
manager.run()
이제 저장하고 실행해 보겠습니다.
$ python manage.py run
만약 실행했을때 아래와 같은 warning이 보인다면 FLASK_ENV를 추가해 줘야 합니다.
$ export FLASK_ENV=development
이제 다시 python manage.py run을 실행합니다.
flask 실행까지 확인했으니 다음 post로 넘어가겠습니다.
'Python > Flask' 카테고리의 다른 글
| Flask로 API 서버 만들기 (6) - Security and Authentication (0) | 2018.11.12 |
|---|---|
| Flask로 API 서버 만들기 (5) - User Operations (2) | 2018.11.12 |
| Flask로 API 서버 만들기 (4) - Testing (0) | 2018.11.12 |
| Flask로 API 서버 만들기 (3) - User 테이블 만들기 (2) | 2018.11.12 |
| Flask로 API 서버 만들기 (1) - 개발 환경 준비 (1) | 2018.11.06 |