chore: resolve shared import problem in production

move everything into packages
This commit is contained in:
moonrailgun 2024-01-29 01:00:35 +08:00
parent d912c788c5
commit 486587f232
23 changed files with 75 additions and 52 deletions

View File

@ -17,7 +17,7 @@ RUN pnpm build
RUN pip install apprise --break-system-packages RUN pip install apprise --break-system-packages
# remove unused source file # remove unused source file
RUN rm -rf ./src RUN rm -rf ./src/client
RUN rm -rf ./website RUN rm -rf ./website
RUN rm -rf ./reporter RUN rm -rf ./reporter

View File

@ -6,8 +6,9 @@
"dev": "concurrently --kill-others npm:dev:server npm:dev:web", "dev": "concurrently --kill-others npm:dev:server npm:dev:web",
"dev:web": "cd src/client && pnpm dev", "dev:web": "cd src/client && pnpm dev",
"dev:server": "cd src/server && pnpm dev", "dev:server": "cd src/server && pnpm dev",
"start": "cross-env NODE_ENV=production node ./dist/src/server/main.js", "start": "cd src/server && cross-env NODE_ENV=production node ./dist/src/server/main.js",
"start:docker": "pnpm db:migrate:apply && pnpm db:generate && pnpm start", "start:docker": "pnpm start:docker:db && pnpm start",
"start:docker:db": "cd src/server && pnpm db:migrate:apply",
"test": "vitest", "test": "vitest",
"build": "pnpm build:tracker && pnpm build:server && pnpm build:client && pnpm build:geo", "build": "pnpm build:tracker && pnpm build:server && pnpm build:client && pnpm build:geo",
"build:client": "cd src/client && pnpm build", "build:client": "cd src/client && pnpm build",

View File

