您好,登錄后才能下訂單哦!
小編給大家分享一下如何通過(guò)SCF Component輕松構(gòu)建REST API,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
當(dāng)一個(gè)應(yīng)用需要對(duì)第三方提供服務(wù)接口時(shí),REST API 無(wú)疑是目前最主流的選擇。不過(guò),如果自建 REST API,開(kāi)發(fā)者需要購(gòu)買虛擬機(jī)、配置環(huán)境等等,等一切都搞定,可能已經(jīng)又是一個(gè)深夜。
而這些,都可以用 Serverless Framework 來(lái)解決。本教程將分享如何通過(guò) Serverless SCF Component 、云函數(shù) SCF 及 API 網(wǎng)關(guān)組件,快速構(gòu)建一個(gè) REST API ,并實(shí)現(xiàn) GET/PUT 操作。
安裝 Serverless Framework
$ npm install -g serverless
通過(guò)如下命令直接下載該例子,目錄結(jié)構(gòu)如下:
$ 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()
通過(guò) sls
命令進(jìn)行部署,并可以添加 --debug
參數(shù)查看部署過(guò)程中的信息
如您的賬號(hào)未登陸或注冊(cè)騰訊云,您可以直接通過(guò)微信掃描命令行中的二維碼進(jìn)行授權(quán)登陸和注冊(cè)。
$ 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
通過(guò)如下命令測(cè)試 REST API 的返回情況:
注:如 Windows 系統(tǒng)中未安裝
curl
,也可以直接通過(guò)瀏覽器打開(kāi)對(duì)應(yīng)鏈接查看返回情況
$ 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"}
可以通過(guò)以下命令移除 REST API 應(yīng)用
$ 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
當(dāng)前默認(rèn)支持 CLI 掃描二維碼登錄,如您希望配置持久的環(huán)境變量/秘鑰信息,也可以本地創(chuàng)建 .env
文件
$ touch .env # 騰訊云的配置信息
在 .env
文件中配置騰訊云的 SecretId 和 SecretKey 信息并保存
如果沒(méi)有騰訊云賬號(hào),可以在此注冊(cè)新賬號(hào)。
如果已有騰訊云賬號(hào),可以在 API 密鑰管理中獲取 SecretId
和SecretKey
.
# .env TENCENT_SECRET_ID=123 TENCENT_SECRET_KEY=123
以上是“如何通過(guò)SCF Component輕松構(gòu)建REST API”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。