支持自定义的endpoint
This commit is contained in:
parent
7d465f87c3
commit
51cb5d36fd
|
|
@ -1,4 +1,3 @@
|
|||
/// <reference path="../../src/typings/polyfill.d.ts" />
|
||||
import './polyfill';
|
||||
import { BackendRuntimeContext } from 'oak-frontend-base/lib/context/BackendRuntimeContext';
|
||||
import { Connector, EntityDict } from 'oak-domain/lib/types';
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.startup = void 0;
|
||||
exports.startup = startup;
|
||||
const tslib_1 = require("tslib");
|
||||
/// <reference path="../typings/polyfill.d.ts" />
|
||||
require("./polyfill");
|
||||
|
|
@ -55,7 +55,7 @@ async function startup(path, connector, omitWatchers, omitTimers, routine) {
|
|||
}
|
||||
: serverConfiguration.cors
|
||||
? {
|
||||
origin: serverConfiguration.cors.origin,
|
||||
origin: serverConfiguration.cors.origin, //socket.io配置cors origin是支持数组和字符串
|
||||
allowedHeaders: [
|
||||
...corsHeaders.concat(connector.getCorsHeader()),
|
||||
...(serverConfiguration.cors.headers || []),
|
||||
|
|
@ -259,18 +259,13 @@ async function startup(path, connector, omitWatchers, omitTimers, routine) {
|
|||
});
|
||||
// 注入所有的endpoints
|
||||
const endpoints = appLoader.getEndpoints(connector.getEndpointRouter());
|
||||
endpoints.forEach(([name, method, url, fn]) => {
|
||||
endpoints.forEach(([type, name, method, url, fn]) => {
|
||||
router[method](url, async (ctx) => {
|
||||
const { req, request, params } = ctx;
|
||||
const { body, headers, files } = request;
|
||||
try {
|
||||
const result = await fn(params, headers, req, files ? Object.assign({}, body, files) : body);
|
||||
ctx.response.body = result;
|
||||
return;
|
||||
await fn(ctx);
|
||||
}
|
||||
catch (err) {
|
||||
ctx.response.status = 500;
|
||||
return;
|
||||
console.log("exec:", name, "occure a error: ", err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
@ -302,4 +297,3 @@ async function startup(path, connector, omitWatchers, omitTimers, routine) {
|
|||
};
|
||||
return shutdown;
|
||||
}
|
||||
exports.startup = startup;
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
|
|||
omitWatchers?: boolean,
|
||||
omitTimers?: boolean,
|
||||
routine?: (context: AsyncContext<ED>) => Promise<void>,
|
||||
): Promise<(() => Promise<void> )| undefined> {
|
||||
): Promise<(() => Promise<void>) | undefined> {
|
||||
const serverConfiguration: ServerConfiguration = require(join(
|
||||
path,
|
||||
'lib',
|
||||
|
|
@ -68,11 +68,11 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
|
|||
const socketOption: Partial<ServerOptions> = {
|
||||
path: connector.getSocketPath(),
|
||||
cors: ['development', 'staging'].includes(process.env.NODE_ENV!)
|
||||
? {
|
||||
origin: '*',
|
||||
allowedHeaders: corsHeaders.concat(connector.getCorsHeader()),
|
||||
}
|
||||
: serverConfiguration.cors
|
||||
? {
|
||||
origin: '*',
|
||||
allowedHeaders: corsHeaders.concat(connector.getCorsHeader()),
|
||||
}
|
||||
: serverConfiguration.cors
|
||||
? {
|
||||
origin: serverConfiguration.cors.origin, //socket.io配置cors origin是支持数组和字符串
|
||||
allowedHeaders: [
|
||||
|
|
@ -102,15 +102,15 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
|
|||
}
|
||||
const isCluster = Array.isArray(redisConfig);
|
||||
// 创建 Redis 客户端
|
||||
const pubClient = isCluster ?
|
||||
const pubClient = isCluster ?
|
||||
new Redis.Cluster(redisConfig.map((config) => ({
|
||||
...config,
|
||||
lazyConnect: true,
|
||||
})))
|
||||
: new Redis({
|
||||
...redisConfig,
|
||||
lazyConnect: true,
|
||||
});
|
||||
: new Redis({
|
||||
...redisConfig,
|
||||
lazyConnect: true,
|
||||
});
|
||||
const subClient = pubClient.duplicate();
|
||||
pubClient.on('connect', () => {
|
||||
console.log('PUB已成功连接到Redis服务器');
|
||||
|
|
@ -156,7 +156,7 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
|
|||
const appLoader = clusterInfo.usingCluster
|
||||
? new ClusterAppLoader<ED, Cxt>(
|
||||
path,
|
||||
io.of(DATA_SUBSCRIBE_NAMESPACE ),
|
||||
io.of(DATA_SUBSCRIBE_NAMESPACE),
|
||||
io.of(SOCKET_NAMESPACE),
|
||||
io.of(SERVER_SUBSCRIBER_NAMESPACE),
|
||||
connector.getSocketPath()
|
||||
|
|
@ -329,17 +329,13 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
|
|||
|
||||
// 注入所有的endpoints
|
||||
const endpoints = appLoader.getEndpoints(connector.getEndpointRouter());
|
||||
endpoints.forEach(([name, method, url, fn]) => {
|
||||
endpoints.forEach(([type, name, method, url, fn]) => {
|
||||
router[method](url, async (ctx) => {
|
||||
const { req, request, params } = ctx;
|
||||
const { body, headers, files } = request;
|
||||
try {
|
||||
const result = await fn(params, headers, req, files ? Object.assign({}, body, files) : body);
|
||||
ctx.response.body = result;
|
||||
return;
|
||||
await fn(ctx);
|
||||
} catch (err) {
|
||||
ctx.response.status = 500;
|
||||
return;
|
||||
console.log("exec:", name , "occure a error: ", err);
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue