fix: 解决部署问题
This commit is contained in:
69
src/app.ts
69
src/app.ts
@@ -6,14 +6,11 @@ import { minioClient } from './modules/minio.ts';
|
||||
import { sequelize } from './modules/sequelize.ts';
|
||||
import { useContextKey, useContext } from '@kevisual/use-config/context';
|
||||
useConfig();
|
||||
export const emit = (channel: string, message?: any) => {
|
||||
redisPublisher.publish(channel, JSON.stringify(message));
|
||||
};
|
||||
export { redis, minioClient, sequelize };
|
||||
|
||||
const init = () => {
|
||||
console.log('init app', global.context);
|
||||
return new App<{ import: any; emit: typeof emit; sequelize: typeof sequelize }>({
|
||||
return new App<{ import: any; sequelize: typeof sequelize }>({
|
||||
serverOptions: {
|
||||
cors: {
|
||||
origin: '*',
|
||||
@@ -22,72 +19,8 @@ const init = () => {
|
||||
io: true,
|
||||
routerContext: {
|
||||
import: dynamicImport,
|
||||
emit,
|
||||
sequelize,
|
||||
},
|
||||
});
|
||||
};
|
||||
export const app = useContextKey('app', init);
|
||||
// @ts-ignore
|
||||
// app.name = 'main-app';
|
||||
// console.log('app context', global.context);
|
||||
const clients = [];
|
||||
// 订阅频道 pageEdit, container 单个页面预览 container 整个页面预览
|
||||
type ClientData = {
|
||||
cid?: string; // container id
|
||||
pid?: string[]; // page id
|
||||
cids?: string[]; // container id
|
||||
type: 'page' | 'container' | 'flow';
|
||||
};
|
||||
type MessageData = {
|
||||
source: 'container';
|
||||
data: any;
|
||||
operation?: 'edit';
|
||||
};
|
||||
redisSubscriber.subscribe('pageEdit', () => {
|
||||
console.log('Subscribed to Redis data-updates channel');
|
||||
});
|
||||
redisSubscriber.on('message', (channel, message) => {
|
||||
if (channel !== 'pageEdit') return;
|
||||
const m = JSON.parse(message) as MessageData;
|
||||
clients.forEach((client) => {
|
||||
const data = client.data as any;
|
||||
const { cid, cids = [], pid } = data || {};
|
||||
const wrapper = (data: any) => {
|
||||
const res = {
|
||||
type: 'pageEdit',
|
||||
source: 'container',
|
||||
data,
|
||||
pid,
|
||||
};
|
||||
return JSON.stringify(res);
|
||||
};
|
||||
const { source, data: mData } = m; // 拆包
|
||||
if (source === 'container') {
|
||||
if (cid === mData?.id) {
|
||||
client.ws.send(wrapper(mData));
|
||||
} else if (cids.includes(mData?.id)) {
|
||||
client.ws.send(wrapper(mData));
|
||||
}
|
||||
}
|
||||
// 其他操作 暂时 不处理
|
||||
// TODO
|
||||
});
|
||||
});
|
||||
app.io.addListener('subscribe', async ({ data, end, ws }) => {
|
||||
const { type } = data || {};
|
||||
if (type === 'pageEdit') {
|
||||
clients.push({ ws, data: data.data }); // 拆包,里面包含的type信息,去掉
|
||||
end({ code: 200, data: 'subscribe success' });
|
||||
} else if (type === 'unsubscribe') {
|
||||
const index = clients.findIndex((client) => client.ws === ws);
|
||||
clients.splice(index, 1);
|
||||
end({ code: 200, data: 'unsubscribe success' });
|
||||
} else {
|
||||
end({ code: 404, data: 'subscribe fail' });
|
||||
}
|
||||
ws.on('close', () => {
|
||||
const index = clients.findIndex((client) => client.ws === ws);
|
||||
clients.splice(index, 1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,3 +15,5 @@ app.listen(config.port, () => {
|
||||
});
|
||||
app.server.on(uploadMiddleware);
|
||||
// }
|
||||
|
||||
console.log(`run ${config.appName} done`);
|
||||
|
||||
@@ -257,7 +257,7 @@ export const createDemoUser = async (username = 'demo', pwd = custom()) => {
|
||||
const u = await User.findOne({ where: { username }, logging: false });
|
||||
if (!u) {
|
||||
const user = await User.createUser(username, pwd, 'demo');
|
||||
console.info('new Users name', user.username);
|
||||
console.info('new Users name', user.username, pwd);
|
||||
return {
|
||||
code: 200,
|
||||
data: { user, pwd: pwd },
|
||||
|
||||
@@ -103,23 +103,24 @@ app
|
||||
}
|
||||
console.log('path', path);
|
||||
const check = await appPathCheck({ key });
|
||||
let appType: string;
|
||||
if (check) {
|
||||
if (!force) {
|
||||
ctx.throw(400, 'App already exists, please remove it first');
|
||||
} else {
|
||||
const app = manager.getAppShowInfo(key);
|
||||
const appType = app?.type;
|
||||
appType = app?.type;
|
||||
|
||||
await manager.removeApp(key);
|
||||
if (appType === 'system-app') {
|
||||
// 如果是system-app(主进程运行),就重载
|
||||
selfRestart();
|
||||
}
|
||||
}
|
||||
}
|
||||
const installAppData = await installApp({ path, key });
|
||||
await manager.add(installAppData.showAppInfo);
|
||||
ctx.body = installAppData;
|
||||
if (appType === 'system-app') {
|
||||
// 如果是system-app(主进程运行),就重启服务
|
||||
setTimeout(() => selfRestart(), 3000);
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ app
|
||||
const token = await user.createToken(null, loginType);
|
||||
ctx.res.cookie('token', token.token, {
|
||||
maxAge: token.expireTime,
|
||||
domain: { domain },
|
||||
domain,
|
||||
sameSite: 'lax',
|
||||
httpOnly: true,
|
||||
});
|
||||
|
||||
9
src/scripts/sync-user.ts
Normal file
9
src/scripts/sync-user.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { User } from '../models/user.ts';
|
||||
|
||||
// User.sync({ alter: true, logging: true }).then(() => {
|
||||
// console.log('sync user done');
|
||||
// });
|
||||
|
||||
User.findOne({ where: { username: 'admin' } }).then((user) => {
|
||||
console.log('user', user);
|
||||
});
|
||||
Reference in New Issue
Block a user