mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
feat(dev-proxy): reload env file changes (#36384)
This commit is contained in:
9 files changed
+274
-15
No files matched your search
@@ -1,6 +1,9 @@
|
||||
import type { ServerType } from '@hono/node-server'
|
||||
import type { DevProxyCliOptions, DevProxyConfig } from './types'
|
||||
import process from 'node:process'
|
||||
import { serve } from '@hono/node-server'
|
||||
import { loadDevProxyConfig, parseDevProxyCliArgs, resolveDevProxyServerOptions } from './config'
|
||||
import { watch } from 'chokidar'
|
||||
import { assertDevProxyConfig, loadDevProxyConfig, parseDevProxyCliArgs, resolveDevProxyServerOptions, watchDevProxyConfig } from './config'
|
||||
import { createDevProxyApp } from './server'
|
||||
|
||||
function printUsage() {
|
||||
@@ -12,6 +15,8 @@ Options:
|
||||
--env-file <path> Load environment variables before evaluating the config file.
|
||||
--host <host> Override the configured host.
|
||||
--port <port> Override the configured port.
|
||||
--watch Reload config and env file changes. Enabled by default.
|
||||
--no-watch Disable config and env file reloads.
|
||||
--help, -h Show this help message.`)
|
||||
}
|
||||
|
||||
@@ -22,6 +27,78 @@ async function flushStandardStreams() {
|
||||
])
|
||||
}
|
||||
|
||||
const closeServer = (server: ServerType) => new Promise<void>((resolve, reject) => {
|
||||
server.close((error) => {
|
||||
if (error)
|
||||
reject(error)
|
||||
else
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
|
||||
const startDevProxyServer = (config: DevProxyConfig, cliOptions: DevProxyCliOptions) => {
|
||||
let app = createDevProxyApp(config)
|
||||
const { host, port } = resolveDevProxyServerOptions(config.server, cliOptions)
|
||||
const server = serve({
|
||||
fetch: (request, env) => app.fetch(request, env),
|
||||
hostname: host,
|
||||
port,
|
||||
})
|
||||
|
||||
return {
|
||||
host,
|
||||
port,
|
||||
server,
|
||||
updateConfig(nextConfig: DevProxyConfig) {
|
||||
app = createDevProxyApp(nextConfig)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const createDevProxyRuntime = (initialConfig: DevProxyConfig, cliOptions: DevProxyCliOptions) => {
|
||||
let runtime = startDevProxyServer(initialConfig, cliOptions)
|
||||
let reloadTask = Promise.resolve()
|
||||
|
||||
console.log(`[dev-proxy] listening on http://${runtime.host}:${runtime.port}`)
|
||||
|
||||
const reload = async (nextConfig: unknown, reason: string) => {
|
||||
assertDevProxyConfig(nextConfig)
|
||||
const nextServerOptions = resolveDevProxyServerOptions(nextConfig.server, cliOptions)
|
||||
|
||||
if (runtime.host === nextServerOptions.host && runtime.port === nextServerOptions.port) {
|
||||
runtime.updateConfig(nextConfig)
|
||||
console.log(`[dev-proxy] reloaded ${reason}`)
|
||||
return
|
||||
}
|
||||
|
||||
await closeServer(runtime.server)
|
||||
runtime = startDevProxyServer(nextConfig, cliOptions)
|
||||
console.log(`[dev-proxy] restarted on http://${runtime.host}:${runtime.port} after ${reason}`)
|
||||
}
|
||||
|
||||
const enqueueReload = (loadConfig: () => Promise<unknown> | unknown, reason: string) => {
|
||||
reloadTask = reloadTask.then(async () => {
|
||||
try {
|
||||
await reload(await loadConfig(), reason)
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`[dev-proxy] failed to reload ${reason}`)
|
||||
console.error(error instanceof Error ? error.message : error)
|
||||
}
|
||||
})
|
||||
|
||||
return reloadTask
|
||||
}
|
||||
|
||||
return {
|
||||
enqueueReload,
|
||||
close: async () => {
|
||||
await reloadTask
|
||||
await closeServer(runtime.server)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const cliOptions = parseDevProxyCliArgs(process.argv.slice(2))
|
||||
|
||||
@@ -33,16 +110,44 @@ async function main() {
|
||||
const config = await loadDevProxyConfig(cliOptions.config, process.cwd(), {
|
||||
envFile: cliOptions.envFile,
|
||||
})
|
||||
const { host, port } = resolveDevProxyServerOptions(config.server, cliOptions)
|
||||
const app = createDevProxyApp(config)
|
||||
const runtime = createDevProxyRuntime(config, cliOptions)
|
||||
|
||||
serve({
|
||||
fetch: app.fetch,
|
||||
hostname: host,
|
||||
port,
|
||||
if (cliOptions.watch === false)
|
||||
return
|
||||
|
||||
const configWatcher = await watchDevProxyConfig(cliOptions.config, process.cwd(), {
|
||||
envFile: cliOptions.envFile,
|
||||
onUpdate: ({ newConfig }) => runtime.enqueueReload(() => newConfig.config, 'config changes'),
|
||||
})
|
||||
|
||||
console.log(`[dev-proxy] listening on http://${host}:${port}`)
|
||||
const envWatcher = cliOptions.envFile
|
||||
? watch(cliOptions.envFile, {
|
||||
cwd: process.cwd(),
|
||||
ignoreInitial: true,
|
||||
})
|
||||
: undefined
|
||||
|
||||
envWatcher?.on('all', () => {
|
||||
void runtime.enqueueReload(
|
||||
() => loadDevProxyConfig(cliOptions.config, process.cwd(), {
|
||||
envFile: cliOptions.envFile,
|
||||
}),
|
||||
'env file changes',
|
||||
)
|
||||
})
|
||||
|
||||
const cleanup = async () => {
|
||||
await envWatcher?.close()
|
||||
await configWatcher.unwatch()
|
||||
await runtime.close()
|
||||
}
|
||||
|
||||
process.once('SIGINT', () => {
|
||||
void cleanup().finally(() => process.exit(0))
|
||||
})
|
||||
process.once('SIGTERM', () => {
|
||||
void cleanup().finally(() => process.exit(0))
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user