From 6a4ff85683593ff72e6e7647a16d557e4fb12126 Mon Sep 17 00:00:00 2001 From: xion Date: Tue, 25 Feb 2025 10:01:12 +0800 Subject: [PATCH] temp --- app.config.json5 | 10 ++++++---- package.json | 10 ++++++---- src/index.ts | 6 +++--- src/module/config.ts | 33 +++++++++++++++++++++++++++++++++ src/module/get-user-app.ts | 11 +++-------- src/module/index.ts | 19 +++++++------------ src/module/query/get-router.ts | 9 ++------- src/route/app/list.ts | 3 +-- src/scripts/copy.ts | 8 +++----- 9 files changed, 64 insertions(+), 45 deletions(-) create mode 100644 src/module/config.ts diff --git a/app.config.json5 b/app.config.json5 index e04958a..82d4260 100644 --- a/app.config.json5 +++ b/app.config.json5 @@ -1,10 +1,12 @@ { - port: 3005, api: { host: 'localhost:4002', // 后台代理 path: '/api/router', }, - allowedOrigins: ['localhost', 'xiongxiao.me', 'zxj.im'], - domain: 'kevisual.xiongxiao.me', - resources: 'minio.xiongxiao.me/resources', + proxy: { + port: 3005, + domain: 'kevisual.xiongxiao.me', + resources: 'minio.xiongxiao.me/resources', + allowedOrigins: ['localhost', 'xiongxiao.me', 'zxj.im', 'silkyai.cn'], + }, } diff --git a/package.json b/package.json index 84ba9c9..89559e0 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "app": { "key": "page-proxy", "entry": "dist/app.mjs", - "type": "micro-app", + "type": "pm2-system-app", "files": [ "dist" ] @@ -18,12 +18,14 @@ "scripts": { "dev": "cross-env NODE_ENV=development nodemon --ignore upload --exec tsx src/index.ts", "build": "rimraf dist && rollup -c", - "deploy": "rsync -avz dist/ light:~/apps/var-proxy/backend", + "deploy:light": "rsync -avz dist/ light:~/apps/var-proxy/backend", "reload": "ssh light pm2 restart proxy", - "pub": "npm run build && npm run deploy && npm run reload", + "pub:light": "npm run build && npm run deploy && npm run reload", "demo": "rsync -avz dist/ on:~/docker/page-proxy/dist", "start": "pm2 start dist/app.mjs --name page-proxy", - "release": "node ./scripts/release/index.mjs" + "release": "node ./scripts/release/index.mjs", + "deploy": "envision switch root && envision pack -p -u", + "pub": "npm run build && npm run deploy" }, "keywords": [], "author": "", diff --git a/src/index.ts b/src/index.ts index ccd12c3..cd6b442 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ import { handleRequest } from './module/index.ts'; -import { useConfig } from '@kevisual/use-config'; +import { config } from './module/config.ts'; import { app } from './app.ts'; -import './route/route.ts' -const { port } = useConfig<{ port: number }>(); +import './route/route.ts'; +const port = config?.proxy?.port || 3005; app .route({ diff --git a/src/module/config.ts b/src/module/config.ts new file mode 100644 index 0000000..c3ddddf --- /dev/null +++ b/src/module/config.ts @@ -0,0 +1,33 @@ +import { useConfig } from '@kevisual/use-config'; +import { useFileStore } from '@kevisual/use-config/file-store'; +export const fileStore = useFileStore('proxy-upload'); + +type ConfigType = { + api: { + /** + * API host address + */ + host: string; + path?: string; + port?: number; + }; + proxy: { + port?: number; + /** + * self domain kevisual.xiongxiao.me + */ + domain: string; + /** + * resources path + * https://minio.xiongxiao.me/resources + */ + resources: string; + /** + * allow origin xiongxiao.me zxj.im silkyai.cn + * 允许跨域访问的地址 + */ + allowOrigin: string[]; + }; +}; + +export const config = useConfig(); diff --git a/src/module/get-user-app.ts b/src/module/get-user-app.ts index 73e1822..704d90e 100644 --- a/src/module/get-user-app.ts +++ b/src/module/get-user-app.ts @@ -1,7 +1,6 @@ import path from 'path'; import { redis, subscriber } from './redis/redis.ts'; -import { useFileStore } from '@kevisual/use-config/file-store'; -import { useConfig } from '@kevisual/use-config'; +import { config, fileStore } from '../module/config.ts'; import fs from 'fs'; import crypto from 'crypto'; import { nanoid } from 'nanoid'; @@ -10,8 +9,7 @@ import { promisify } from 'util'; import { fetchApp, fetchDomain, fetchTest } from './query/get-router.ts'; const pipelineAsync = promisify(pipeline); -const { resources, api } = useConfig<{ resources: string; api: { host: string; path: string } }>(); -const fileStore = useFileStore('upload'); +const { resources } = config?.proxy || { resources: 'https://minio.xiongxiao.me/resources' }; const status: { [key: string]: boolean } = {}; const demoData = { user: 'root', @@ -255,10 +253,7 @@ export const downloadUserAppFiles = async (user: string, app: string, data: type }; } if (data.type === 'oss') { - let serverPath = 'https://' + resources + '/'; - if(resources.includes('localhost')) { - serverPath = 'http://' + resources + '/'; - } + let serverPath = new URL(resources).href + '/'; // server download file for (let i = 0; i < files.length; i++) { const file = files[i]; diff --git a/src/module/index.ts b/src/module/index.ts index b68b2d9..66a77a9 100644 --- a/src/module/index.ts +++ b/src/module/index.ts @@ -1,22 +1,17 @@ import { getDNS, isLocalhost } from '@/utils/dns.ts'; import http from 'http'; import { UserApp } from './get-user-app.ts'; -import { useFileStore } from '@kevisual/use-config/file-store'; +import { config, fileStore } from '../module/config.ts'; import path from 'path'; import fs from 'fs'; -import { useConfig } from '@kevisual/use-config'; import { getContentType } from './get-content-type.ts'; import { sleep } from '@/utils/sleep.ts'; -const { api, domain, allowedOrigins } = useConfig<{ - api: { - host: string; - port?: number; - }; - domain: string; - allowedOrigins: string[]; -}>(); -const fileStore = useFileStore('upload'); +const api = config?.api || { host: 'kevisual.xiongxiao.me', path: '/api/router' }; +const domain = config?.proxy?.domain || 'kevisual.xiongxiao.me'; +const allowedOrigins = config?.proxy?.allowOrigin || []; + + const noProxyUrl = ['/', '/favicon.ico']; export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => { if (req.url === '/favicon.ico') { @@ -34,7 +29,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR let header: any = {}; if (req.headers?.['Authorization']) { header.authorization = req.headers['Authorization']; - } else if(req.headers?.['authorization']) { + } else if (req.headers?.['authorization']) { header.authorization = req.headers['authorization']; } if (req.headers?.['Content-Type']) { diff --git a/src/module/query/get-router.ts b/src/module/query/get-router.ts index 4e92eca..5b7d7a7 100644 --- a/src/module/query/get-router.ts +++ b/src/module/query/get-router.ts @@ -1,11 +1,6 @@ -import { useConfig } from '@kevisual/use-config'; - -const { resources, api } = useConfig<{ - resources: string; - api: { host: string; path: string }; - ƒ; -}>(); +import { config } from '../config.ts'; +const api = config?.api || { host: 'kevisual.xiongxiao.me', path: '/api/router' }; const apiPath = api.path || '/api/router'; export const fetchTest = async (id: string) => { const fetchUrl = 'http://' + api.host + apiPath; diff --git a/src/route/app/list.ts b/src/route/app/list.ts index 97b673e..5aafae2 100644 --- a/src/route/app/list.ts +++ b/src/route/app/list.ts @@ -2,8 +2,7 @@ import { UserApp } from '@/module/get-user-app.ts'; import { app } from '../../app.ts'; import { redis } from '@/module/redis/redis.ts'; import fs from 'fs'; -import { useFileStore } from '@kevisual/use-config/file-store'; -const fileStore = useFileStore('upload'); +import { fileStore } from '../../module/config.ts'; app .route({ diff --git a/src/scripts/copy.ts b/src/scripts/copy.ts index 0eaaa7f..a34ad92 100644 --- a/src/scripts/copy.ts +++ b/src/scripts/copy.ts @@ -1,8 +1,7 @@ import { UserApp, clearAllUserApp } from '../module/get-user-app.ts'; import { redis } from '../module/redis/redis.ts'; import path from 'path'; -import { useFileStore } from '@kevisual/use-config/file-store'; -const filePath = useFileStore('upload'); +import { config, fileStore } from '../module/config.ts'; const main = async () => { const userApp = new UserApp({ user: 'root', app: 'codeflow' }); @@ -33,7 +32,6 @@ const clearData = async () => { // clearData(); // clearAllUserApp(); - const expireData = async () => { await redis.set('user:app:exist:' + 'codeflow:root', 'value', 'EX', 2); process.exit(0); @@ -45,5 +43,5 @@ const keysData = async () => { const keys = await redis.keys('user:app:exist:*'); console.log('keys', keys); process.exit(0); -} -keysData(); \ No newline at end of file +}; +keysData();