From f3a014c8686fbda96b42436ecf01f09a978f5ba5 Mon Sep 17 00:00:00 2001 From: David Matejka Date: Tue, 7 Sep 2021 16:40:18 +0200 Subject: [PATCH 1/2] admin server: fix 404 error handling --- .../src/http/StaticFileHandler.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/admin-server/src/http/StaticFileHandler.ts b/packages/admin-server/src/http/StaticFileHandler.ts index c6affc2949..66bf2f5486 100644 --- a/packages/admin-server/src/http/StaticFileHandler.ts +++ b/packages/admin-server/src/http/StaticFileHandler.ts @@ -19,17 +19,20 @@ export class StaticFileHandler { const path = url.pathname.includes('.') ? url.pathname : basePath + 'index.html' const contentType = getType(path) ?? 'application/octet-stream' + let content: Buffer try { - const content = await readFile(this.publicDir + path) - res.setHeader('Content-Type', contentType) - if (options.fileProcessor) { - res.end(await options.fileProcessor(path.slice(basePath.length), content, req)) - } else { - res.end(content) - } + content = await readFile(this.publicDir + path) } catch (e) { res.writeHead(404) - res.end() + return res.end() + } + + res.setHeader('Content-Type', contentType) + if (options.fileProcessor) { + const processed = await options.fileProcessor(path.slice(basePath.length), content, req) + res.end(processed) + } else { + res.end(content) } } } From 5c030f7a6598fabc621c766871e2e2bf80a54f3a Mon Sep 17 00:00:00 2001 From: David Matejka Date: Tue, 7 Sep 2021 16:41:10 +0200 Subject: [PATCH 2/2] update and fix admin-server build --- .dockerignore | 1 + docker-compose.yaml | 8 ++++---- packages/admin-server/Dockerfile | 6 ++---- packages/admin-server/package.json | 7 ++++++- packages/admin-server/vite.config.js | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/.dockerignore b/.dockerignore index 3c3629e647..e3227bd0dc 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,2 @@ node_modules +packages/*/dist diff --git a/docker-compose.yaml b/docker-compose.yaml index 559f5eea59..5b9e360f15 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -17,7 +17,7 @@ services: admin_server: image: node:14-alpine - command: '/src/node_modules/.bin/nodemon dist/run-admin.js' + command: 'node dist/server/run-admin.js' working_dir: /src/packages/admin-server volumes: @@ -29,7 +29,7 @@ services: CONTEMBER_API_ENDPOINT: 'http://api:4000' CONTEMBER_LOGIN_TOKEN: '1111111111111111111111111111111111111111' - CONTEMBER_PUBLIC_DIR: '/src/packages/admin-server/dist' + CONTEMBER_PUBLIC_DIR: '/src/packages/admin-server/dist/public' CONTEMBER_S3_ENDPOINT: 'http://s3:9000' CONTEMBER_S3_REGION: '_' @@ -45,7 +45,7 @@ services: admin_server_esbuild: image: node:14-alpine - command: '/src/node_modules/.bin/esbuild src/run-admin.ts --bundle --watch --sourcemap --platform=node --outdir=dist' + command: 'npm run watch:server' working_dir: /src/packages/admin-server volumes: @@ -54,7 +54,7 @@ services: admin_server_vite: image: node:14-alpine - command: '/src/node_modules/.bin/vite build public --config vite.config.js --watch' + command: 'npm run watch:public' working_dir: /src/packages/admin-server volumes: diff --git a/packages/admin-server/Dockerfile b/packages/admin-server/Dockerfile index d3db25eeca..49bd3c790e 100644 --- a/packages/admin-server/Dockerfile +++ b/packages/admin-server/Dockerfile @@ -12,9 +12,7 @@ RUN cd packages/admin-server && \ FROM node:14-alpine WORKDIR /src -COPY --from=builder /src/packages/admin-server/dist/run-admin.js ./ -COPY --from=builder /src/packages/admin-server/dist/index.html ./public/ -COPY --from=builder /src/packages/admin-server/dist/_static ./public/_static/ +COPY --from=builder /src/packages/admin-server/dist/ ./ ENV NODE_ENV "production" ENV CONTEMBER_PORT "4000" @@ -22,4 +20,4 @@ ENV CONTEMBER_PUBLIC_DIR "/src/public" ENV CONTEMBER_S3_ENDPOINT "" ENV CONTEMBER_S3_PREFIX "" -CMD ["node", "./run-admin.js"] +CMD ["node", "./server/run-admin.js"] diff --git a/packages/admin-server/package.json b/packages/admin-server/package.json index 6befd50f03..838a3a4009 100644 --- a/packages/admin-server/package.json +++ b/packages/admin-server/package.json @@ -2,7 +2,12 @@ "name": "@contember/admin-server", "version": "0.6.52", "scripts": { - "build": "vite build public --config vite.config.js && esbuild src/run-admin.ts --bundle --sourcemap --platform=node --outdir=dist", + "build": "pnpm run build:public && pnpm run build:server", + "watch": "pnpm run watch:public && pnpm run watch:server", + "build:public": "vite build public --config vite.config.js", + "watch:public": "vite build public --config vite.config.js --watch", + "build:server": "esbuild src/run-admin.ts --bundle --sourcemap --platform=node --outdir=dist/server", + "watch:server": "esbuild src/run-admin.ts --bundle --sourcemap --platform=node --outdir=dist/server --watch ", "build:api:dev": "exit 0", "build:api:prod": "exit 0" }, diff --git a/packages/admin-server/vite.config.js b/packages/admin-server/vite.config.js index c86051b631..ead5cdfacc 100644 --- a/packages/admin-server/vite.config.js +++ b/packages/admin-server/vite.config.js @@ -8,7 +8,7 @@ export default defineConfig({ root: 'public', build: { assetsDir: '_static', - outDir: resolve(rootDirectory, `packages/admin-server/dist`), + outDir: resolve(rootDirectory, `packages/admin-server/dist/public`), rollupOptions: { input: ['public/index.html', 'public/_panel/index.html'], treeshake: {