云函数 examples
This commit is contained in:
26
examples/serverless-faas/backend/src/aliyun-fc.ts
Normal file
26
examples/serverless-faas/backend/src/aliyun-fc.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
// 阿里云 - 函数计算:FC
|
||||
// https://cloud.tencent.com/product/scf
|
||||
|
||||
import { init, server } from "./models/server";
|
||||
|
||||
// 阿里云函数计算 - HTTP函数
|
||||
export async function handler(req: any, res: any, context: any) {
|
||||
// JSON 请求
|
||||
if (req.headers['content-type']?.includes('application/json')) {
|
||||
let apiName = req.path.slice(1); // 去除开头的 "/"
|
||||
let ret = await server.inputJSON(apiName, JSON.parse(req.body));
|
||||
res.setStatusCode(ret.isSucc ? 200 : 500);
|
||||
res.send(JSON.stringify(ret));
|
||||
}
|
||||
// 二进制请求
|
||||
else {
|
||||
let output = await server.inputBuffer(req.body);
|
||||
res.send(Buffer.from(output));
|
||||
}
|
||||
}
|
||||
|
||||
// 阿里云函数计算 - 初始化
|
||||
export async function initializer(context: unknown, callback: Function) {
|
||||
await init();
|
||||
callback();
|
||||
}
|
26
examples/serverless-faas/backend/src/api/ApiAddData.ts
Normal file
26
examples/serverless-faas/backend/src/api/ApiAddData.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { ApiCall } from "tsrpc";
|
||||
import { ReqAddData, ResAddData } from "../shared/protocols/PtlAddData";
|
||||
import { AllData } from "./ApiGetData";
|
||||
|
||||
// This is a demo code file
|
||||
// Feel free to delete it
|
||||
|
||||
export async function ApiAddData(call: ApiCall<ReqAddData, ResAddData>) {
|
||||
// Error
|
||||
if (call.req.content === '') {
|
||||
call.error('Content is empty');
|
||||
return;
|
||||
}
|
||||
|
||||
let time = new Date();
|
||||
AllData.unshift({
|
||||
content: call.req.content,
|
||||
time: time
|
||||
})
|
||||
console.log('AllData', AllData)
|
||||
|
||||
// Success
|
||||
call.succ({
|
||||
time: time
|
||||
});
|
||||
}
|
13
examples/serverless-faas/backend/src/api/ApiGetData.ts
Normal file
13
examples/serverless-faas/backend/src/api/ApiGetData.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { ApiCall } from "tsrpc";
|
||||
import { ReqGetData, ResGetData } from "../shared/protocols/PtlGetData";
|
||||
|
||||
// This is a demo code file
|
||||
// Feel free to delete it
|
||||
|
||||
export async function ApiGetData(call: ApiCall<ReqGetData, ResGetData>) {
|
||||
call.succ({
|
||||
data: AllData
|
||||
})
|
||||
}
|
||||
|
||||
export const AllData: { content: string, time: Date }[] = [];
|
8
examples/serverless-faas/backend/src/index.ts
Normal file
8
examples/serverless-faas/backend/src/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { init, server } from "./models/server";
|
||||
|
||||
// 普通 NodeJS 环境入口点
|
||||
async function main() {
|
||||
await init();
|
||||
await server.start();
|
||||
};
|
||||
main();
|
19
examples/serverless-faas/backend/src/models/server.ts
Normal file
19
examples/serverless-faas/backend/src/models/server.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import path from "path";
|
||||
import { HttpServer } from "tsrpc";
|
||||
import { serviceProto } from "../shared/protocols/serviceProto";
|
||||
|
||||
// Create the Server
|
||||
export const server = new HttpServer(serviceProto, {
|
||||
port: 3000,
|
||||
// Remove this to use binary mode (remove from the client too)
|
||||
json: true
|
||||
});
|
||||
|
||||
// Initialize before server start
|
||||
export async function init(delay?: boolean) {
|
||||
// Auto implement APIs
|
||||
await server.autoImplementApi(path.resolve(__dirname, '../api/'), delay);
|
||||
|
||||
// TODO
|
||||
// Prepare something... (e.g. connect the db)
|
||||
};
|
@@ -0,0 +1,16 @@
|
||||
// This is a demo code file
|
||||
// Feel free to delete it
|
||||
|
||||
/**
|
||||
* 增加数据
|
||||
* 此处的注释将会自动附带到生成的 API 文档中
|
||||
*/
|
||||
export interface ReqAddData {
|
||||
/** 要增加的消息内容 */
|
||||
content: string;
|
||||
}
|
||||
|
||||
export interface ResAddData {
|
||||
/** 服务端内容创建时间 */
|
||||
time: Date
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
// This is a demo code file
|
||||
// Feel free to delete it
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
*/
|
||||
export interface ReqGetData {
|
||||
|
||||
}
|
||||
|
||||
export interface ResGetData {
|
||||
/** 返回所有数据列表 */
|
||||
data: {
|
||||
/** 消息内容 */
|
||||
content: string,
|
||||
/** 创建时间 */
|
||||
time: Date
|
||||
}[]
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
export interface BaseRequest {
|
||||
|
||||
}
|
||||
|
||||
export interface BaseResponse {
|
||||
|
||||
}
|
||||
|
||||
export interface BaseConf {
|
||||
|
||||
}
|
@@ -0,0 +1,98 @@
|
||||
import { ServiceProto } from 'tsrpc-proto';
|
||||
import { ReqAddData, ResAddData } from './PtlAddData';
|
||||
import { ReqGetData, ResGetData } from './PtlGetData';
|
||||
|
||||
// This is a demo service proto file (auto generated)
|
||||
// Feel free to delete it
|
||||
|
||||
export interface ServiceType {
|
||||
api: {
|
||||
"AddData": {
|
||||
req: ReqAddData,
|
||||
res: ResAddData
|
||||
},
|
||||
"GetData": {
|
||||
req: ReqGetData,
|
||||
res: ResGetData
|
||||
}
|
||||
},
|
||||
msg: {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const serviceProto: ServiceProto<ServiceType> = {
|
||||
"version": 1,
|
||||
"services": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "AddData",
|
||||
"type": "api"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "GetData",
|
||||
"type": "api"
|
||||
}
|
||||
],
|
||||
"types": {
|
||||
"PtlAddData/ReqAddData": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "content",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlAddData/ResAddData": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "time",
|
||||
"type": {
|
||||
"type": "Date"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlGetData/ReqGetData": {
|
||||
"type": "Interface"
|
||||
},
|
||||
"PtlGetData/ResGetData": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "data",
|
||||
"type": {
|
||||
"type": "Array",
|
||||
"elementType": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "content",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "time",
|
||||
"type": {
|
||||
"type": "Date"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
26
examples/serverless-faas/backend/src/txcloud-scf.ts
Normal file
26
examples/serverless-faas/backend/src/txcloud-scf.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
// 腾讯云 - 云函数:SCF
|
||||
// https://cloud.tencent.com/product/scf
|
||||
|
||||
import { init, server } from "./models/server";
|
||||
|
||||
export async function handler(event: any, context: any) {
|
||||
// init
|
||||
await ensureInit();
|
||||
|
||||
let apiName = event.path.slice(event.requestContext.path.length + 1); // 从 URL 中提取 ApiName
|
||||
let ret = await server.inputJSON(apiName, JSON.parse(event.body));
|
||||
|
||||
return {
|
||||
"statusCode": ret.isSucc ? 200 : 500,
|
||||
"headers": { "Content-Type": "application/json" },
|
||||
"body": JSON.stringify(ret)
|
||||
}
|
||||
}
|
||||
|
||||
let promiseInit: Promise<void> | undefined;
|
||||
async function ensureInit() {
|
||||
if (!promiseInit) {
|
||||
promiseInit = init(true);
|
||||
}
|
||||
return promiseInit;
|
||||
}
|
Reference in New Issue
Block a user