小編給大家分享一下如何通過SCF Component輕松構建REST API,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
當一個應用需要對第三方提供服務接口時,REST API 無疑是目前最主流的選擇。不過,如果自建 REST API,開發者需要購買虛擬機、配置環境等等,等一切都搞定,可能已經又是一個深夜。
而這些,都可以用 Serverless Framework 來解決。本教程將分享如何通過 Serverless SCF Component 、云函數 SCF 及 API 網關組件,快速構建一個 REST API ,并實現 GET/PUT 操作。

安裝 Serverless Framework
$ npm install -g serverless
通過如下命令直接下載該例子,目錄結構如下:
$ serverless create --template-url https://github.com/serverless/components/tree/master/templates/tencent-python-rest-api
. ├── code | └── index.py └── serverless.yml
查看 code/index.py 代碼,可以看到接口的傳參和返回邏輯:
# -*- coding: utf8 -*-
def teacher_go():
# todo: teacher_go action
return {
"result": "it is student_get action"
}
def student_go():
# todo: student_go action
return {
"result": "it is teacher_put action"
}
def student_come():
# todo: student_come action
return {
"result": "it is teacher_put action"
}
def main_handler(event, context):
print(str(event))
if event["pathParameters"]["user_type"] == "teacher":
if event["pathParameters"]["action"] == "go":
return teacher_go()
if event["pathParameters"]["user_type"] == "student":
if event["pathParameters"]["action"] == "go":
return student_go()
if event["pathParameters"]["action"] == "come":
return student_come()通過 sls 命令進行部署,并可以添加 --debug 參數查看部署過程中的信息
如您的賬號未登陸或注冊騰訊云,您可以直接通過微信掃描命令行中的二維碼進行授權登陸和注冊。
$ serverless --debug DEBUG ─ Resolving the template's static variables. DEBUG ─ Collecting components from the template. DEBUG ─ Downloading any NPM components found in the template. DEBUG ─ Analyzing the template's components dependencies. DEBUG ─ Creating the template's components graph. DEBUG ─ Syncing template state. DEBUG ─ Executing the template's components graph. DEBUG ─ Compressing function myRestAPI file to /Users/dfounderliu/Desktop/restAPI/component/.serverless/myRestAPI.zip. DEBUG ─ Compressed function myRestAPI file successful DEBUG ─ Uploading service package to cos[sls-cloudfunction-ap-singapore-code]. sls-cloudfunction-default-myRestAPI-1574856533.zip DEBUG ─ Uploaded package successful /Users/dfounderliu/Desktop/restAPI/component/.serverless/myRestAPI.zip DEBUG ─ Creating function myRestAPI DEBUG ─ Updating code... DEBUG ─ Updating configure... DEBUG ─ Created function myRestAPI successful DEBUG ─ Setting tags for function myRestAPI DEBUG ─ Creating trigger for function myRestAPI DEBUG ─ Starting API-Gateway deployment with name myRestAPI.serverless in the ap-singapore region DEBUG ─ Service with ID service-ibmk6o22 created. DEBUG ─ API with id api-pjs3q3qi created. DEBUG ─ Deploying service with id service-ibmk6o22. DEBUG ─ Deployment successful for the api named myRestAPI.serverless in the ap-singapore region. DEBUG ─ Deployed function myRestAPI successful myRestAPI: Name: myRestAPI Runtime: Python3.6 Handler: index.main_handler MemorySize: 128 Timeout: 20 Region: ap-singapore Role: QCS_SCFExcuteRole Description: My Serverless Function APIGateway: - serverless - http://service-ibmk6o22-1250000000.sg.apigw.tencentcs.com/release 10s ? myRestAPI ? done
通過如下命令測試 REST API 的返回情況:
注:如 Windows 系統中未安裝
curl,也可以直接通過瀏覽器打開對應鏈接查看返回情況
$ curl -XGET http://service-9t28e0tg-1250000000.sg.apigw.tencentcs.com/release/users/teacher/go
{"result": "it is student_get action"}$ curl -PUT http://service-9t28e0tg-1250000000.sg.apigw.tencentcs.com/release/users/student/go
{"result": "it is teacher_put action"}可以通過以下命令移除 REST API 應用
$ sls remove --debug DEBUG ─ Flushing template state and removing all components. DEBUG ─ Removing any previously deployed API. api-37gk3l8q DEBUG ─ Removing any previously deployed service. service-9t28e0tg DEBUG ─ Removing function DEBUG ─ Request id DEBUG ─ Removed function myRestAPI successful 7s ? myRestAPI ? done
當前默認支持 CLI 掃描二維碼登錄,如您希望配置持久的環境變量/秘鑰信息,也可以本地創建 .env 文件
$ touch .env # 騰訊云的配置信息
在 .env 文件中配置騰訊云的 SecretId 和 SecretKey 信息并保存
如果沒有騰訊云賬號,可以在此注冊新賬號。
如果已有騰訊云賬號,可以在 API 密鑰管理中獲取 SecretId 和SecretKey.
# .env TENCENT_SECRET_ID=123 TENCENT_SECRET_KEY=123
以上是“如何通過SCF Component輕松構建REST API”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。