Commit b649bc12 authored by 王曜嵚 Wang Yaoqin's avatar 王曜嵚 Wang Yaoqin

dev: 环境变量

parent 75221ace
...@@ -112,7 +112,7 @@ async function handleInstall() { ...@@ -112,7 +112,7 @@ async function handleInstall() {
execute() execute()
} }
function getTip (status: string) { function getTip (status: string) {
const HOST = process.env.NODE_ENV === 'production' ? '192.168.0.4' : (process.env.DEVHOST || '192.168.0.190') const HOST = useRuntimeConfig().public.dockerHost
const port = useUserStore().port const port = useUserStore().port
const map = new Map([ const map = new Map([
['null', '请点击右侧按钮完成项目初始化'], ['null', '请点击右侧按钮完成项目初始化'],
......
...@@ -141,7 +141,7 @@ async function handleInstall() { ...@@ -141,7 +141,7 @@ async function handleInstall() {
} }
function getTip (status: string) { function getTip (status: string) {
const HOST = process.env.NODE_ENV === 'production' ? '192.168.0.4' : (process.env.DEVHOST || '192.168.0.190') const HOST = useRuntimeConfig().public.dockerHost
const port = useUserStore().port const port = useUserStore().port
const map = new Map([ const map = new Map([
['null', '请点击右侧按钮完成项目初始化'], ['null', '请点击右侧按钮完成项目初始化'],
......
...@@ -8,5 +8,15 @@ export default defineNuxtConfig({ ...@@ -8,5 +8,15 @@ export default defineNuxtConfig({
experimental: { experimental: {
websocket: true websocket: true
} }
},
runtimeConfig: {
pgUser: '',
pgPort: '',
pgHost: '',
pgPassword: '',
pgDatabase: '',
public: {
dockerHost: ''
}
} }
}) })
...@@ -12,7 +12,7 @@ export default defineEventHandler(async (event) => { ...@@ -12,7 +12,7 @@ export default defineEventHandler(async (event) => {
LogUtil.print(username, `[progress] [[1;34mInfo[m] 打包微信小程序zip \n`) LogUtil.print(username, `[progress] [[1;34mInfo[m] 打包微信小程序zip \n`)
await docker.execContainerCommand({ container, cmd: 'rm -f weapp.zip', dir: '/var/logwire-backend/build-output/backend' }) await docker.execContainerCommand({ container, cmd: 'rm -f weapp.zip', dir: '/var/logwire-backend/build-output/backend' })
await docker.execContainerCommand({ container, cmd: `zip -q -r weapp.zip miniapp/main/dist miniapp/main/src/pages miniapp/main/src/app.config.ts`, dir: '/var/logwire-backend/build-output/backend' }) await docker.execContainerCommand({ container, cmd: `zip -q -r weapp.zip miniapp/main/dist miniapp/main/src/pages miniapp/main/src/app.config.ts`, dir: '/var/logwire-backend/build-output/backend' })
LogUtil.print(username, `[progress] [[1;34mInfo[m] 打包结束,访问 <a target="_blank" href="http://192.168.0.4:${port}/download/backend/">http://192.168.0.4:${port}/download/backend/</a> 下载 webapp.zip 文件 \n`) LogUtil.print(username, `[progress] [[1;34mInfo[m] 打包结束,访问 <a target="_blank" href="http://${HOST}:${port}/download/backend/">http://${HOST}:${port}/download/backend/</a> 下载 webapp.zip 文件 \n`)
} else { } else {
throw new Error('没有创建容器,请先初始化容器') throw new Error('没有创建容器,请先初始化容器')
} }
......
...@@ -9,7 +9,7 @@ export default defineEventHandler(async (event) => { ...@@ -9,7 +9,7 @@ export default defineEventHandler(async (event) => {
let user = await getPgTableData('user', username) let user = await getPgTableData('user', username)
if (!user) { if (!user) {
const port = await getAvailableNodePort(HOST) const port = await getAvailableNodePort(useRuntimeConfig().public.dockerHost)
await insertPgTableData('user', username) await insertPgTableData('user', username)
await setPgTableData('user', username, 'port', port) await setPgTableData('user', username, 'port', port)
user = await getPgTableData('user', username) user = await getPgTableData('user', username)
......
...@@ -179,22 +179,7 @@ class Docker { ...@@ -179,22 +179,7 @@ class Docker {
} }
} }
class DevDocker extends Docker {
constructor (username: string) {
super('localhost', username)
}
}
class ProductionDocker extends Docker {
constructor (username: string) {
super('192.168.0.4', username)
}
}
export function createDockerFactory (username: string) { export function createDockerFactory (username: string) {
if (process.env.NODE_ENV?.trim() === 'production') { const DOCKERHOST = useRuntimeConfig().public.dockerHost
return new ProductionDocker(username) return new Docker(process.env.NODE_ENV === 'development' ? 'localhost': DOCKERHOST, username)
} else {
return new DevDocker(username)
}
} }
\ No newline at end of file
declare module global { declare module 'nuxt/schema' {
namespace NodeJS { interface RuntimeConfig {
interface ProcessEnv { pgUser: string
DEVHOST: string pgPort: string
NODE_ENV: string pgHost: string
PGUSER: string pgPassword: string
postgres: string pgDatabase: string
} }
interface PublicRuntimeConfig {
dockerHost: string
} }
} }
// 当增强类型时,确保始终导入/导出某些内容是很重要的
export {}
\ No newline at end of file
import fs from 'fs' import fs from 'fs'
export const HOST = process.env.NODE_ENV === 'production' ? '192.168.0.4' : (process.env.DEVHOST || '192.168.0.190') export const HOST = useRuntimeConfig().public.dockerHost
export function readJson(path: string): Record<string, any> { export function readJson(path: string): Record<string, any> {
try { try {
......
import pg from 'pg' import pg from 'pg'
import { sendProjectStatus } from './log' import { sendProjectStatus } from './log'
export const PgPort = process.env.NODE_ENV?.trim() === 'production' ? 25556 : 30001 const config = useRuntimeConfig()
export const PgHost = process.env.NODE_ENV?.trim() === 'production' ? '192.168.0.4' : process.env.DEVHOST
export const PgPort = config.pgPort
export const PgHost = config.pgHost
let pool: pg.Pool = new pg.Pool({ let pool: pg.Pool = new pg.Pool({
host: PgHost, host: PgHost,
port: PgPort, port: Number(PgPort),
database: 'backend_helper' database: 'backend_helper',
user: config.pgUser,
password: config.pgPassword
}) })
export async function createPgClient (payload: { export async function createPgClient (payload: {
...@@ -18,11 +22,11 @@ export async function createPgClient (payload: { ...@@ -18,11 +22,11 @@ export async function createPgClient (payload: {
port?: number port?: number
}) { }) {
let client = new pg.Client({ let client = new pg.Client({
user: payload.user, user: payload.user || config.pgUser,
password: payload.password, password: payload.password || config.pgPassword,
database: payload.database, database: payload.database || config.pgDatabase,
host: payload.host || PgHost, host: payload.host || config.pgHost,
port: payload.port || PgPort port: payload.port || Number(config.pgPort)
}) })
await client.connect() await client.connect()
return client return client
......
...@@ -62,9 +62,9 @@ export async function copyAndCreateServerPropertiesV2InDocker (username: string) ...@@ -62,9 +62,9 @@ export async function copyAndCreateServerPropertiesV2InDocker (username: string)
const fn = (f: (obj: Record<string, any>) => string) => f(userConfigs!) || f(userDefaultSetting) const fn = (f: (obj: Record<string, any>) => string) => f(userConfigs!) || f(userDefaultSetting)
// postgres config // postgres config
backendText = backendText.replace(/spring.datasource.url=(.*?)\r?\n/, `spring.datasource.url=jdbc:postgresql://${HOST}:${PgPort}/${username}\n`) backendText = backendText.replace(/spring.datasource.url=(.*?)\r?\n/, `spring.datasource.url=jdbc:postgresql://${PgHost}:${PgPort}/${username}\n`)
backendText = backendText.replace(/spring.datasource.username=(.*?)\r?\n/, `spring.datasource.username=${process.env.PGUSER}\n`) backendText = backendText.replace(/spring.datasource.username=(.*?)\r?\n/, `spring.datasource.username=${useRuntimeConfig().pgUser}\n`)
backendText = backendText.replace(/spring.datasource.password=(.*?)\r?\n/, `spring.datasource.password=${process.env.PGPASSWORD}\n`) backendText = backendText.replace(/spring.datasource.password=(.*?)\r?\n/, `spring.datasource.password=${useRuntimeConfig().pgPassword}\n`)
// redis config // redis config
backendText = backendText.replace(/spring.redis.host=(.*?)\r?\n/, `spring.redis.host=${HOST}\n`) backendText = backendText.replace(/spring.redis.host=(.*?)\r?\n/, `spring.redis.host=${HOST}\n`)
...@@ -125,9 +125,9 @@ export async function copyAndCreateServerPropertiesV1InDocker (username: string) ...@@ -125,9 +125,9 @@ export async function copyAndCreateServerPropertiesV1InDocker (username: string)
const userConfigs = await getPgTableData('v1', username) const userConfigs = await getPgTableData('v1', username)
// postgres config // postgres config
backendText = backendText.replace(/spring.datasource.url=(.*?)\r?\n/, `spring.datasource.url=jdbc:postgresql://${HOST}:${PgPort}/${username + '_v1'}\n`) backendText = backendText.replace(/spring.datasource.url=(.*?)\r?\n/, `spring.datasource.url=jdbc:postgresql://${PgHost}:${PgPort}/${username + '_v1'}\n`)
backendText = backendText.replace(/spring.datasource.username=(.*?)\r?\n/, `spring.datasource.username=${process.env.PGUSER}\n`) backendText = backendText.replace(/spring.datasource.username=(.*?)\r?\n/, `spring.datasource.username=${useRuntimeConfig().pgUser}\n`)
backendText = backendText.replace(/spring.datasource.password=(.*?)\r?\n/, `spring.datasource.password=${process.env.PGPASSWORD}\n`) backendText = backendText.replace(/spring.datasource.password=(.*?)\r?\n/, `spring.datasource.password=${useRuntimeConfig().pgPassword}\n`)
// redis config // redis config
backendText = backendText.replace(/spring.redis.host=(.*?)\r?\n/, `spring.redis.host=${HOST}\n`) backendText = backendText.replace(/spring.redis.host=(.*?)\r?\n/, `spring.redis.host=${HOST}\n`)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment