From cf5f1b6f254f34d32337e3035c21f71d4d29c521 Mon Sep 17 00:00:00 2001 From: whyour Date: Sat, 19 Feb 2022 13:08:14 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=95=B0=E6=8D=AE=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_docker_image.yml | 6 +- .gitignore | 4 +- .umirc.ts | 1 + README.md | 7 +-- back/api/system.ts | 6 +- back/config/index.ts | 39 ++++++------ back/loaders/db.ts | 75 ++---------------------- back/loaders/express.ts | 9 +-- back/loaders/initFile.ts | 16 ++--- docker-compose.yml | 7 +-- docker/docker-entrypoint.sh | 4 +- docker/front.conf | 2 +- package.json | 6 +- shell/check.sh | 4 +- shell/notify.js | 2 +- shell/notify.sh | 2 +- shell/share.sh | 16 ++--- shell/update.sh | 10 ++-- src/pages/crontab/index.tsx | 4 +- tsconfig.back.json | 2 +- tsconfig.json | 3 +- 21 files changed, 75 insertions(+), 150 deletions(-) diff --git a/.github/workflows/build_docker_image.yml b/.github/workflows/build_docker_image.yml index 4a2b2777..4ff157ea 100644 --- a/.github/workflows/build_docker_image.yml +++ b/.github/workflows/build_docker_image.yml @@ -36,9 +36,9 @@ jobs: GITHUB_REPO: github.com/${{ github.repository_owner }}/qinglong-static GITHUB_BRANCH: ${{ github.ref_name }} run: | - mkdir -p static - cd ./static - cp -rf ../dist ./ && cp -rf ../build ./ + mkdir -p tmp + cd ./tmp + cp -rf ../static ./ git init -b ${GITHUB_BRANCH} && git add . git config --local user.name 'github-actions[bot]' git config --local user.email 'github-actions[bot]@users.noreply.github.com' diff --git a/.gitignore b/.gitignore index ca3bd885..cb4233d3 100644 --- a/.gitignore +++ b/.gitignore @@ -8,8 +8,8 @@ /package-lock.json # production -/dist -/build +/static +/data # misc .DS_Store diff --git a/.umirc.ts b/.umirc.ts index fff008f9..e9b630bc 100644 --- a/.umirc.ts +++ b/.umirc.ts @@ -4,6 +4,7 @@ const CompressionPlugin = require('compression-webpack-plugin'); export default defineConfig({ hash: true, layout: false, + outputPath: 'static/dist', nodeModulesTransform: { type: 'none', }, diff --git a/README.md b/README.md index b6400fb2..62417625 100644 --- a/README.md +++ b/README.md @@ -56,12 +56,7 @@ sudo curl -sSL get.docker.com | sh ```bash docker run -dit \ - -v $PWD/ql/config:/ql/config \ - -v $PWD/ql/log:/ql/log \ - -v $PWD/ql/db:/ql/db \ - -v $PWD/ql/repo:/ql/repo \ - -v $PWD/ql/raw:/ql/raw \ - -v $PWD/ql/scripts:/ql/scripts \ + -v $PWD/ql:/ql/data \ -p 5700:5700 \ --name qinglong \ --hostname qinglong \ diff --git a/back/api/system.ts b/back/api/system.ts index 8aba494e..8de86351 100644 --- a/back/api/system.ts +++ b/back/api/system.ts @@ -5,8 +5,8 @@ import * as fs from 'fs'; import config from '../config'; import SystemService from '../services/system'; import { celebrate, Joi } from 'celebrate'; -import { getFileContentByName } from '../config/util'; import UserService from '../services/user'; +import { EnvModel } from '../data/env'; const route = Router(); export default (app: Router) => { @@ -17,7 +17,7 @@ export default (app: Router) => { try { const userService = Container.get(UserService); const authInfo = await userService.getUserInfo(); - const envDbContent = getFileContentByName(config.envDbFile); + const envCount = await EnvModel.count(); const versionRegx = /.*export const version = \'(.*)\'\;/; const currentVersionFile = fs.readFileSync(config.versionFile, 'utf8'); @@ -28,7 +28,7 @@ export default (app: Router) => { Object.keys(authInfo).length === 2 && authInfo.username === 'admin' && authInfo.password === 'admin' && - envDbContent.length === 0 + envCount === 0 ) { isInitialized = false; } diff --git a/back/config/index.ts b/back/config/index.ts index 3563f5d3..d524e588 100644 --- a/back/config/index.ts +++ b/back/config/index.ts @@ -9,27 +9,26 @@ const lastVersionFile = const envFound = dotenv.config(); const rootPath = process.cwd(); -const envFile = path.join(rootPath, 'config/env.sh'); -const confFile = path.join(rootPath, 'config/config.sh'); -const sampleFile = path.join(rootPath, 'sample/config.sample.sh'); -const crontabFile = path.join(rootPath, 'config/crontab.list'); -const confBakDir = path.join(rootPath, 'config/bak/'); -const authConfigFile = path.join(rootPath, 'config/auth.json'); -const extraFile = path.join(rootPath, 'config/extra.sh'); -const configPath = path.join(rootPath, 'config/'); -const scriptPath = path.join(rootPath, 'scripts/'); -const bakPath = path.join(rootPath, 'bak/'); +const dataPath = path.join(rootPath, 'data/'); const samplePath = path.join(rootPath, 'sample/'); -const logPath = path.join(rootPath, 'log/'); +const configPath = path.join(dataPath, 'config/'); +const scriptPath = path.join(dataPath, 'scripts/'); +const bakPath = path.join(dataPath, 'bak/'); +const logPath = path.join(dataPath, 'log/'); +const dbPath = path.join(dataPath, 'db/'); + +const envFile = path.join(configPath, 'env.sh'); +const confFile = path.join(configPath, 'config.sh'); +const crontabFile = path.join(configPath, 'crontab.list'); +const authConfigFile = path.join(configPath, 'auth.json'); +const extraFile = path.join(configPath, 'extra.sh'); +const confBakDir = path.join(dataPath, 'config/bak/'); +const sampleFile = path.join(samplePath, 'config.sample.sh'); +const sqliteFile = path.join(samplePath, 'database.sqlite'); + const authError = '错误的用户名密码,请重试'; const loginFaild = '请先登录!'; const configString = 'config sample crontab shareCode diy'; -const dbPath = path.join(rootPath, 'db/'); -const cronDbFile = path.join(rootPath, 'db/crontab.db'); -const envDbFile = path.join(rootPath, 'db/env.db'); -const appDbFile = path.join(rootPath, 'db/app.db'); -const authDbFile = path.join(rootPath, 'db/auth.db'); -const dependenceDbFile = path.join(rootPath, 'db/dependence.db'); const versionFile = path.join(rootPath, 'src/version.ts'); if (envFound.error) { @@ -59,11 +58,6 @@ export default { confFile, envFile, dbPath, - cronDbFile, - envDbFile, - appDbFile, - authDbFile, - dependenceDbFile, configPath, scriptPath, samplePath, @@ -86,4 +80,5 @@ export default { ], versionFile, lastVersionFile, + sqliteFile, }; diff --git a/back/loaders/db.ts b/back/loaders/db.ts index be93c8ac..e1b8ca14 100644 --- a/back/loaders/db.ts +++ b/back/loaders/db.ts @@ -1,7 +1,4 @@ -import DataStore from 'nedb'; -import config from '../config'; import Logger from './logger'; -import { fileExist } from '../config/util'; import { EnvModel } from '../data/env'; import { CrontabModel } from '../data/cron'; import { DependenceModel } from '../data/dependence'; @@ -12,7 +9,11 @@ import { sequelize } from '../data'; export default async () => { try { await sequelize.sync(); - await new Promise((resolve) => setTimeout(() => resolve(null), 5000)); + await CrontabModel.sync(); + await DependenceModel.sync(); + await AppModel.sync(); + await AuthModel.sync(); + await EnvModel.sync(); // try { // const queryInterface = sequelize.getQueryInterface(); @@ -23,72 +24,6 @@ export default async () => { // } - const crondbExist = await fileExist(config.cronDbFile); - const dependenceDbExist = await fileExist(config.dependenceDbFile); - const envDbExist = await fileExist(config.envDbFile); - const appDbExist = await fileExist(config.appDbFile); - const authDbExist = await fileExist(config.authDbFile); - - const cronCount = await CrontabModel.count(); - const dependenceCount = await DependenceModel.count(); - const envCount = await EnvModel.count(); - const appCount = await AppModel.count(); - const authCount = await AuthModel.count(); - if (crondbExist && cronCount === 0) { - const cronDb = new DataStore({ - filename: config.cronDbFile, - autoload: true, - }); - cronDb.persistence.compactDatafile(); - cronDb.find({}).exec(async (err, docs) => { - await CrontabModel.bulkCreate(docs, { ignoreDuplicates: true }); - }); - } - - if (dependenceDbExist && dependenceCount === 0) { - const dependenceDb = new DataStore({ - filename: config.dependenceDbFile, - autoload: true, - }); - dependenceDb.persistence.compactDatafile(); - dependenceDb.find({}).exec(async (err, docs) => { - await DependenceModel.bulkCreate(docs, { ignoreDuplicates: true }); - }); - } - - if (envDbExist && envCount === 0) { - const envDb = new DataStore({ - filename: config.envDbFile, - autoload: true, - }); - envDb.persistence.compactDatafile(); - envDb.find({}).exec(async (err, docs) => { - await EnvModel.bulkCreate(docs, { ignoreDuplicates: true }); - }); - } - - if (appDbExist && appCount === 0) { - const appDb = new DataStore({ - filename: config.appDbFile, - autoload: true, - }); - appDb.persistence.compactDatafile(); - appDb.find({}).exec(async (err, docs) => { - await AppModel.bulkCreate(docs, { ignoreDuplicates: true }); - }); - } - - if (authDbExist && authCount === 0) { - const authDb = new DataStore({ - filename: config.authDbFile, - autoload: true, - }); - authDb.persistence.compactDatafile(); - authDb.find({}).exec(async (err, docs) => { - await AuthModel.bulkCreate(docs, { ignoreDuplicates: true }); - }); - } - Logger.info('✌️ DB loaded'); } catch (error) { Logger.info('✌️ DB load failed'); diff --git a/back/loaders/express.ts b/back/loaders/express.ts index 3dec5034..8482de51 100644 --- a/back/loaders/express.ts +++ b/back/loaders/express.ts @@ -5,13 +5,14 @@ import routes from '../api'; import config from '../config'; import jwt from 'express-jwt'; import fs from 'fs'; -import { getFileContentByName, getPlatform, getToken } from '../config/util'; +import { getPlatform, getToken } from '../config/util'; import Container from 'typedi'; import OpenService from '../services/open'; import rewrite from 'express-urlrewrite'; import UserService from '../services/user'; import handler from 'serve-handler'; import * as Sentry from '@sentry/node'; +import { EnvModel } from '../data/env'; export default ({ app }: { app: Application }) => { app.enable('trust proxy'); @@ -22,7 +23,7 @@ export default ({ app }: { app: Application }) => { next(); } else { return handler(req, res, { - public: 'dist', + public: 'static/dist', rewrites: [{ source: '**', destination: '/index.html' }], }); } @@ -104,14 +105,14 @@ export default ({ app }: { app: Application }) => { } const userService = Container.get(UserService); const authInfo = await userService.getUserInfo(); - const envDbContent = getFileContentByName(config.envDbFile); + const envCount = await EnvModel.count(); let isInitialized = true; if ( Object.keys(authInfo).length === 2 && authInfo.username === 'admin' && authInfo.password === 'admin' && - envDbContent.length === 0 + envCount === 0 ) { isInitialized = false; } diff --git a/back/loaders/initFile.ts b/back/loaders/initFile.ts index 09bddf13..5b37de87 100644 --- a/back/loaders/initFile.ts +++ b/back/loaders/initFile.ts @@ -5,13 +5,15 @@ import Logger from './logger'; import { fileExist } from '../config/util'; const rootPath = process.cwd(); -const confFile = path.join(rootPath, 'config/config.sh'); -const sampleConfigFile = path.join(rootPath, 'sample/config.sample.sh'); -const sampleAuthFile = path.join(rootPath, 'sample/auth.sample.json'); -const authConfigFile = path.join(rootPath, 'config/auth.json'); -const configPath = path.join(rootPath, 'config/'); -const scriptPath = path.join(rootPath, 'scripts/'); -const logPath = path.join(rootPath, 'log/'); +const dataPath = path.join(rootPath, 'data/'); +const configPath = path.join(dataPath, 'config/'); +const scriptPath = path.join(dataPath, 'scripts/'); +const logPath = path.join(dataPath, 'log/'); +const samplePath = path.join(rootPath, 'sample/'); +const confFile = path.join(configPath, 'config.sh'); +const authConfigFile = path.join(configPath, 'auth.json'); +const sampleConfigFile = path.join(samplePath, 'config.sample.sh'); +const sampleAuthFile = path.join(samplePath, 'auth.sample.json'); export default async () => { const authFileExist = await fileExist(authConfigFile); diff --git a/docker-compose.yml b/docker-compose.yml index e5440dba..8e2b08a6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,12 +3,7 @@ services: web: image: whyour/qinglong:latest volumes: - - ./data/config:/ql/config - - ./data/log:/ql/log - - ./data/db:/ql/db - - ./data/scripts:/ql/scripts - - ./data/repo:/ql/repo - - ./data/raw:/ql/raw + - ./data:/ql/data ports: - "0.0.0.0:5700:5700" restart: unless-stopped diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index db3b7474..e852e637 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -22,12 +22,12 @@ echo -e "nginx启动成功...\n" echo -e "======================4. 启动控制面板========================\n" pm2 delete panel &>/dev/null -pm2 start $dir_root/build/app.js -n panel --source-map-support --time +pm2 start $dir_static/build/app.js -n panel --source-map-support --time echo -e "控制面板启动成功...\n" echo -e "======================5. 启动定时任务========================\n" pm2 delete schedule &>/dev/null -pm2 start $dir_root/build/schedule.js -n schedule --source-map-support --time +pm2 start $dir_static/build/schedule.js -n schedule --source-map-support --time echo -e "定时任务启动成功...\n" if [[ $AutoStartBot == true ]]; then diff --git a/docker/front.conf b/docker/front.conf index 7456e7fa..ee894c21 100644 --- a/docker/front.conf +++ b/docker/front.conf @@ -9,7 +9,7 @@ map $http_upgrade $connection_upgrade { server { listen 5700; - root /ql/dist; + root /ql/static/dist; ssl_session_timeout 5m; location /api { diff --git a/package.json b/package.json index f4289b2b..d3748b94 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "start:back": "nodemon", "build:front": "umi build", "build:back": "tsc -p tsconfig.back.json", - "panel": "npm run build:back && node build/app.js", - "schedule": "npm run build:back && node build/schedule.js", + "panel": "npm run build:back && node static/build/app.js", + "schedule": "npm run build:back && node static/build/schedule.js", "prettier": "prettier --write '**/*.{js,jsx,tsx,ts,less,md,json}'", "prepare": "umi generate tmp", "test": "umi-test", @@ -42,7 +42,6 @@ "iconv-lite": "^0.6.3", "jsonwebtoken": "^8.5.1", "lodash": "^4.17.21", - "nedb": "^1.8.0", "node-fetch": "^2.6.1", "node-schedule": "^2.0.0", "nodemailer": "^6.7.0", @@ -67,7 +66,6 @@ "@types/express-jwt": "^6.0.1", "@types/jsonwebtoken": "^8.5.0", "@types/lodash": "^4.14.170", - "@types/nedb": "^1.8.11", "@types/node": "^14.11.2", "@types/node-fetch": "^2.5.8", "@types/node-schedule": "^1.3.2", diff --git a/shell/check.sh b/shell/check.sh index 6d1e57d7..be1cbc7f 100644 --- a/shell/check.sh +++ b/shell/check.sh @@ -38,10 +38,10 @@ reload_pm2() { pm2 l &>/dev/null pm2 delete panel --source-map-support --time &>/dev/null - pm2 start $dir_root/build/app.js -n panel --source-map-support --time &>/dev/null + pm2 start $dir_static/build/app.js -n panel --source-map-support --time &>/dev/null pm2 delete schedule --source-map-support --time &>/dev/null - pm2 start $dir_root/build/schedule.js -n schedule --source-map-support --time &>/dev/null + pm2 start $dir_static/build/schedule.js -n schedule --source-map-support --time &>/dev/null } pm2_log() { diff --git a/shell/notify.js b/shell/notify.js index b70fe673..92bce2b4 100644 --- a/shell/notify.js +++ b/shell/notify.js @@ -1,4 +1,4 @@ -const notify = require('/ql/scripts/sendNotify.js'); +const notify = require('/ql/data/scripts/sendNotify.js'); const title = process.argv[2]; const content = process.argv[3]; diff --git a/shell/notify.sh b/shell/notify.sh index ae669f70..e066858b 100755 --- a/shell/notify.sh +++ b/shell/notify.sh @@ -1,7 +1,7 @@ #!/bin/bash #author:spark thanks to: https://github.com/sparkssssssss/scripts -. /ql/config/config.sh +. /ql/data/config/config.sh title=$(echo $1|sed 's/-/_/g') msg=$(echo -e $2) diff --git a/shell/share.sh b/shell/share.sh index ca6b11af..63f975e2 100755 --- a/shell/share.sh +++ b/shell/share.sh @@ -2,15 +2,17 @@ ## 目录 dir_root=/ql +dir_data=$dir_root/data dir_shell=$dir_root/shell dir_sample=$dir_root/sample -dir_config=$dir_root/config -dir_scripts=$dir_root/scripts -dir_repo=$dir_root/repo -dir_raw=$dir_root/raw -dir_log=$dir_root/log -dir_db=$dir_root/db -dir_dep=$dir_root/deps +dir_static=$dir_root/static +dir_config=$dir_data/config +dir_scripts=$dir_data/scripts +dir_repo=$dir_data/repo +dir_raw=$dir_data/raw +dir_log=$dir_data/log +dir_db=$dir_data/db +dir_dep=$dir_data/deps dir_list_tmp=$dir_log/.tmp dir_code=$dir_log/code dir_update_log=$dir_log/update diff --git a/shell/update.sh b/shell/update.sh index 3fd39f42..47f0afa6 100755 --- a/shell/update.sh +++ b/shell/update.sh @@ -271,9 +271,9 @@ update_qinglong() { echo -e "\n更新$ql_static_repo成功...\n" local static_version=$(cat /ql/src/version.ts | perl -pe "s|.*\'(.*)\';\.*|\1|" | head -1) echo -e "\n当前版本 $static_version...\n" - cd $dir_root - rm -rf $dir_root/build && rm -rf $dir_root/dist - cp -rf $ql_static_repo/* $dir_root + + rm -rf $dir_static + cp -rf $ql_static_repo/* $dir_static if [[ $no_restart != "no-restart" ]]; then nginx -s reload 2>/dev/null || nginx -c /etc/nginx/nginx.conf echo -e "重启面板中..." @@ -307,10 +307,10 @@ reload_pm2() { pm2 l &>/dev/null pm2 delete panel --source-map-support --time &>/dev/null - pm2 start $dir_root/build/app.js -n panel --source-map-support --time &>/dev/null + pm2 start $dir_static/build/app.js -n panel --source-map-support --time &>/dev/null pm2 delete schedule --source-map-support --time &>/dev/null - pm2 start $dir_root/build/schedule.js -n schedule --source-map-support --time &>/dev/null + pm2 start $dir_static/build/schedule.js -n schedule --source-map-support --time &>/dev/null } ## 对比脚本 diff --git a/src/pages/crontab/index.tsx b/src/pages/crontab/index.tsx index 75c2e149..428f4570 100644 --- a/src/pages/crontab/index.tsx +++ b/src/pages/crontab/index.tsx @@ -367,8 +367,8 @@ const Crontab = ({ headerStyle, isPhone }: any) => { const goToScriptManager = (record: any) => { const cmd = record.command.split(' ') as string[]; if (cmd[0] === 'task') { - if (cmd[1].startsWith('/ql/scripts')) { - cmd[1] = cmd[1].replace('/ql/scripts/', ''); + if (cmd[1].startsWith('/ql/data/scripts')) { + cmd[1] = cmd[1].replace('/ql/data/scripts/', ''); } let [p, s] = cmd[1].split('/'); diff --git a/tsconfig.back.json b/tsconfig.back.json index 4fa3876c..c977de1f 100644 --- a/tsconfig.back.json +++ b/tsconfig.back.json @@ -15,7 +15,7 @@ "module": "commonjs", "pretty": true, "sourceMap": true, - "outDir": "./build", + "outDir": "./static/build", "allowJs": true, "noEmit": false, "esModuleInterop": true diff --git a/tsconfig.json b/tsconfig.json index 064f9f70..06ab0637 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -32,7 +32,8 @@ "node_modules", "lib", "es", - "dist", + "static", + "data", "typings", "**/__test__", "test",