云函数 examples

This commit is contained in:
k8w
2021-12-23 23:36:33 +08:00
parent ed409795d9
commit 82663162b7
35 changed files with 5767 additions and 0 deletions

View 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();
}

View 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
});
}

View 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 }[] = [];

View File

@@ -0,0 +1,8 @@
import { init, server } from "./models/server";
// 普通 NodeJS 环境入口点
async function main() {
await init();
await server.start();
};
main();

View 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)
};

View File

@@ -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
}

View File

@@ -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
}[]
}

View File

@@ -0,0 +1,11 @@
export interface BaseRequest {
}
export interface BaseResponse {
}
export interface BaseConf {
}

View File

@@ -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"
}
}
]
}
}
}
]
}
}
};

View 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;
}