通过 SCF Component 轻松构建 REST API,再也不用熬夜加班了
当一个应用需要对第三方提供服务接口时,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 应用的开发!
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Firefox 73 正式发布
Firefox 73 正式发布了,包含以下更新内容: New 新的 Firefox 版本包含两项功能,可帮助用户更轻松,快速地查看和阅读网站内容。像所有可访问性改进一样,这些功能可以改善每个人的浏览。 Firefox 十多年来一直提供页面缩放功能,允许用户在每个站点上设置缩放级别。对于需要缩放大多数网站的用户,不得不为每个新网站调整缩放可能会很烦人。为了解决这个问题,我们实现了一个新的全局默认缩放级别设置。此选项在“Language and Appearance”下的 about:preferences 中可用,并且可以根据需要从 100% 缩放或为所有站点设置默认缩放级别。每个站点的缩放仍然可以根据需要对单个站点进行调整。 许多视力低下的用户依靠 Windows 的“高对比度模式”使网站更具可读性。传统上,为了提高文本的可读性,当启用“高对比度”模式时,Firefox 会禁用背景图像。在Firefox 73 中,我们引入了“可读性背板”解决方案,该解决方案在文本和背景图像之间放置了一块背景色。现在,高对比度模式下的网站在不禁用背景图片的情况下更具可读性。 Fixed 各种安全修复程序...
- 下一篇
Serverless 的开发者工具建设
本文将介绍 Serverless 生态下的开发者工具,并简述这些工具是如何贯穿开发、调试、测试和部署的生命周期,提升开发者效率的。 由于 Serverless 平台具备弹性扩缩、免运维、按需付费等特点,越来越多的公司和个人开始使用 Serverless 承载项目。但对于开发者而言,Serverless 一定程度上减少了开发难度,可以让我们聚焦业务逻辑进行开发;但同时,由于 Serverless 颠覆了传统的开发模式,从而在相关的开发工具,函数编排组织上也面临着重大挑战。 面向 Serverless 开发的挑战 从一个开发者的角度而言,Serverless 开发和传统的开发方式相比发生了很大的改变。开发者习惯了在本地进行开发,调试,测试和持续集成,持续部署等流程,在面向 Serverless 进行开发时,免不了会有很多疑问: 怎样本地开发一个 Serverless 项目? 怎样对 Serverless 函数进行本地调试? 开发过程中打印的日志是否可以方便的检索并用于 debug? 如何保证本地环境和云端环境的一致? 发布时怎样将多个函数一次性发布到云环境中? 面向 Serverless ...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS8编译安装MySQL8.0.19
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2全家桶,快速入门学习开发网站教程