Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
FlaskとDjango以外のAPI開発の選択肢
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
shimakaze-git
April 10, 2019
520
0
Share
FlaskとDjango以外のAPI開発の選択肢
shimakaze-git
April 10, 2019
More Decks by shimakaze-git
See All by shimakaze-git
DjangoとFastAPIによる 実践認証技術
shimakaze_soft
0
910
フロントエンドとバックエンドのコミュニケーションをスムーズにするスキーマ駆動開発
shimakaze_soft
2
560
クリーンアーキテクチャのリポジトリパターン - Pythonでの実装
shimakaze_soft
1
6k
lt20221030.pdf
shimakaze_soft
0
180
Dependabotを使って 運用しているおはなし
shimakaze_soft
0
2.2k
DjangoCongressJP 2019/2020 & 今年にPyConJP初登壇できたはなし
shimakaze_soft
0
460
GAEによるPythonWEBアプリケーションの高速開発
shimakaze_soft
0
3.2k
Python で Dependency Injection(DI) をやるには?
shimakaze_soft
1
3.7k
FalconAPI開発にいいよ!
shimakaze_soft
0
750
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
160
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
120
How to make the Groovebox
asonas
2
2.1k
Docker and Python
trallard
47
3.8k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.9k
Utilizing Notion as your number one productivity tool
mfonobong
4
290
Testing 201, or: Great Expectations
jmmastey
46
8.1k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.5k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
690
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
61
43k
Transcript
FlaskとDjango以外の API開発の選択肢 みんなのPython勉強会 #44 2019 / 2 / 23 @shimakaze_soft
Agenda - Who are you? - Python Frameworks - Falcon
Features
Who are you? おまえだれよ?
Who are you? おまえだれよ? ・Twitter: @shimakaze_soft ・GitHub: shimakaze-git
皆さん、WEBフレームワーク何 使ってますか?
他のPythonフレームワーク - Django - Flask - Pyramid - Bottle -
Responder - Sanic
Pythonの二大巨頭 Flask - 軽量 - WSGI - 学習コスト低 Django -
フルスタック - WSGI - 学習コスト高
Flaskの使う用途 - 機械学習のモデルを読み込ませたエンジンの開発 - 何かシンプルで膨大な処理を行うAPIなど - 初心者がプロトタイプを開発する際など
Flaskの問題点 - パフォーマンスが貧弱 - 最低限の機能しか無い => サードパーティーのライブラリをある程度把握しておかないと いけない => 他のフレームワークに比べてもパフォーマンスはそれほど良
くない
Djangoの使う用途 - 本格的なWEBアプリケーションの開発 - 他のサードパーティなどを入れなくても一通りの開発をしてお きたい(マイグレーション,認証)
Djangoの問題点 - モノシリックな構造になっていく - 大規模開発化していき、設計を怠るとコードが汚くなり、肥大 化していく => 学習コストもそこそこあるため、初心者やちょっとしたものを開 発するのには不向き =>
バックエンドAPIを作りたい場合には不必要な機能も多い
パフォーマンスの良いAPIを開発するには? - Djangoでは不必要な機能も多い - Flaskは機能が貧弱すぎる && パフォーマンス不足 => 最近はフロントエンドでSPA化が進んでいるので、フロントと バックエンドで分離したい場合は?
=> API開発用途に特化した場合に最適なフレームワークは?
Falconとは - Pythonの高速な軽量FW - API開発に特化 - Flaskの10倍以上の速度
Falconとは
Falconとは - Hooks, Middlewareが標 準投搭載 - Cythonでビルドすると更 に高速化
Flacon Features Features - Resource - Routing - Hooks -
Middleware ThirdParty - SQL ORM - Validation Falcon Features
Resource
Resource - MVCモデルでいうControllerに相当 - HTTPメソッド毎にメソッドを記述していく - FalconのResourceはRESTful
Resource - GETリクエストを処理する 場合はon_getメソッド - 引数にはリクエストが入っ たreq,レスポンスが入った resp
Resource - POSTリクエストを処理す る場合はon_postメソッド - リクエストボディを取り出 す場合は req.stream.read()
Resource - PUTリクエスト => on_put() - DELETEリクエスト => on_delete()
Routing
Routing - Routing処理を記述 - URL毎に対応するコントローラーを記述していく
Routing - add_routeメソッドにルー ティングを記述していく - 第一引数(URL) - 第二引数(対応するコント ローラー)
Middleware
Middleware - リクエストが来たときに中間処理を挟むことが可能 - Resourceに行くまでに処理を挟みたい場合 - Resourceが処理を終えたときにレスポンスを返すまでに処 理を挟みたい場合
Middleware - process_request() リクエスト受信後に動作 - process_resource() リソース実行前 - process_response() リソース実行後
Middleware - 起動時にMiddleware指 定 - 複数のMiddlewareを指定 可能
Hooks
Hooks - リクエストの前後に任意の処理を追加できる - Pythonのデコレーターで記述
Hooks - デコレーターで記述 - リソース(クラスに記述) @falcon.before =>Resource処理前の処理 @falcon.after =>Resource処理後の処理
Hooks - デコレーターで記述 - リソース(クラスに記述) @falcon.before =>Resource処理前の処理 @falcon.after =>Resource処理後の処理
Authentication (ThirdParty) falcon-auth - basic-auth - token-auth - jwt-auth
Authentication (JWT sample) FalconAuthMiddlewareとい うミドルウェアを読み込ませる
Authentication (JWT sample)
Authentication (JWT sample) JWTAuthBackendのインスタ ンスをResourceに読み込ま せる
他のThirdPartyとの組み合わせ ORM - SQLAlchemy ValidationCheck - Cerberus - Marshmallow (https://note.mu/shimakaze_soft/n/nb4d8d50730e6)
Falconの問題点 - 同じ軽量フレームワークであるFlaskよりThirdPartyがかなり 少ない - Flask同様にきちんと設計したディレクトリ構成組まないとい けない => DDD(ドメイン駆動設計)などに出てくるオニオンアーキテク チャ、クリーンアーキテクチャなどで設計するのが良いかも
Falconとは? - API開発にオススメ - Cythonでビルドすれば 更に高速化される - Flaskよりも機能豊富
Falcon 最新情報 - 現在の最新バージョンは2.0 - 6~8月辺りに3.0がリリース予定 - ASGI対応予定
Falconのサンプル類載せてます - Falconのサンプルコード類 https://github.com/shimakaze-git/falcon-todo - Note https://note.mu/shimakaze_soft
ありがとうございました!