diff --git a/README.md b/README.md index 20e0e41..cee6282 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ code center + +``` +unzip -x app.config.json5 +``` \ No newline at end of file diff --git a/app.config.json5.example b/app.config.json5.example index e0f6077..96252fe 100644 --- a/app.config.json5.example +++ b/app.config.json5.example @@ -3,6 +3,8 @@ tokenSecret: '', appPath: 'apps', appName: 'codeflow', + domain: '*', + 'mainApp': 'https://kevisual.xiongxiao.me', postgres: { username: 'root', host: 'localhost', @@ -10,6 +12,7 @@ password: '*****', port: 5432, }, + redis: {}, minio: { endPoint: 'minio.xiongxiao.me', bucketName: 'resources', diff --git a/package.json b/package.json index e2639e6..4c0665d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/code-center", - "version": "0.0.1", + "version": "0.0.3", "description": "code center", "type": "module", "main": "index.js", @@ -32,7 +32,7 @@ "license": "UNLICENSED", "dependencies": { "@kevisual/auth": "1.0.5", - "@kevisual/local-app-manager": "0.1.6", + "@kevisual/local-app-manager": "0.1.8", "@kevisual/router": "^0.0.6", "@kevisual/use-config": "^1.0.7", "@types/semver": "^7.5.8", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fb49ceb..a764685 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,8 +17,8 @@ importers: specifier: 1.0.5 version: 1.0.5 '@kevisual/local-app-manager': - specifier: 0.1.6 - version: 0.1.6(@kevisual/router@0.0.6)(@kevisual/types@0.0.6)(@kevisual/use-config@1.0.7) + specifier: 0.1.8 + version: 0.1.8(@kevisual/router@0.0.6)(@kevisual/types@0.0.6)(@kevisual/use-config@1.0.7)(pm2@5.4.3) '@kevisual/router': specifier: ^0.0.6 version: 0.0.6 @@ -339,12 +339,13 @@ packages: '@kevisual/auth@1.0.5': resolution: {integrity: sha512-GwsLj7unKXi7lmMiIIgdig4LwwLiDJnOy15HHZR5gMbyK6s5/uJiMY5RXPB2+onGzTNDqFo/hXjsD2wkerHPVg==} - '@kevisual/local-app-manager@0.1.6': - resolution: {integrity: sha512-ECIM72GS42++xWisE0MVfaby3wGby9Qz+AphpAzYJ286sbSatY7z5cHYKIVeKwIzqU9pyUlgLFDU8UeKCYpsmA==} + '@kevisual/local-app-manager@0.1.8': + resolution: {integrity: sha512-3dRstV+g1zZox7NvkXPpQHmil0rU89bXM15msmbmWUB450FIh1WaaQ7pWT6cQKhTuPO+duvSSAXtbbL7A1TqvQ==} peerDependencies: '@kevisual/router': ^0.0.6-alpha-2 '@kevisual/types': ^0.0.1 '@kevisual/use-config': ^1.0.5 + pm2: ^5.4.3 '@kevisual/router@0.0.6': resolution: {integrity: sha512-7FQUY87Zy5A4V30OAggRbGpO/Asd7SUpnhHv8mlxnSFFTto25xpXmjHYp12mu/HJTsHM7RTaxVEyD1DeP44D2A==} @@ -2509,11 +2510,12 @@ snapshots: '@kevisual/auth@1.0.5': {} - '@kevisual/local-app-manager@0.1.6(@kevisual/router@0.0.6)(@kevisual/types@0.0.6)(@kevisual/use-config@1.0.7)': + '@kevisual/local-app-manager@0.1.8(@kevisual/router@0.0.6)(@kevisual/types@0.0.6)(@kevisual/use-config@1.0.7)(pm2@5.4.3)': dependencies: '@kevisual/router': 0.0.6 '@kevisual/types': 0.0.6 '@kevisual/use-config': 1.0.7 + pm2: 5.4.3 '@kevisual/router@0.0.6': dependencies: diff --git a/rollup.config.mjs b/rollup.config.mjs index f4ab874..ad75ff4 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -73,6 +73,8 @@ const config = { 'socket.io', // socket.io 'minio', // minio + 'pm2', + 'pg', // pg 'pino', // pino 'pino-pretty', // pino-pretty diff --git a/script/release/generate-token.mjs b/script/release/generate-token.mjs new file mode 100644 index 0000000..5b10d51 --- /dev/null +++ b/script/release/generate-token.mjs @@ -0,0 +1,11 @@ +import * as nanoid from 'nanoid'; + +const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; + +//HMAC-256:推荐 32 字节。 +// HMAC-512:推荐 64 字节。 + +// jsonwentoken 默认使用 HMAC-256 算法,生成 32 字节的 token。 + +const v = nanoid.customAlphabet(alphabet, 32); +console.log('v', v()); diff --git a/script/release/index.mjs b/script/release/index.mjs index c260181..c9eee8b 100644 --- a/script/release/index.mjs +++ b/script/release/index.mjs @@ -108,7 +108,9 @@ const getZip = async () => { getZip().then(() => { console.log('zip success'); - console.log(`envision switchOrg system && envision deploy ./release/${zipName} -v 1.0.0 -k code-center -y y -u`); + // 固定上传位置在1.0.0版本,不需要更新,因为是zip文件 + console.log(`envision switch system && envision deploy ./release/${zipName} -v 1.0.0 -k code-center -y y -u`); - console.log(`download zip: https://kevisual.xiongxiao.me/system/code-center/${zipName}`); + console.log(`download zip:\n\ncurl -O https://kevisual.xiongxiao.me/system/code-center/${zipName} && unzip ${zipName}`); + }); diff --git a/src/app.ts b/src/app.ts index 1d347aa..84b6e86 100644 --- a/src/app.ts +++ b/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); - }); -}); diff --git a/src/index.ts b/src/index.ts index ae758b3..fab712e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,3 +15,5 @@ app.listen(config.port, () => { }); app.server.on(uploadMiddleware); // } + +console.log(`run ${config.appName} done`); diff --git a/src/models/user.ts b/src/models/user.ts index d943295..6cf8d6b 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -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 }, diff --git a/src/routes/micro-app/list.ts b/src/routes/micro-app/list.ts index ed50bf2..0a2c73a 100644 --- a/src/routes/micro-app/list.ts +++ b/src/routes/micro-app/list.ts @@ -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); diff --git a/src/routes/user/me.ts b/src/routes/user/me.ts index bd127ea..1bf7a25 100644 --- a/src/routes/user/me.ts +++ b/src/routes/user/me.ts @@ -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, }); diff --git a/src/scripts/sync-user.ts b/src/scripts/sync-user.ts new file mode 100644 index 0000000..cb9a41d --- /dev/null +++ b/src/scripts/sync-user.ts @@ -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); +}); \ No newline at end of file