支持自定义的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',
|
||||
|
|
@ -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