With Tencent cloud cloud function to achieve a minimalist API Gateway

With Tencent cloud cloud function to achieve a minimalist API Gateway

Intro

Micro-letter domain name applet requires the record, but no servers continent, and I think the record somewhat cumbersome at first to do a little applet want to give up, and later learned that Tencent cloud cloud function, so use Tencent cloud cloud function implements a simple API gateway to call the real address through the cloud API function, thereby to circumvent the problem of domain name for the record.

Introduction to Cloud function

Tencent and so on function (Serverless Cloud Function, SCF) is Tencent cloud for businesses and developers No server execution environment to help you run the code without the need to purchase and manage server, is a real-time file processing and data processing and other scenes ideal computing platform. You simply write the conditions using SCF platform supported language core code and set the code running, elasticity can safely run code on Tencent cloud infrastructure.

Changes computing resources

With the development of cloud services, computing resources, highly abstract, Tencent cloud offers from physical servers to the cloud across a variety of functions and level of abstraction of computing resources for users to choose.

  • Blackstone physical servers: the physical machine extension units. User computing entity wholly owned by the whole resource, the best security.
  • Cloud server (CVM): cloud server expansion unit, virtualized hardware devices. Users and other tenants to share physical machine resources, CVM can still configure their own indicators of relative and iterative deployment easier.
  • Containers: to serve as an extension unit, virtualized operating system. Test and production environments exactly the same, testing and deployment very easy.
  • Cloud function: The function expansion unit, virtualized run-time environment (Runtime). Is the smallest unit of existing computing resources, it has a fully automated, one-click deploy, highly scalable and other characteristics, is a lightweight service deployment very good choice.

Overview serverless

No server (Serverless) is not that there is no server, means that when you use Serverless, you do not care about the underlying resources, without having to log server and optimization server, just focus on the core code snippet, you can skip the complicated, tedious the basic work. The core code snippet entirely by an event or request triggers, parallel platform automatically adjust resources according to the service request. Serverless have virtually unlimited expansion capability, idle, do not run any resources. Stateless code runs, you can easily achieve rapid iteration, speed deployment.

More Description Reference official documents

Tencent cloud functions currently free credit can be used, not the application, which can be enough:

Free credit

I'm just a demo of this feature, the amount should be sufficient for free, use the minimum memory configuration to run properly properly month no problem, there will be time left.

60 * 60 * 24 * 31 = 2,678,400 < 3,200,000

Create a cloud function

Create a cloud function:

Create a cloud function

Provides a number of default templates, you can create according to their own needs, I choose here empty functions, select the operating environment is nodejs 8.9, then the next step can

Configuration cloud function

If your method is relatively simple and can be edited directly and then complete a good, if more complex, then you can complete the first, well then updates your local editor.

Use

  1. Cloud Gateway API function Source: https://github.com/WeihanLi/ActivityReservation/tree/dev/ActivityReservation.Clients/ReservationApiFunction

  2. Cloud function update

Modify the index.tsfile to be forwarded address

Execute in the directory tsc, js generated after compilation

To perform under the dist directory npm install, install dependencies, currently used only one got, if you use something else please package.jsonadd a file, or performnpm install <package-name> --save

After packing the contents of the dist directory under the zip, you can then upload to the console Tencent cloud

upload code

Note that the archive can not contain dist directory, open the archive after the code is

dist.zip

  • -- node_modules
  • -- httpRequester.js
  • -- index.js
  • -- packages.json
  1. Configuration Trigger:

Configure Trigger

There will be created after creating the API Gateway in the API Gateway trigger a service, and will get the address of an access

Trigger Details

API Service

We visit the following path to access to our api, and examples:

https://service-balxf7hr-1251288923.ap-shanghai.apigateway.myqcloud.com/release/reservationWxAppGateway/api/notice

This request will be forwarded to the actual https://reservation.weihanli.xyz/api/notice

Use this address in the request interface address applet on it, thus temporarily bypass the applet server domain name for the record ~~

The principle

Forwards the request to implement a simple API Gateway

Implementation issues encountered in the process

unable to verify the first certificate

This is the problem https request a certificate of verification, reference StackOverflow https://stackoverflow.com/questions/31673587/error-unable-to-verify-the-first-certificate-in-nodejs/32440021

By setting up an environment variable NODE_TLS_REJECT_UNAUTHORIZED=0to solve

Access api 404

After the visit, by looking at the log, the output of address discovery request, request the path is with the function name, so the function name can be removed

if ((<string>event.path).startsWith('/reservationWxAppGateway')) {
    event.path = (<string>event.path).replace('/reservationWxAppGateway', '');
}

Later found in the eventparameters, there are two event.requestContext.pathto represent the cloud function path, this path is the real path to remove the request

if((<string>event.path).startsWith(`${event.requestContext.path}`)){
    event.path = (<string>event.path).replace(`${event.requestContext.path}`, '');
}

Forward the request header

When forwarding the request header, hostrequest header can not pass, I pass headers when the hostrequest is set to headundefined

headers["host"]= undefined;

Reference

Guess you like

Origin www.cnblogs.com/weihanli/p/implement-api-gateway-via-tencent-cloud-function.html