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

dev: 环境变量

parent 75221ace
......@@ -112,7 +112,7 @@ async function handleInstall() {
execute()
}
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 map = new Map([
['null', '请点击右侧按钮完成项目初始化'],
......
......@@ -141,7 +141,7 @@ async function handleInstall() {
}
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 map = new Map([
['null', '请点击右侧按钮完成项目初始化'],
......
......@@ -8,5 +8,15 @@ export default defineNuxtConfig({
experimental: {
websocket: true
}
},
runtimeConfig: {
pgUser: '',
pgPort: '',
pgHost: '',
pgPassword: '',
pgDatabase: '',
public: {
dockerHost: ''
}
}
})
......@@ -12,7 +12,7 @@ export default defineEventHandler(async (event) => {
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: `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 {
throw new Error('没有创建容器,请先初始化容器')
}
......
......@@ -9,7 +9,7 @@ export default defineEventHandler(async (event) => {
let user = await getPgTableData('user', username)
if (!user) {
const port = await getAvailableNodePort(HOST)
const port = await getAvailableNodePort(useRuntimeConfig().public.dockerHost)
await insertPgTableData('user', username)
await setPgTableData('user', username, 'port', port)
user = await getPgTableData('user', username)
......
......@@ -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) {
if (process.env.NODE_ENV?.trim() === 'production') {
return new ProductionDocker(username)
} else {
return new DevDocker(username)
}
const DOCKERHOST = useRuntimeConfig().public.dockerHost
return new Docker(process.env.NODE_ENV === 'development' ? 'localhost': DOCKERHOST, username)
}
\ No newline at end of file
declare module global {
namespace NodeJS {
interface ProcessEnv {
DEVHOST: string
NODE_ENV: string
PGUSER: string
postgres: string
declare module 'nuxt/schema' {
interface RuntimeConfig {
pgUser: string
pgPort: string
pgHost: string
pgPassword: string
pgDatabase: string
}
interface PublicRuntimeConfig {
dockerHost: string
}
}
// 当增强类型时,确保始终导入/导出某些内容是很重要的
export {}
\ No newline at end of file
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> {
try {
......
import pg from 'pg'
import { sendProjectStatus } from './log'
export const PgPort = process.env.NODE_ENV?.trim() === 'production' ? 25556 : 30001
export const PgHost = process.env.NODE_ENV?.trim() === 'production' ? '192.168.0.4' : process.env.DEVHOST
const config = useRuntimeConfig()
export const PgPort = config.pgPort
export const PgHost = config.pgHost
let pool: pg.Pool = new pg.Pool({
host: PgHost,
port: PgPort,
database: 'backend_helper'
port: Number(PgPort),
database: 'backend_helper',
user: config.pgUser,
password: config.pgPassword
})
export async function createPgClient (payload: {
......@@ -18,11 +22,11 @@ export async function createPgClient (payload: {
port?: number
}) {
let client = new pg.Client({
user: payload.user,
password: payload.password,
database: payload.database,
host: payload.host || PgHost,
port: payload.port || PgPort
user: payload.user || config.pgUser,
password: payload.password || config.pgPassword,
database: payload.database || config.pgDatabase,
host: payload.host || config.pgHost,
port: payload.port || Number(config.pgPort)
})
await client.connect()
return client
......
......@@ -62,9 +62,9 @@ export async function copyAndCreateServerPropertiesV2InDocker (username: string)
const fn = (f: (obj: Record<string, any>) => string) => f(userConfigs!) || f(userDefaultSetting)
// postgres config
backendText = backendText.replace(/spring.datasource.url=(.*?)\r?\n/, `spring.datasource.url=jdbc:postgresql://${HOST}:${PgPort}/${username}\n`)
backendText = backendText.replace(/spring.datasource.username=(.*?)\r?\n/, `spring.datasource.username=${process.env.PGUSER}\n`)
backendText = backendText.replace(/spring.datasource.password=(.*?)\r?\n/, `spring.datasource.password=${process.env.PGPASSWORD}\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=${useRuntimeConfig().pgUser}\n`)
backendText = backendText.replace(/spring.datasource.password=(.*?)\r?\n/, `spring.datasource.password=${useRuntimeConfig().pgPassword}\n`)
// redis config
backendText = backendText.replace(/spring.redis.host=(.*?)\r?\n/, `spring.redis.host=${HOST}\n`)
......@@ -125,9 +125,9 @@ export async function copyAndCreateServerPropertiesV1InDocker (username: string)
const userConfigs = await getPgTableData('v1', username)
// 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.username=(.*?)\r?\n/, `spring.datasource.username=${process.env.PGUSER}\n`)
backendText = backendText.replace(/spring.datasource.password=(.*?)\r?\n/, `spring.datasource.password=${process.env.PGPASSWORD}\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=${useRuntimeConfig().pgUser}\n`)
backendText = backendText.replace(/spring.datasource.password=(.*?)\r?\n/, `spring.datasource.password=${useRuntimeConfig().pgPassword}\n`)
// redis config
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