Skip to content

Commit

Permalink
Merge pull request #14 from H4ad/feature/huawei
Browse files Browse the repository at this point in the history
feature: huawei
  • Loading branch information
H4ad authored May 25, 2022
2 parents 2dba1c5 + 677c0d9 commit 2423405
Show file tree
Hide file tree
Showing 14 changed files with 869 additions and 22 deletions.
79 changes: 74 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
[![Semantic Release][semantic-release-img]][semantic-release-url]

Run REST APIs and other web applications using your existing Node.js application framework (Express, Koa, Hapi and
Fastify), on top of AWS Lambda, Amazon API Gateway and many other event sources.
Fastify), on top of AWS Lambda, Huawei and many other clouds.

This library was a refactored version of [@vendia/serverless-express](https://github.com/vendia/serverless-express), I
created a new way to interact and extend event sources by creating contracts to abstract the integrations between each
Expand Down Expand Up @@ -56,7 +56,7 @@ You can quickly use this library as follows:

```ts
import { ServerlessAdapter } from '@h4ad/serverless-adapter';
import { ApiGatewayV2Adapter } from '@h4ad/serverless-adapter/lib/adapters/aws';
import { ApiGatewayV2Adapter, AlbAdapter, SQSAdapter, SNSAdapter } from '@h4ad/serverless-adapter/lib/adapters/aws';
import { ExpressFramework } from '@h4ad/serverless-adapter/lib/frameworks/express';
import { DefaultHandler } from '@h4ad/serverless-adapter/lib/handlers/default';
import { PromiseResolver } from '@h4ad/serverless-adapter/lib/resolvers/promise';
Expand All @@ -70,8 +70,6 @@ export const handler = ServerlessAdapter.new(app)
.addAdapter(new SQSAdapter())
.addAdapter(new SNSAdapter())
.build();

export { handler };
```

Too fast? Ok, I can explain.
Expand Down Expand Up @@ -140,9 +138,14 @@ We support these event sources:
using ([SNSAdapter](./src/adapters/aws/sns.adapter.ts))
- [AWS SQS](https://docs.aws.amazon.com/pt_br/lambda/latest/dg/with-sqs.html) by
using ([SQSAdapter](./src/adapters/aws/sqs.adapter.ts))
- Huawei
- [Http Function](https://support.huaweicloud.com/intl/en-us/usermanual-functiongraph/functiongraph_01_1442.html): Look [this section](#huawei-http-function) about Huawei support for Http Function.
- [Event Function](https://support.huaweicloud.com/intl/en-us/usermanual-functiongraph/functiongraph_01_1441.html): Work in progress.
- Azure
- [The support is coming soon.](https://github.com/H4ad/serverless-adapter/issues/3)
- Firebase
- The support is coming soon.
- Huawei
- GCP
- The support is coming soon.

We support these resolvers:
Expand All @@ -154,6 +157,66 @@ We support these resolvers:
We support these handlers:

- Default by using ([DefaultHandler](src/handlers/default/default.handler.ts))
- Huawei by using ([HttpHuaweiHandler](src/handlers/huawei/http-huawei.handler.ts)) and ([EventHuaweiHandler](#huawei-event-function))

## Huawei

In Huawei, currently we only added support to FunctionGraphV2 with Http Function type.

The difference between Http Function and Event Function is that in Http Function you must expose port 8000 and Huawei will proxy Api Gateway requests to your application.
So, on implementation, this library will create an http server to listen on port 8000 and forward the request to your framework.

In Event Function, you will receive the event from event source in the same way you receive in AWS, an object with some structure, you can see the supported event sources [here](https://support.huaweicloud.com/intl/en-us/devg-functiongraph/functiongraph_02_0102.html).

### Huawei Http Function

To integrate your app with Huawei FunctionGrapth with the Http Function type, you must do the following:

```ts
import { ServerlessAdapter } from '@h4ad/serverless-adapter';
import { ExpressFramework } from '@h4ad/serverless-adapter/lib/frameworks/express';
import { HttpHuaweiHandler } from '@h4ad/serverless-adapter/lib/handlers/huawei';
import { DummyResolver } from '@h4ad/serverless-adapter/lib/resolvers/dummy';
import { DummyAdapter } from '@h4ad/serverless-adapter/lib/adapters/dummy';
import app from './app';

// instead exposing handler, you have the dispose function
// this dispose function is never called
// but you can to close the http server created with him
const dispose = ServerlessAdapter.new(app)
.setHandler(new HttpHuaweiHandler())
.setFramework(new ExpressFramework())
// dummy resolver and adapter is used because
// they are necessary in the core of the library to build
// but is optional to make huawei http function works.
.setResolver(new DummyResolver())
.addAdapter(new DummyAdapter())
.build();
```

> You don't need to expose a variable called `handler` when you choose Http Function, you just need to call build to the library create a http server.
By the way of Huawei architecture in Http Function, they have no use for Resolvers and Adapters, so you need to use the dummy versions because the library requires it.

#### ONE IMPORTANT THING

You need to configure a file called `bootstrap` in the root of folder that you upload to Huawei, is like the file `Procfile` but for Huawei.

In my setup, I configure like:
```
node /opt/function/code/index.js
```

The path `/opt/function/code` is where your code is uploaded when you deploy something and `index.js` is the file that contains the `ServerlessAdapter`.

In the end, the structure of the zip file you upload looks like this:

- `bootstrap`
- `index.js`

### Huawei Event Function

Work in progress.

# Architecture

Expand Down Expand Up @@ -207,6 +270,12 @@ PR just to extend the library's functionality :)
Honestly, I just refactored all the code that the @vendia team and many other contributors wrote, thanks so much to them
for existing and giving us a brilliant library that is the core of my current company.

# Sponsors

| <a href="https://liga.facens.br/"><img height="50" src="https://mlogu6g7z5ex.i.optimole.com/yEwfkqo-4R0ttNtd/w:auto/h:auto/q:mauto/f:avif/http://liga.facens.br/wp-content/uploads/2020/03/logo-1.png" title="The LIGA logo" width="100"/></a> |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|


[build-img]:https://github.com/H4ad/serverless-adapter/actions/workflows/release.yml/badge.svg

[build-url]:https://github.com/H4ad/serverless-adapter/actions/workflows/release.yml
Expand Down
Loading

0 comments on commit 2423405

Please sign in to comment.