使用 Serverless/Flask/Swagger 在 AWS 上搭建打造 Open API

使用 Serverless/Flask/Swagger 在 AWS 上搭建打造 Open API

前言約在一年前,當時公司的同事帶我玩過 Swagger,只是當時的我寫得不太習慣,直到寫這篇之前都還只是寫 markdown 來做 API 文件 😓,不過寫 API 到最後總是要開放給其他人需要測試以及用文件來描述功能,但只是寫 Readme.md 給其他人看感覺有點不負責任 (?),所以這個時候 Swagger 就發揮它的作用啦~ 但是看著下面這張圖…左邊那是怎樣!怎麼設定了一大堆右邊才出現

約在一年前,當時公司的同事帶我玩過 Swagger,只是當時的我寫得不太習慣,直到寫這篇之前都還只是寫 markdown 來做 API 文件 😓,不過寫 API 到最後總是要開放給其他人需要測試以及用文件來描述功能,但只是寫 Readme.md 給其他人看感覺有點不負責任 (?),所以這個時候 Swagger 就發揮它的作用啦~

但是看著下面這張圖…

左邊那是怎樣!怎麼設定了一大堆右邊才出現一點點東西,估且不論還要設定 Model、Payload 什麼的,我想光寫左邊這份文件加測試的時間我都可以寫一份 Markdown 給其他同仁用 Postman 測試了…🤣🤣🤣

這時候套件就出動啦,這次要介紹的是 flask-restful-swagger-2.0,他是延續原版套件並加以更新的,不得不說再搭更新完後整體寫起來人性多了,像是文先寫法用 decorator 的寫法在路由之前、宣告 Schema 時只要用類別包起來之類的,讓寫文件可以用 python 的寫法去寫,除了特殊的設定需要比對 Swagger 官方文件外,在建一個 Swagger 格式的文件這樣已經超快了 💪

既然如此那就介紹一下他的相關設定並把它記錄下來吧!

若對 Serverless + WSGI +flask 還不清楚的話可以參考我之前寫的文章

這次的範例已經放在 Github 上面,建議可以 Clone 下來邊看會比較清楚,若想要比較 flask-restful 套件生成的版本差異的朋友可以參考分支 — Original

api.py 設定

一般來說,如果有用 flask_restful 套件的話會有以下兩行:

from flask_restful import Api
api = Api(app)

這次因為需要引入 swagger,將程式替代成以下:

from flask_restful_swagger_2 import Api
api = Api(app,
    host="localhost:5000",
    schemes=['http'],
   # schemes=['https'],
   # base_path='/dev',
    security_definitions=security,
    security=[{'appKey': []}],
    api_version='0.01',
    api_spec_url='/api/swagger')
  • host: 在本地端起的預設 port 為 5000 (部署上 AWS 後需要替換掉)

host 在此處可以先註解,設定此項目是如果需要使用 swagger-codegen 來產生相對應的 library 時,所需對應的 host

  • schemes: host 的協定 (部署後需要改成 https)
  • base_path: 一般 API 都會有 /v1、/v2 的版本號可以用在此處,而 serverless 部署上後則會有 /dev
  • security_definitions: 若在 call API 之前需要 OAuth 或是一般的 key 的話則在此地方設定
  • security: 設定完之後要在此告訴 swagger 要啟動這個 key
  • api_version: 使用者定義的版本號
  • api_sepc_url: swagger.json 輸出的路由

在 sls deploy 之後需要將 host、schemes 以及 bash_path 改成部署完的設定

serverless flask swagger

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Serverless NestJS: Document your API with Swagger and AWS API Gateway

Add documentation to your API is really important for share it with your team or with your clients. Documentation with NestJS and Swagger and use your JSON file to edit it as you wish and replace it with the API Gateway Documentation, once you are done you can simply comment your Swagger code in your main.ts file and re-deploy your application or if you want deploy your Swagger-ui in production.

Download a Flask template ready to plug in your business logic

Let’s assume after lots of hard work you have your machine learning model running the way it should. This model could be one which responds to a user’s request to classify a tweet sentiment or identify objects in an image or recommend a product or...

Python Flask-Mail Library to Send Emails in Browser Using Flask Full Project For Beginners

Python Flask-Mail Library to Send Emails in Browser Using Flask Full Project For Beginners #python #flask #flaskmail Welcome Folks My name is Gautam

Working with APIs using Flask, Flask-RESTPlus and Swagger UI

The article discusses about how to use Flask and Flask-RESTPlus to create APIs and then use them to send and retrieve information.

Tencent Cloud and Serverless Join Forces to Bring the Serverless Movement to China

Tencent becomes a Premier Cloud Provider of the Serverless Framework. A few months ago we met with a team from Tencent Cloud to discuss how we could work together to bring the serverless movement to China, and we immediately realized that we both share a similar vision... that serverless is the future of the cloud. We also recognized that Tencent is more ambitious about serverless than other cloud providers.