@ -7,7 +7,10 @@ import { keyBy } from 'lodash-es';
interface StatusPageServicesProps { interface StatusPageServicesProps {
workspaceId: string; workspaceId: string;
monitorList: PrismaJson.MonitorStatusPageList; monitorList: {
id: string;
showCurrent?: boolean;
}[];
} }
export const StatusPageServices: React.FC<StatusPageServicesProps> = React.memo( export const StatusPageServices: React.FC<StatusPageServicesProps> = React.memo(
(props) => { (props) => {

View File

@ -6,7 +6,7 @@ export default defineConfig({
root: __dirname, root: __dirname,
plugins: [react()], plugins: [react()],
build: { build: {
outDir: '../../dist/src/server/public', outDir: '../server/public',
}, },
clearScreen: false, clearScreen: false,
server: { server: {

View File

@ -25,6 +25,7 @@ import { initCronjob } from './cronjob';
import { logger } from './utils/logger'; import { logger } from './utils/logger';
import { monitorRouter } from './router/monitor'; import { monitorRouter } from './router/monitor';
import { healthRouter } from './router/health'; import { healthRouter } from './router/health';
import path from 'path';
const port = env.port; const port = env.port;
@ -44,7 +45,7 @@ app.use(express.json());
app.use(passport.initialize()); app.use(passport.initialize());
app.use(morgan('tiny')); app.use(morgan('tiny'));
app.use(cors()); app.use(cors());
app.use(express.static('dist')); app.use(express.static('public'));
// http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header // http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header
app.disable('x-powered-by'); app.disable('x-powered-by');
@ -71,6 +72,13 @@ if (env.allowOpenapi) {
app.use('/open', trpcOpenapiHttpHandler); app.use('/open', trpcOpenapiHttpHandler);
} }
// fallback
app.get('/*', (req, res) => {
if (req.accepts('html')) {
res.sendFile(path.join(process.cwd(), 'public', 'index.html'));
}
});
app.use((err: any, req: any, res: any, next: any) => { app.use((err: any, req: any, res: any, next: any) => {
logger.error(err); logger.error(err);
res.status(500).json({ message: err.message }); res.status(500).json({ message: err.message });

View File

@ -1,5 +1,4 @@
import { z } from 'zod'; import { z } from 'zod';
import { schemas } from '../../../types';
// Match prisma `JsonValue` // Match prisma `JsonValue`
export const jsonFieldSchema = z.union([ export const jsonFieldSchema = z.union([

View File

@ -0,0 +1,12 @@
import { MonitorInfo } from '../../../types';
import { MonitorModelSchema } from '../../prisma/zod';
export type MonitorInfoWithNotificationIds = MonitorInfo & {
notifications: { id: string }[];
};
export const MonitorPublicInfoSchema = MonitorModelSchema.pick({
id: true,
name: true,
type: true,
});

View File

@ -1,5 +1,5 @@
import { MonitorPublicInfoSchema } from '../../../types';
import { prisma } from '../_client'; import { prisma } from '../_client';
import { MonitorPublicInfoSchema } from '../_schema/monitor';
import { MonitorManager } from './manager'; import { MonitorManager } from './manager';
export const monitorManager = new MonitorManager(); export const monitorManager = new MonitorManager();

View File

@ -3,14 +3,11 @@ import { body, header, param, validate } from '../middleware/validate';
import { recordServerStatus } from '../model/serverStatus'; import { recordServerStatus } from '../model/serverStatus';
import fs from 'fs-extra'; import fs from 'fs-extra';
import path from 'path'; import path from 'path';
import { env } from '../utils/env';
export const serverStatusRouter = Router(); export const serverStatusRouter = Router();
const installScript = fs.readFileSync( const installScript = fs.readFileSync(
env.isProd path.resolve(process.cwd(), '../../scripts/install.sh')
? path.resolve(process.cwd(), './scripts/install.sh')
: path.resolve(process.cwd(), '../../scripts/install.sh')
); );
serverStatusRouter.post( serverStatusRouter.post(

View File

@ -13,10 +13,6 @@ import {
getMonitorRecentData, getMonitorRecentData,
monitorManager, monitorManager,
} from '../../model/monitor'; } from '../../model/monitor';
import {
MonitorInfoWithNotificationIds,
MonitorPublicInfoSchema,
} from '../../../types';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { import {
monitorEventSchema, monitorEventSchema,
@ -29,6 +25,10 @@ import { OpenApiMeta } from 'trpc-openapi';
import { MonitorStatusPageModelSchema } from '../../prisma/zod'; import { MonitorStatusPageModelSchema } from '../../prisma/zod';
import { runCodeInVM } from '../../model/monitor/provider/custom'; import { runCodeInVM } from '../../model/monitor/provider/custom';
import { createAuditLog } from '../../model/auditLog'; import { createAuditLog } from '../../model/auditLog';
import {
MonitorInfoWithNotificationIds,
MonitorPublicInfoSchema,
} from '../../model/_schema/monitor';
export const monitorRouter = router({ export const monitorRouter = router({
all: workspaceProcedure all: workspaceProcedure

View File

@ -4,7 +4,7 @@
"exclude": ["node_modules/**/*", "dist"], "exclude": ["node_modules/**/*", "dist"],
"compilerOptions": { "compilerOptions": {
"rootDirs": ["./", "../"], "rootDirs": ["./", "../"],
"outDir": "../../dist", "outDir": "./dist",
"noEmit": false "noEmit": false
} }
} }

View File

@ -1,7 +1,17 @@
import type { JWTPayload } from '../middleware/auth'; import type { JWTPayload } from '../middleware/auth';
import type { MonitorStatusPageListSchema } from '../prisma/zod/schemas';
declare global { declare global {
namespace Express { namespace Express {
interface User extends JWTPayload {} interface User extends JWTPayload {}
} }
namespace PrismaJson {
type CommonPayload = Record<string, any>;
type DashboardLayout = {
layouts: Record<string, any[]>;
items: any[];
} | null;
type MonitorStatusPageList = z.infer<typeof MonitorStatusPageListSchema>;
}
} }

View File

@ -1,3 +0,0 @@
import packageJson from '../../package.json';
export const version = packageJson.version;

View File

@ -3,8 +3,17 @@
"version": "1.0.0", "version": "1.0.0",
"private": true, "private": true,
"description": "", "description": "",
"main": "index.ts", "types": "./dist/cjs/src/shared/src/index.d.ts",
"scripts": {}, "exports": {
"import": "./dist/esm/src/shared/src/index.js",
"require": "./dist/cjs/src/shared/src/index.js"
},
"scripts": {
"build": "pnpm build:cjs && pnpm build:esm",
"build:cjs": "tsc --module commonjs --outDir ./dist/cjs",
"build:esm": "tsc --module esnext --outDir ./dist/esm",
"postinstall": "pnpm build"
},
"keywords": [], "keywords": [],
"author": "moonrailgun <moonrailgun@gmail.com>", "author": "moonrailgun <moonrailgun@gmail.com>",
"dependencies": { "dependencies": {

3
src/shared/src/config.ts Normal file
View File

@ -0,0 +1,3 @@
import packageJson from '../../../package.json';
export const version = packageJson.version;

View File

@ -1,4 +1,4 @@
import { ServerStatusInfo } from '../types'; import type { ServerStatusInfo } from '../../types';
export function isServerOnline(info: ServerStatusInfo): boolean { export function isServerOnline(info: ServerStatusInfo): boolean {
return new Date(info.updatedAt).valueOf() + info.timeout * 1000 > Date.now(); return new Date(info.updatedAt).valueOf() + info.timeout * 1000 > Date.now();

10
src/shared/tsconfig.json Normal file
View File

@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"declaration": true,
"declarationMap": true,
"noEmit": false
},
"include": ["./src/**/*"]
}

View File

@ -1,18 +1,3 @@
import { z } from 'zod'; export type * from './server';
import { MonitorStatusPageListSchema } from '../server/prisma/zod/schemas'; export type * from './monitor';
export type * from './utils';
export * as schemas from '../server/prisma/zod/index';
export * from './server';
export * from './monitor';
export * from './utils';
declare global {
namespace PrismaJson {
type CommonPayload = Record<string, any>;
type DashboardLayout = {
layouts: Record<string, any[]>;
items: any[];
} | null;
type MonitorStatusPageList = z.infer<typeof MonitorStatusPageListSchema>;
}
}

View File

@ -1,6 +1,5 @@
import type { MonitorModelSchema } from '../server/prisma/zod'; import type { MonitorModelSchema } from '../server/prisma/zod';
import { ExactType } from './utils'; import { ExactType } from './utils';
import { schemas } from '.';
import z from 'zod'; import z from 'zod';
type Monitor = z.infer<typeof MonitorModelSchema>; type Monitor = z.infer<typeof MonitorModelSchema>;
@ -11,13 +10,3 @@ export type MonitorInfo = ExactType<
payload: Record<string, any>; payload: Record<string, any>;
} }
>; >;
export type MonitorInfoWithNotificationIds = MonitorInfo & {
notifications: { id: string }[];
};
export const MonitorPublicInfoSchema = schemas.MonitorModelSchema.pick({
id: true,
name: true,
type: true,
});