通过 SCF Component 轻松构建 REST API,再也不用熬夜加班了
本教程将分享如何通过 Serverless SCF Component 、云函数 SCF 及 API 网关组件,快速构建一个 REST API 并实现 GET/PUT 操作。

当一个应用需要对第三方提供服务接口时,REST API 无疑是目前最主流的选择。不过,如果自建 REST API,开发者需要购买虚拟机、配置环境等等,等一切都搞定,可能已经又是一个深夜。
而这些,都可以用 Serverless Framework 来解决。本教程将分享如何通过 Serverless SCF Component 、云函数 SCF 及 API 网关组件,快速构建一个 REST API ,并实现 GET/PUT 操作。

快速构建 REST API
快速开始
1. 安装
安装 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
查看:完整仓库模板
目前 REST API 模板主要展示了 GET/PUT 操作,后续腾讯云 Serverless Framework 也将支持对 Serverless DB 的连接,可以完整实现 CRUD 操作,并支持资源的弹性扩缩容。您可以通过该模板快速开发业务 REST API、扩展代码,探索更丰富的场景。
传送门:
- GitHub: github.com/serverless
- 官网:serverless.com
欢迎访问:Serverless 中文网,您可以在 最佳实践 里体验更多关于 Serverless 应用的开发!
通过 SCF Component 轻松构建 REST API,再也不用熬夜加班了的更多相关文章
- springboot集成swagger2构建RESTful API文档
在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...
- 使用ASP.NET Core 3.x 构建 RESTful API - 2. 什么是RESTful API
1. 使用ASP.NET Core 3.x 构建 RESTful API - 1.准备工作 什么是REST REST一词最早是在2000年,由Roy Fielding在他的博士论文<Archit ...
- 使用ASP.NET Core构建RESTful API的技术指南
译者荐语:利用周末的时间,本人拜读了长沙.NET技术社区翻译的技术标准<微软RESTFul API指南>,打算按照步骤写一个完整的教程,后来无意中看到了这篇文章,与我要写的主题有不少相似之 ...
- 腾讯大牛半年心血高级编程PDF,帮你轻松构建企业级Web应用
毫无疑问,Java 是这些年来最流行的编程语言之一.它无处不在一计算机. 手机.网站以及各种嵌入式设备中都存在着大量的Java 应用程序,而其中应用最为广泛的应该就是Java EE Web应用程序(以 ...
- 在 .NET Core 中构建 REST API
翻译自 Camilo Reyes 2020年8月26日的文章 <Build a REST API in .NET Core> [1] REST API 可以使用简单的动词(如 POST.P ...
- Spring Boot 入门系列(二十二)使用Swagger2构建 RESTful API文档
前面介绍了如何Spring Boot 快速打造Restful API 接口,也介绍了如何优雅的实现 Api 版本控制,不清楚的可以看我之前的文章:https://www.cnblogs.com/zha ...
- Spring MVC中使用 Swagger2 构建Restful API
1.Spring MVC配置文件中的配置 [java] view plain copy <!-- 设置使用注解的类所在的jar包,只加载controller类 --> <contex ...
- php 使用 restler 框架构建 restfull api
php 使用 restler 框架构建 restfull api restler 轻量级,小巧,构建restfull api非常方便! 官网:http://restler3.luracast.com/ ...
- spring boot / cloud (三) 集成springfox-swagger2构建在线API文档
spring boot / cloud (三) 集成springfox-swagger2构建在线API文档 前言 不能同步更新API文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...
随机推荐
- TypeScript躬行记(8)——装饰器
装饰器(Decorator)可声明在类及其成员(例如属性.方法等)之上,为它们提供一种标注,用于分离复杂逻辑或附加额外逻辑,其语法形式为@expression.expression是一个会在运行时被调 ...
- centos批量创建用户并发送邮件,(修订版)
# cat user_create.sh echo -n "创建用户输入C,删除用户输入D!" read name function monitor() { if [ " ...
- Zabbix监控web
1.1 创建主机 1.2 应用集 1.3 创建web场景 1.4 创建触发器
- .NET 半天搭建Jenkins持续集成与自动化部署系统
前言 相信每一位程序员都经历过深夜加班上线的痛苦!而作为一个加班上线如家常便饭的码农,更是深感其痛.由于我们所做的系统业务复杂,系统庞大,设计到多个系统之间的合作,而核心系统更是采用分布式系统架构,由 ...
- 有关call和apply、bind的区别及this指向问题
call和apply都是解决this指向问题的方法,唯一的区别是apply传入的参数除了其指定的this对象之外的参数是一个数组,数组中的值会作为参数按照顺序传入到this指定的对象中. bind是解 ...
- $POJ1742\ Coins$ 多重背包+贪心
Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最 ...
- C++简单项目--推箱子
在处理移动的时候有太多种情况了: 1.有空位 2.在推箱子,推到了空地 3.推箱子推到了目标, 4.推目标位的箱子推到另一个目标 5.推目标位的箱子推到空地 首先记录目标位置,在每次推动之后会再绘画中 ...
- cometoj 茶颜悦色|扫描线+懒惰标记
传送门 题目描述 茶颜悦色也太好喝了!鸡尾酒在长沙的各种茶颜悦色的店铺中流连忘返.他发现长沙有炒鸡多的茶颜悦色店,走两步就能遇到一家. “方圆一公里能有十家茶颜悦色!”鸡尾酒感叹了起来. 于是他想到了 ...
- MySQL之性能优化
查看执行计划explain 1.1 Explain命令:它可以对select语句进行分析,并输出select执行的详细信息,以对开发人员针对性优化 1.2 Explain的用法:在selec ...
- 快速幂模板Super
//求x^nint ans=1;while(n){ if(n&1) ans=ans*x; x*=x; n>>=1;} 快速幂就是快速算底数的n次幂.其时间复杂度为 O(logN), ...