From 70f0f56b47f71d7dd62c9c502d553a3c491cf9e2 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 28 Jan 2024 16:40:15 +0800 Subject: [PATCH] chore: improve dev env support add websocket proxy in dev add path diff between dev and prod --- package.json | 4 ++-- src/client/api/socketio.ts | 2 +- src/client/vite.config.ts | 5 +++++ src/server/router/serverStatus.ts | 5 ++++- src/server/utils/env.ts | 1 + 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index fe5a880..28bc184 100644 --- a/package.json +++ b/package.json @@ -5,13 +5,13 @@ "scripts": { "dev": "concurrently --kill-others npm:dev:server npm:dev:web", "dev:web": "cd src/client && pnpm dev", - "dev:server": "pnpm run --filter @tianji/server dev", + "dev:server": "cd src/server && pnpm dev", "start": "cross-env NODE_ENV=production node ./dist/src/server/main.js", "start:docker": "pnpm db:migrate:apply && pnpm db:generate && pnpm start", "test": "vitest", "build": "pnpm build:tracker && pnpm build:server && pnpm build:client && pnpm build:geo", "build:client": "cd src/client && pnpm build", - "build:server": "pnpm run --filter @tianji/server build", + "build:server": "cd src/server && pnpm build", "build:tracker": "ts-node scripts/build-tracker.ts", "build:geo": "ts-node scripts/build-geo.ts", "build:openapi": "ts-node scripts/build-openapi-schema.ts", diff --git a/src/client/api/socketio.ts b/src/client/api/socketio.ts index 8439628..5dab5ab 100644 --- a/src/client/api/socketio.ts +++ b/src/client/api/socketio.ts @@ -4,7 +4,7 @@ import type { SubscribeEventMap, SocketEventMap } from '../../server/ws/shared'; import { create } from 'zustand'; import { useEvent } from '../hooks/useEvent'; import { useEffect, useReducer, useState } from 'react'; -import { useIsLogined, useUserInfo } from '../store/user'; +import { useIsLogined } from '../store/user'; const useSocketStore = create<{ socket: Socket | null; diff --git a/src/client/vite.config.ts b/src/client/vite.config.ts index d196f45..684df50 100644 --- a/src/client/vite.config.ts +++ b/src/client/vite.config.ts @@ -11,6 +11,11 @@ export default defineConfig({ clearScreen: false, server: { proxy: { + '/socket.io': { + target: 'ws://localhost:12345', + changeOrigin: true, + ws: true, + }, '/trpc': { target: 'http://localhost:12345', }, diff --git a/src/server/router/serverStatus.ts b/src/server/router/serverStatus.ts index 9d3c9e4..a96ba5d 100644 --- a/src/server/router/serverStatus.ts +++ b/src/server/router/serverStatus.ts @@ -3,11 +3,14 @@ import { body, header, param, validate } from '../middleware/validate'; import { recordServerStatus } from '../model/serverStatus'; import fs from 'fs-extra'; import path from 'path'; +import { env } from '../utils/env'; export const serverStatusRouter = Router(); const installScript = fs.readFileSync( - path.resolve(process.cwd(), './scripts/install.sh') + env.isProd + ? path.resolve(process.cwd(), './scripts/install.sh') + : path.resolve(process.cwd(), '../../scripts/install.sh') ); serverStatusRouter.post( diff --git a/src/server/utils/env.ts b/src/server/utils/env.ts index e76c33e..f5d8cb9 100644 --- a/src/server/utils/env.ts +++ b/src/server/utils/env.ts @@ -1,4 +1,5 @@ export const env = { + isProd: process.env.NODE_ENV === 'production', port: Number(process.env.PORT || 12345), allowRegister: checkEnvTrusty(process.env.ALLOW_REGISTER), allowOpenapi: checkEnvTrusty(process.env.ALLOW_OPENAPI),