初始化拼接socket字符串的小问题
This commit is contained in:
parent
57d436c344
commit
5877e7b7de
|
|
@ -231,7 +231,7 @@ async function create(dirName, cmd) {
|
|||
}
|
||||
// 获取package.json内容
|
||||
const packageJson = (0, template_1.packageJsonContent)({
|
||||
name: DEFAULT_PROJECT_NAME, // 后面再统一rename
|
||||
name: DEFAULT_PROJECT_NAME,
|
||||
version,
|
||||
description,
|
||||
cliName: config_1.CLI_NAME,
|
||||
|
|
|
|||
|
|
@ -16,17 +16,30 @@ const sticky_1 = require("@socket.io/sticky");
|
|||
const socket_io_1 = require("socket.io");
|
||||
const DATA_SUBSCRIBER_NAMESPACE = '/ds';
|
||||
const SERVER_SUBSCRIBER_NAMESPACE = process.env.OAK_SSUB_NAMESPACE || '/ssub';
|
||||
function concat(...paths) {
|
||||
return paths.reduce((prev, current) => {
|
||||
if (current.startsWith('/')) {
|
||||
return `${prev}${current}`;
|
||||
}
|
||||
return `${prev}/${current}`;
|
||||
});
|
||||
}
|
||||
async function startup(path, connector, omitWatchers, omitTimers, routine) {
|
||||
const serverConfiguration = require((0, path_1.join)(path, 'lib', 'configuration', 'server'));
|
||||
const serverConfiguration = require((0, path_1.join)(path, 'lib', 'configuration', 'server')).default;
|
||||
const corsHeaders = ['Content-Type', 'Content-Length', 'Authorization', 'Accept', 'X-Requested-With', 'oak-cxt', 'oak-aspect'];
|
||||
const corsMethods = ['PUT', 'POST', 'GET', 'DELETE', 'OPTIONS'];
|
||||
const koa = new koa_1.default();
|
||||
// socket
|
||||
const httpServer = (0, http_1.createServer)(koa.callback());
|
||||
const socketOption = {
|
||||
path: connector.getSubscribeRouter(),
|
||||
cors: serverConfiguration.cors ? {
|
||||
cors: process.env.NODE_ENV === 'development' ? {
|
||||
origin: '*',
|
||||
allowedHeaders: corsHeaders,
|
||||
} : (serverConfiguration.cors ? {
|
||||
origin: serverConfiguration.cors.origin,
|
||||
allowedHeaders: serverConfiguration.cors.headers,
|
||||
} : undefined,
|
||||
} : undefined),
|
||||
};
|
||||
const io = new socket_io_1.Server(httpServer, socketOption);
|
||||
const clusterInfo = (0, oak_backend_base_1.getClusterInfo)();
|
||||
|
|
@ -70,8 +83,6 @@ async function startup(path, connector, omitWatchers, omitTimers, routine) {
|
|||
}));
|
||||
const router = new koa_router_1.default();
|
||||
// 如果是开发环境,允许options
|
||||
const corsHeaders = ['Content-Type', 'Content-Length', 'Authorization', 'Accept', 'X-Requested-With', 'oak-cxt', 'oak-aspect'];
|
||||
const corsMethods = ['PUT', 'POST', 'GET', 'DELETE', 'OPTIONS'];
|
||||
if (['development'].includes(process.env.NODE_ENV)) {
|
||||
koa.use(async (ctx, next) => {
|
||||
ctx.set('Access-Control-Allow-Origin', '*');
|
||||
|
|
@ -132,7 +143,7 @@ async function startup(path, connector, omitWatchers, omitTimers, routine) {
|
|||
if (nginx.port) {
|
||||
url += `:${nginx.port}`;
|
||||
}
|
||||
url += `/${nginx.socketPath}`;
|
||||
url = concat(url, `${nginx.socketPath}`);
|
||||
}
|
||||
else if (clusterInfo.usingCluster) {
|
||||
url += `:${process.env.PM2_PORT || 8080}`;
|
||||
|
|
@ -140,7 +151,7 @@ async function startup(path, connector, omitWatchers, omitTimers, routine) {
|
|||
else {
|
||||
url += `:${port}`;
|
||||
}
|
||||
url += `/${DATA_SUBSCRIBER_NAMESPACE}`;
|
||||
url = concat(url, DATA_SUBSCRIBER_NAMESPACE);
|
||||
router.get(connector.getSubscribePointRouter(), async (ctx) => {
|
||||
const { response } = ctx;
|
||||
response.body = {
|
||||
|
|
|
|||
|
|
@ -18,6 +18,17 @@ import { AccessConfiguration, ServerConfiguration } from 'oak-domain/lib/types/C
|
|||
const DATA_SUBSCRIBER_NAMESPACE = '/ds';
|
||||
const SERVER_SUBSCRIBER_NAMESPACE = process.env.OAK_SSUB_NAMESPACE || '/ssub';
|
||||
|
||||
function concat(...paths: string[]) {
|
||||
return paths.reduce(
|
||||
(prev, current) => {
|
||||
if (current.startsWith('/')) {
|
||||
return `${prev}${current}`;
|
||||
}
|
||||
return `${prev}/${current}`;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt extends SyncContext<ED>>(
|
||||
path: string,
|
||||
connector: Connector<ED, FrontCxt>,
|
||||
|
|
@ -27,17 +38,22 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
|
|||
) {
|
||||
const serverConfiguration: ServerConfiguration = require(
|
||||
join(path, 'lib', 'configuration', 'server')
|
||||
);
|
||||
).default;
|
||||
const corsHeaders = ['Content-Type', 'Content-Length', 'Authorization', 'Accept', 'X-Requested-With', 'oak-cxt', 'oak-aspect'];
|
||||
const corsMethods = ['PUT', 'POST', 'GET', 'DELETE', 'OPTIONS'];
|
||||
|
||||
const koa = new Koa();
|
||||
// socket
|
||||
const httpServer = createServer(koa.callback());
|
||||
const socketOption: Partial<ServerOptions> = {
|
||||
path: connector.getSubscribeRouter(),
|
||||
cors: serverConfiguration.cors ? {
|
||||
cors: process.env.NODE_ENV === 'development' ? {
|
||||
origin: '*',
|
||||
allowedHeaders: corsHeaders,
|
||||
} : (serverConfiguration.cors ? {
|
||||
origin: serverConfiguration.cors.origin,
|
||||
allowedHeaders: serverConfiguration.cors.headers,
|
||||
} : undefined,
|
||||
} : undefined),
|
||||
};
|
||||
const io = new Server(httpServer, socketOption);
|
||||
const clusterInfo = getClusterInfo();
|
||||
|
|
@ -84,8 +100,6 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
|
|||
const router = new KoaRouter();
|
||||
|
||||
// 如果是开发环境,允许options
|
||||
const corsHeaders = ['Content-Type', 'Content-Length', 'Authorization', 'Accept', 'X-Requested-With', 'oak-cxt', 'oak-aspect'];
|
||||
const corsMethods = ['PUT', 'POST', 'GET', 'DELETE', 'OPTIONS'];
|
||||
if (['development'].includes(process.env.NODE_ENV!)) {
|
||||
koa.use(async (ctx, next) => {
|
||||
ctx.set('Access-Control-Allow-Origin', '*');
|
||||
|
|
@ -149,7 +163,7 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
|
|||
if (nginx.port) {
|
||||
url += `:${nginx.port}`;
|
||||
}
|
||||
url += `/${nginx.socketPath}`;
|
||||
url = concat(url, `${nginx.socketPath}`);
|
||||
}
|
||||
else if (clusterInfo.usingCluster){
|
||||
url += `:${process.env.PM2_PORT || 8080}`;
|
||||
|
|
@ -157,7 +171,7 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
|
|||
else {
|
||||
url += `:${port}`;
|
||||
}
|
||||
url += `/${DATA_SUBSCRIBER_NAMESPACE}`;
|
||||
url = concat(url, DATA_SUBSCRIBER_NAMESPACE);
|
||||
router.get(connector.getSubscribePointRouter(), async (ctx) => {
|
||||
const { response } = ctx;
|
||||
response.body = {
|
||||
|
|
|
|||
Loading…
Reference in New Issue