当一个应用须要对第三方提供服务接口时,REST API 无疑是目前最主流的选择。不过,若是自建 REST API,开发者须要购买虚拟机、配置环境等等,等一切都搞定,可能已经又是一个深夜。html
而这些,均可以用 Serverless Framework 来解决。本教程将分享如何经过 Serverless SCF Component 、云函数 SCF 及 API 网关组件,快速构建一个 REST API ,并实现 GET/PUT 操做。python
安装 Serverless Frameworkgit
$ npm install -g serverless
经过以下命令直接下载该例子,目录结构以下:github
$ serverless create --template-url https://github.com/serverless/components/tree/master/templates/tencent-python-rest-api
. ├── code | └── index.py └── serverless.yml
查看 code/index.py 代码,能够看到接口的传参和返回逻辑:npm
# -*- 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
参数查看部署过程当中的信息segmentfault
如您的帐号未登录或注册腾讯云,您能够直接经过微信扫描命令行中的二维码进行受权登录和注册。api
$ 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
查看:完整仓库模板
目前 REST API 模板主要展现了 GET/PUT 操做,后续腾讯云 Serverless Framework 也将支持对 Serverless DB 的链接,能够完整实现 CRUD 操做,并支持资源的弹性扩缩容。您能够经过该模板快速开发业务 REST API、扩展代码,探索更丰富的场景。
传送门:
- GitHub: github.com/serverless
- 官网:serverless.com
欢迎访问:Serverless 中文网,您能够在 最佳实践 里体验更多关于 Serverless 应用的开发!
推荐阅读: 《Serverless 架构:从原理、设计到项目实战》