Easily build REST API through SCF Component, no need to stay up late and work overtime

When an application needs to provide a service interface to a third party, REST API is undoubtedly the most popular choi...
Quick start

When an application needs to provide a service interface to a third party, REST API is undoubtedly the most popular choice at present. However, if you build your own REST API, developers need to buy virtual machines, configure the environment, and so on. It may be another midnight.

All of these can be solved with the Serverless Framework. This tutorial will share how to quickly build a REST API and implement GET/PUT operation through Serverless SCF Component, cloud function SCF and API gateway component.

Quickly build REST API

Quick start

  1. install
  2. To configure
  3. deploy
  4. test
  5. remove

1. installation

Install Serverless Framework

$ npm install -g serverless

2. configuration

Download the example directly through the following command. The directory structure is as follows:

$ serverless create --template-url https://github.com/serverless/components/tree/master/templates/tencent-python-rest-api
. ├── code | └── index.py └── serverless.yml

Check the code/index.py, and you can see the transfer parameter and return logic of the interface:

# -*- 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. deployment

Deploy through sls command, and you can add -- debug parameter to view the information during deployment

If your account is not Land or register Tencent cloud, you can directly scan the QR code in the command line of wechat to authorize login and registration.

$ 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. test

Test the return of the REST API with the following command:

Note: if curl is not installed in the Windows system, you can also directly open the corresponding link through the browser to view the return status

$ 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. remove

You can remove the REST API Application with the following command

$ 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

Account configuration (optional)

Currently, CLI scan QR code login is supported by default. If you want to configure persistent environment variables / secret key information, you can also create a. env file locally

$ touch .env # Tencent cloud configuration information

Configure the SecretId and SecretKey information of Tencent cloud in the. env file and save them

If there is no Tencent cloud account, you can go here Register a new account.

If you already have a Tencent cloud account, you can API key management Get the secret ID and secret key

# .env TENCENT_SECRET_ID=123 TENCENT_SECRET_KEY=123

See: Complete warehouse template

At present, the REST API template mainly shows GET/PUT operations, and Tencent cloud Serverless Framework It will also support connection to Serverless DB, complete CRUD operation and elastic expansion and contraction of resources. You can use this template to quickly develop business rest APIs, extend code, and explore richer scenarios.

Portal:

Welcome to: Serverless Chinese network , you can Best practices Experience more about Serverless application development!

Recommended reading: Serverless architecture: from principle, design to project implementation

6 February 2020, 09:02 | Views: 2094

Add new comment

For adding a comment, please log in
or create account

0 comments