From 8c7658de0f8c47db34c429e6449a9716f90a38ea Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sat, 3 Feb 2024 15:44:49 +0800 Subject: [PATCH] 1,349th Commit --- api/package.json | 1 + api/src/app.ts | 2 +- api/src/plugins/sleep.ts | 2 +- api/src/server.ts | 2 +- api/src/shims.d.ts | 9 +++++++++ api/vite.config.ts | 9 +++++---- pnpm-lock.yaml | 3 +++ vite.config.ts | 1 + 8 files changed, 22 insertions(+), 7 deletions(-) diff --git a/api/package.json b/api/package.json index 0349c111..f508da6a 100644 --- a/api/package.json +++ b/api/package.json @@ -11,6 +11,7 @@ "fastify-plugin": "^4.5.1" }, "devDependencies": { + "process-envify": "^2.0.0", "vite": "^5.0.12", "vite-plugin-fastify": "^1.2.5", "vite-plugin-fastify-routes": "^1.1.0" diff --git a/api/src/app.ts b/api/src/app.ts index 95b4c8ca..8f2d8b51 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -16,7 +16,7 @@ export default () => { app.register(multipart); app.register(router); - // app.register(sleep); + app.register(sleep); return app; }; diff --git a/api/src/plugins/sleep.ts b/api/src/plugins/sleep.ts index 1fc4cf62..3c81b49e 100644 --- a/api/src/plugins/sleep.ts +++ b/api/src/plugins/sleep.ts @@ -3,7 +3,7 @@ import plugin from 'fastify-plugin'; export default plugin( async (app) => { app.addHook('onRequest', async () => { - await new Promise((resolve) => setTimeout(resolve, 1000)); + await new Promise((resolve) => setTimeout(resolve, 333.33)); }); }, { name: 'sleep' }, diff --git a/api/src/server.ts b/api/src/server.ts index 6395dc5f..a1daf332 100644 --- a/api/src/server.ts +++ b/api/src/server.ts @@ -4,7 +4,7 @@ const server = app(); const start = async () => { try { - await server.listen({ host: '127.0.0.1', port: 3000 }); + await server.listen({ host: process.env.HOST, port: process.env.PORT }); } catch (err) { server.log.error(err); process.exit(1); diff --git a/api/src/shims.d.ts b/api/src/shims.d.ts index 591a6141..50dc749b 100644 --- a/api/src/shims.d.ts +++ b/api/src/shims.d.ts @@ -1 +1,10 @@ /// + +declare namespace NodeJS { + export interface ProcessEnv { + NODE_ENV: string; + + HOST: string; + PORT: number; + } +} diff --git a/api/vite.config.ts b/api/vite.config.ts index 5179ae8c..f39a71d3 100644 --- a/api/vite.config.ts +++ b/api/vite.config.ts @@ -2,12 +2,13 @@ import { resolve } from 'path'; import { defineConfig } from 'vite'; import fastify from 'vite-plugin-fastify'; import fastifyRoutes from 'vite-plugin-fastify-routes'; +import envify from 'process-envify'; export default defineConfig({ - server: { - host: '127.0.0.1', - port: 3000, - }, + define: envify({ + HOST: process.env.HOST || '127.0.0.1', + PORT: process.env.PORT || 3000, + }), plugins: [fastify(), fastifyRoutes()], resolve: { alias: { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 83d33406..791cf299 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -139,6 +139,9 @@ importers: specifier: ^4.5.1 version: 4.5.1 devDependencies: + process-envify: + specifier: ^2.0.0 + version: 2.0.0 vite: specifier: ^5.0.12 version: 5.0.12(sass@1.70.0) diff --git a/vite.config.ts b/vite.config.ts index b249c710..28616650 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -41,6 +41,7 @@ export default defineConfig({ '@': path.resolve(__dirname, 'src'), api: path.resolve(__dirname, 'api/src/routes'), }, + mainFields: ['module'], }, server: { proxy: {