From 5164f854846b2a98ab9a7d5ab669b37c1699530b Mon Sep 17 00:00:00 2001 From: chenos Date: Sun, 6 Mar 2022 17:29:20 +0800 Subject: [PATCH] chore: env --- Dockerfile | 21 ++++++++++++++++ docker-compose.yml | 39 +++++++++++++++++++++++++++++ packages/api/src/index.ts | 2 -- packages/app/.umirc.ts | 16 ++++++++++++ packages/app/src/pages/apiClient.ts | 2 +- packages/utils/src/index.ts | 4 ++- packages/utils/src/umiConfig.ts | 36 ++++++++++++++++++++++++++ 7 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 packages/utils/src/umiConfig.ts diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..40434394f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM node:12.20.0-stretch + +WORKDIR /app +# COPY . /app +RUN ls -a + +RUN npm config set registry https://registry.npm.taobao.org +RUN yarn config set registry https://registry.npm.taobao.org + +# RUN npm install +# RUN npm run bootstrap +# RUN npm run build + +# # Install app dependencies +# ENV NPM_CONFIG_LOGLEVEL warn +# RUN yarn install + +# # Show current folder structure in logs +RUN ls -a + +# CMD [ "npm", "run", "serve" ] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..9f8b01280 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,39 @@ +version: "3" +networks: + nocobase: + driver: bridge +services: + adminer: + image: adminer + restart: always + networks: + - nocobase + ports: + - ${ADMINER_PORT}:8080 + postgres: + image: postgres:10 + restart: always + ports: + - "${DB_POSTGRES_PORT}:5432" + networks: + - nocobase + command: postgres -c wal_level=logical + environment: + POSTGRES_USER: ${DB_USER} + POSTGRES_DB: ${DB_DATABASE} + POSTGRES_PASSWORD: ${DB_PASSWORD} + nocobase: + build: + context: . + dockerfile: Dockerfile + networks: + - nocobase + command: [ "yarn", "start" ] + working_dir: /app + env_file: ./.env + volumes: + - ./:/app + expose: + - 8000 + ports: + - "${APP_PORT}:8000" \ No newline at end of file diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index 6670e728d..5be894645 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -65,8 +65,6 @@ if (process.argv.length < 3) { process.argv.push('start', '--port', process.env.API_PORT || 12302); } -console.log(process.argv); - api.parse(process.argv).then(() => { console.log(`${new Date().toLocaleTimeString()} Start-up time: ${(Date.now() - start) / 1000}s`); }); diff --git a/packages/app/.umirc.ts b/packages/app/.umirc.ts index c4e4cbda9..e14479f15 100644 --- a/packages/app/.umirc.ts +++ b/packages/app/.umirc.ts @@ -1,9 +1,25 @@ +import dotenv from 'dotenv'; import { resolve } from 'path'; import { defineConfig } from 'umi'; +import { getUmiConfig } from '../utils/src/umiConfig'; + +dotenv.config({ + path: resolve(__dirname, '../../.env'), +}); process.env.MFSU_AD = 'none'; +const umiConfig = getUmiConfig(); + export default defineConfig({ + define: { + ...umiConfig.define, + }, + // only proxy when using `umi dev` + // if the assets are built, will not proxy + proxy: { + ...umiConfig.proxy, + }, nodeModulesTransform: { type: 'none', }, diff --git a/packages/app/src/pages/apiClient.ts b/packages/app/src/pages/apiClient.ts index eb369b3b8..082319619 100644 --- a/packages/app/src/pages/apiClient.ts +++ b/packages/app/src/pages/apiClient.ts @@ -1,7 +1,7 @@ import { APIClient } from '@nocobase/client'; const apiClient = new APIClient({ - baseURL: `http://localhost:3000/api/`, + baseURL: process.env.API_BASE_URL, }); export default apiClient; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 02f1a11ac..20890e60d 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,5 +1,7 @@ export * from './merge'; export * from './mixin'; export * from './mixin/AsyncEmitter'; -export * from './uid'; export * from './registry'; +export * from './uid'; +export * from './umiConfig'; + diff --git a/packages/utils/src/umiConfig.ts b/packages/utils/src/umiConfig.ts new file mode 100644 index 000000000..d66b957f6 --- /dev/null +++ b/packages/utils/src/umiConfig.ts @@ -0,0 +1,36 @@ +export function getUmiConfig() { + const { API_PORT, API_BASE_URL } = process.env; + const API_BASE_PATH = process.env.API_BASE_PATH || '/api/'; + const PROXY_TARGET_URL = process.env.PROXY_TARGET_URL || `http://127.0.0.1:${API_PORT}`; + const LOCAL_STORAGE_BASE_URL = process.env.LOCAL_STORAGE_BASE_URL || '/uploads'; + + function getLocalStorageProxy() { + if (LOCAL_STORAGE_BASE_URL.startsWith('http')) { + return {}; + } + + return { + [LOCAL_STORAGE_BASE_URL]: { + target: PROXY_TARGET_URL, + changeOrigin: true, + }, + }; + } + + return { + define: { + 'process.env.API_BASE_URL': API_BASE_URL || API_BASE_PATH, + }, + // only proxy when using `umi dev` + // if the assets are built, will not proxy + proxy: { + [API_BASE_PATH]: { + target: PROXY_TARGET_URL, + changeOrigin: true, + pathRewrite: { [`^${API_BASE_PATH}`]: API_BASE_PATH }, + }, + // for local storage + ...getLocalStorageProxy(), + }, + }; +}