{"componentChunkName":"component---src-templates-best-practice-detail-tsx","path":"/best-practice/2019-12-3-Easy-to-build-REST-API","result":{"data":{"currentBlog":{"id":"1a6791e1-0295-546d-ac0c-c19730a4bac2","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/2020115/1579074115778-1577347088399-website_fang.png","authors":["liujiang"],"categories":["best-practice"],"date":"2019-12-03T00:00:00.000Z","title":"通过 SCF Component 轻松构建 REST API，再也不用熬夜加班了","description":"本教程将分享如何通过 Serverless SCF Component 、云函数 SCF 及 API 网关组件，快速构建一个 REST API 并实现 GET/PUT 操作。","authorslink":["https://github.com/jiangliu5267"],"translators":null,"translatorslink":null,"tags":["Serverless","Restful"],"keywords":"SCF Component,REST API,构建 REST API,Serverless SCF Component","outdated":null},"wordCount":{"words":99,"sentences":25,"paragraphs":25},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2019-12-3-Easy-to-build-REST-API.md","fields":{"slug":"/best-practice/2019-12-3-Easy-to-build-REST-API/","keywords":["go","python","serverless","spa","serverless","action","student","teacher","Serverless","event"]},"html":"<p>当一个应用需要对第三方提供服务接口时，REST API 无疑是目前最主流的选择。不过，如果自建 REST API，开发者需要购买虚拟机、配置环境等等，等一切都搞定，可能已经又是一个深夜。</p>\n<p>REST API 模板使用 Tencent SCF 组件及其触发器能力，方便的在腾讯云创建，配置和管理一个 REST API 应用。您可以通过 Serverless SCF 组件快速构建一个 REST API 应用，实现 GET/PUT 操作。</p>\n<p><img src=\"https://main.qcloudimg.com/raw/918551c66d6fa9c01f3667706d44f1b7.png\" alt=\"Serverless\"></p>\n<h3 id=\"1-安装\"><a href=\"#1-%E5%AE%89%E8%A3%85\" aria-label=\"1 安装 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. 安装</h3>\n<p><strong>安装 Serverless Framework</strong></p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$ npm install -g serverless</code></pre></div>\n<h3 id=\"2-配置\"><a href=\"#2-%E9%85%8D%E7%BD%AE\" aria-label=\"2 配置 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. 配置</h3>\n<p>通过如下命令直接下载该例子，目录结构如下：</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">serverless create --template-url https://github.com/serverless/components/tree/v1/templates/tencent-python-rest-api</code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.\n├── code\n|   └── index.py\n└── serverless.yml</code></pre></div>\n<p>目前 SCF 组件已支持 v2 版本，因此修改了 serverless.yml 文件，改为以下内容：</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\"># serverless.yml \n\ncomponent: scf \nname: apidemo \norg: test \napp: scfApp \nstage: dev \n\ninputs:\n  name: myRestAPI\n  enableRoleAuth: ture\n  src: ./code\n  handler: index.main_handler\n  runtime: Python3.6\n  region: ap-guangzhou\n  description: My Serverless Function\n  memorySize: 128\n  timeout: 20\n  events:\n      - apigw:\n          name: serverless\n          parameters:\n            protocols:\n              - http\n            serviceName: serverless\n            description: the serverless service\n            environment: release\n            endpoints:\n              - path: /users/{user_type}/{action}\n                method: GET\n                description: Serverless REST API\n                enableCORS: TRUE\n                serviceTimeout: 10\n                param:\n                  - name: user_type\n                    position: PATH\n                    required: &#39;TRUE&#39;\n                    type: string\n                    defaultValue: teacher\n                    desc: mytest\n                  - name: action\n                    position: PATH\n                    required: &#39;TRUE&#39;\n                    type: string\n                    defaultValue: go\n                    desc: mytest</code></pre></div>\n<p>查看 code/index.py 代码，可以看到接口的传参和返回逻辑：</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\"># -*- coding: utf8 -*-\n\ndef teacher_go():\n    # todo: teacher_go action\n    return {\n        &quot;result&quot;: &quot;it is student_get action&quot;\n    }\n\ndef student_go():\n    # todo: student_go action\n    return {\n        &quot;result&quot;: &quot;it is teacher_put action&quot;\n    }\n\ndef student_come():\n    # todo: student_come action\n    return {\n        &quot;result&quot;: &quot;it is teacher_put action&quot;\n    }\n\ndef main_handler(event, context):\n    print(str(event))\n    if event[&quot;pathParameters&quot;][&quot;user_type&quot;] == &quot;teacher&quot;:\n        if event[&quot;pathParameters&quot;][&quot;action&quot;] == &quot;go&quot;:\n            return teacher_go()\n    if event[&quot;pathParameters&quot;][&quot;user_type&quot;] == &quot;student&quot;:\n        if event[&quot;pathParameters&quot;][&quot;action&quot;] == &quot;go&quot;:\n            return student_go()\n        if event[&quot;pathParameters&quot;][&quot;action&quot;] == &quot;come&quot;:\n            return student_come()</code></pre></div>\n<h3 id=\"3-部署\"><a href=\"#3-%E9%83%A8%E7%BD%B2\" aria-label=\"3 部署 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. 部署</h3>\n<p>通过 <code class=\"language-text\">sls deploy</code> 命令进行部署，并可以添加 <code class=\"language-text\">--debug</code> 参数查看部署过程中的信息</p>\n<p>如您的账号未<a href=\"https://cloud.tencent.com/login\">登陆</a>或<a href=\"https://cloud.tencent.com/register\">注册</a>腾讯云，您可以直接通过微信扫描命令行中的二维码进行授权登陆和注册。</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$ sls deploy\n\nserverless ⚡ framework\nAction: &quot;deploy&quot; - Stage: &quot;dev&quot; - App: &quot;scfApp&quot; - Instance: &quot;myRestAPI&quot;\n\nFunctionName: myRestAPI\nDescription:  My Serverless Function\nNamespace:    default\nRuntime:      Python3.6\nHandler:      index.main_handler\nMemorySize:   128\nTriggers: \n  apigw: \n    - http://service-jyl9i6mc-1258834142.gz.apigw.tencentcs.com/release/users/{user_type}/{action}\n\n31s › myRestAPI › Success</code></pre></div>\n<h3 id=\"4-测试\"><a href=\"#4-%E6%B5%8B%E8%AF%95\" aria-label=\"4 测试 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. 测试</h3>\n<p>通过如下命令测试 REST API 的返回情况：</p>\n<blockquote>\n<p>注：如 Windows 系统中未安装 <code class=\"language-text\">curl</code>，也可以直接通过浏览器打开对应链接查看返回情况</p>\n</blockquote>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$ curl -XGET http://service-9t28e0tg-1250000000.gz.apigw.tencentcs.com/release/users/teacher/go\n\n{&quot;result&quot;: &quot;it is student_get action&quot;}</code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$ curl -PUT http://service-9t28e0tg-1250000000.gz.apigw.tencentcs.com/release/users/student/go\n\n{&quot;result&quot;: &quot;it is teacher_put action&quot;}</code></pre></div>\n<h3 id=\"5-移除\"><a href=\"#5-%E7%A7%BB%E9%99%A4\" aria-label=\"5 移除 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>5. 移除</h3>\n<p>可以通过以下命令移除 REST API 应用</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$ sls remove --debug\n\n  DEBUG ─ Flushing template state and removing all components.\n  DEBUG ─ Removing any previously deployed API. api-37gk3l8q\n  DEBUG ─ Removing any previously deployed service. service-9t28e0tg\n  DEBUG ─ Removing function\n  DEBUG ─ Request id\n  DEBUG ─ Removed function myRestAPI successful\n\n  7s » myRestAPI » done</code></pre></div>\n<p>查看：<a href=\"https://cloud.tencent.com/document/product/1154/40216\">操作指南</a></p>\n<hr>\n<div id='scf-deploy-iframe-or-md'></div>\n<hr>\n<blockquote>\n<p><strong>传送门：</strong></p>\n<ul>\n<li>GitHub: <a href=\"https://github.com/serverless/serverless/blob/master/README_CN.md\">github.com/serverless</a></li>\n<li>官网：<a href=\"https://serverless.com/\">serverless.com</a></li>\n</ul>\n</blockquote>\n<p>欢迎访问：<a href=\"https://serverlesscloud.cn/\">Serverless 中文网</a>，您可以在 <a href=\"https://serverlesscloud.cn/best-practice\">最佳实践</a> 里体验更多关于 Serverless 应用的开发！</p>","tableOfContents":"<ul>\n<li><a href=\"/best-practice/2019-12-3-Easy-to-build-REST-API/#1-%E5%AE%89%E8%A3%85\">1. 安装</a></li>\n<li><a href=\"/best-practice/2019-12-3-Easy-to-build-REST-API/#2-%E9%85%8D%E7%BD%AE\">2. 配置</a></li>\n<li><a href=\"/best-practice/2019-12-3-Easy-to-build-REST-API/#3-%E9%83%A8%E7%BD%B2\">3. 部署</a></li>\n<li><a href=\"/best-practice/2019-12-3-Easy-to-build-REST-API/#4-%E6%B5%8B%E8%AF%95\">4. 测试</a></li>\n<li><a href=\"/best-practice/2019-12-3-Easy-to-build-REST-API/#5-%E7%A7%BB%E9%99%A4\">5. 移除</a></li>\n</ul>"},"previousBlog":{"id":"74a25989-4611-5393-85ef-698f2c988635","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/20191226/1577353111488-website.png","authors":["liujiang"],"categories":["best-practice"],"date":"2019-12-04T00:00:00.000Z","title":"Hexo + Serverless Framework，简单三步搭建你的个人博客","description":"简单三步，即可通过 Serverless Website 组件快速构建一个 Serverless Hexo 站点。","authorslink":["https://github.com/jiangliu5267"],"translators":null,"translatorslink":null,"tags":["Serverless","Hexo"],"keywords":"Hexo Serverless,Serverless Website,Serverless Hexo 站点","outdated":true},"wordCount":{"words":146,"sentences":34,"paragraphs":34},"fileAbsolutePath":"/opt/build/repo/content/best-practice/2019-12-4-Quickly-build-personal-blog.md","fields":{"slug":"/best-practice/2019-12-4-Quickly-build-personal-blog/","keywords":["nodejs","serverless","website","website 组件","无服务器","hexo","serverless","Serverless","yml","Hexo","website","tencent"]}},"nextBlog":{"id":"8fb3dfc3-b43e-5c5d-82d2-b4c0809761e3","frontmatter":{"thumbnail":"https://img.serverlesscloud.cn/2020114/1578992366948-16e6e6a4c7f54ffb.png","authors":["腾讯 IVWEB 团队"],"categories":["guides-and-tutorials"],"date":"2019-11-15T00:00:00.000Z","title":"NGW，前端新技术赛场：Serverless SSR 技术内幕","description":"腾讯 IVWeb team（负责腾讯互动视频 NOW 直播、花样直播等产品） 经验分享~","authorslink":["https://juejin.im/user/5a9f77666fb9a028c14a01eb"],"translators":null,"translatorslink":null,"tags":["NGW","Serverless SSR"],"keywords":"Serverless SSR,Serverless 新技术","outdated":null},"wordCount":{"words":582,"sentences":109,"paragraphs":109},"fileAbsolutePath":"/opt/build/repo/content/blog/2019-11-15-serverless-ssr.md","fields":{"slug":"/blog/2019-11-15-serverless-ssr/","keywords":["php","serverless","webpack","服务端渲染","同构渲染","无服务器","云函数","函数","直出","Serverless","serverlesscloud"]}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"blogId":"1a6791e1-0295-546d-ac0c-c19730a4bac2","previousBlogId":"74a25989-4611-5393-85ef-698f2c988635","nextBlogId":"8fb3dfc3-b43e-5c5d-82d2-b4c0809761e3"}}}