久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

如何通過SCF Component輕松構建REST API

164次閱讀
沒有評論

共計 3979 個字符,預計需要花費 10 分鐘才能閱讀完成。

丸趣 TV 小編給大家分享一下如何通過 SCF Component 輕松構建 REST API,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

當一個應用需要對第三方提供服務接口時,REST API 無疑是目前最主流的選擇。不過,如果自建 REST API,開發者需要購買虛擬機、配置環境等等,等一切都搞定,可能已經又是一個深夜。

而這些,都可以用 Serverless Framework 來解決。本教程將分享如何通過 Serverless SCF Component、云函數 SCF 及 API 網關組件,快速構建一個 REST API,并實現 GET/PUT 操作。

快速構建 REST API1. 安裝

安裝 Serverless Framework

$ npm install -g serverless

2. 配置

通過如下命令直接下載該例子,目錄結構如下:

$ 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()

3. 部署

通過 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

4. 測試

通過如下命令測試 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}

5. 移除

可以通過以下命令移除 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”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-16發表,共計3979字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 大宁县| 潢川县| 教育| 崇仁县| 景洪市| 额尔古纳市| 南昌市| 偃师市| 舞钢市| 大田县| 泰和县| 剑阁县| 德昌县| 巩义市| 方山县| 永靖县| 平塘县| 龙游县| 铁岭市| 绥阳县| 德化县| 洞口县| 阜新市| 双峰县| 伊宁市| 寿光市| 宝兴县| 高淳县| 武冈市| 正阳县| 和硕县| 交口县| 白沙| 丰原市| 扶余县| 乾安县| 望都县| 银川市| 奇台县| 乐平市| 克东县|