[优化] 代码优化

This commit is contained in:
ryan
2026-05-31 19:59:09 +08:00
parent bc3d9ee285
commit 4775e5cb73
+16 -23
View File
@@ -24,15 +24,8 @@ import (
"openflare-agent/internal/protocol" "openflare-agent/internal/protocol"
) )
const CertDirPlaceholder = "__OPENFLARE_CERT_DIR__"
const RouteConfigPlaceholder = "__OPENFLARE_ROUTE_CONFIG__"
const AccessLogPlaceholder = "__OPENFLARE_ACCESS_LOG__"
const LuaDirPlaceholder = "__OPENFLARE_LUA_DIR__"
const RuntimeConfigDirPlaceholder = "__OPENFLARE_RUNTIME_CONFIG_DIR__" const RuntimeConfigDirPlaceholder = "__OPENFLARE_RUNTIME_CONFIG_DIR__"
const ObservabilityListenPlaceholder = "__OPENFLARE_OBSERVABILITY_LISTEN__"
const ObservabilityPortPlaceholder = "__OPENFLARE_OBSERVABILITY_PORT__"
const ResolverDirectivePlaceholder = "__OPENFLARE_RESOLVER_DIRECTIVE__" const ResolverDirectivePlaceholder = "__OPENFLARE_RESOLVER_DIRECTIVE__"
const PowStaticDirPlaceholder = "__OPENFLARE_POW_STATIC_DIR__"
type Executor interface { type Executor interface {
Test(ctx context.Context) error Test(ctx context.Context) error
@@ -395,30 +388,30 @@ func (m *Manager) CurrentChecksum() (string, error) {
} }
normalizedMain := string(mainData) normalizedMain := string(mainData)
if includePath := m.routeConfigIncludePath(); includePath != "" { if includePath := m.routeConfigIncludePath(); includePath != "" {
normalizedMain = strings.ReplaceAll(normalizedMain, includePath, RouteConfigPlaceholder) normalizedMain = strings.ReplaceAll(normalizedMain, includePath, openrestyrender.RouteConfigPlaceholder)
} }
if accessLogPath := m.accessLogRuntimePath(); accessLogPath != "" { if accessLogPath := m.accessLogRuntimePath(); accessLogPath != "" {
normalizedMain = strings.ReplaceAll(normalizedMain, accessLogPath, AccessLogPlaceholder) normalizedMain = strings.ReplaceAll(normalizedMain, accessLogPath, openrestyrender.AccessLogPlaceholder)
} }
if luaDir := m.luaRuntimePath(); luaDir != "" { if luaDir := m.luaRuntimePath(); luaDir != "" {
normalizedMain = strings.ReplaceAll(normalizedMain, luaDir, LuaDirPlaceholder) normalizedMain = strings.ReplaceAll(normalizedMain, luaDir, openrestyrender.LuaDirPlaceholder)
} }
if listen := strings.TrimSpace(m.OpenrestyObservabilityListen); listen != "" { if listen := strings.TrimSpace(m.OpenrestyObservabilityListen); listen != "" {
normalizedMain = strings.ReplaceAll(normalizedMain, listen, ObservabilityListenPlaceholder) normalizedMain = strings.ReplaceAll(normalizedMain, listen, openrestyrender.ObservabilityListenPlaceholder)
} }
if m.OpenrestyObservabilityPort > 0 { if m.OpenrestyObservabilityPort > 0 {
normalizedMain = strings.ReplaceAll(normalizedMain, fmt.Sprintf("%d", m.OpenrestyObservabilityPort), ObservabilityPortPlaceholder) normalizedMain = strings.ReplaceAll(normalizedMain, fmt.Sprintf("%d", m.OpenrestyObservabilityPort), openrestyrender.ObservabilityPortPlaceholder)
} }
if resolverDirective := strings.TrimSpace(m.OpenrestyResolverDirective); resolverDirective != "" { if resolverDirective := strings.TrimSpace(m.OpenrestyResolverDirective); resolverDirective != "" {
normalizedMain = strings.ReplaceAll(normalizedMain, resolverDirective, ResolverDirectivePlaceholder) normalizedMain = strings.ReplaceAll(normalizedMain, resolverDirective, ResolverDirectivePlaceholder)
} }
normalizedRoute := string(data) normalizedRoute := string(data)
if m.NginxCertDir != "" { if m.NginxCertDir != "" {
normalizedRoute = strings.ReplaceAll(normalizedRoute, m.NginxCertDir, CertDirPlaceholder) normalizedRoute = strings.ReplaceAll(normalizedRoute, m.NginxCertDir, openrestyrender.CertDirPlaceholder)
} }
if luaDir := m.luaRuntimePath(); luaDir != "" { if luaDir := m.luaRuntimePath(); luaDir != "" {
normalizedRoute = strings.ReplaceAll(normalizedRoute, luaDir+"/pow/static", PowStaticDirPlaceholder) normalizedRoute = strings.ReplaceAll(normalizedRoute, luaDir+"/pow/static", openrestyrender.PowStaticDirPlaceholder)
normalizedRoute = strings.ReplaceAll(normalizedRoute, luaDir, LuaDirPlaceholder) normalizedRoute = strings.ReplaceAll(normalizedRoute, luaDir, openrestyrender.LuaDirPlaceholder)
} }
files, err := m.readManagedSupportFiles() files, err := m.readManagedSupportFiles()
if err != nil { if err != nil {
@@ -1051,11 +1044,11 @@ func (m *Manager) ensureMimeTypes() error {
func (m *Manager) renderRouteConfig(content string) string { func (m *Manager) renderRouteConfig(content string) string {
rendered := content rendered := content
if m.NginxCertDir != "" { if m.NginxCertDir != "" {
rendered = strings.ReplaceAll(rendered, CertDirPlaceholder, m.NginxCertDir) rendered = strings.ReplaceAll(rendered, openrestyrender.CertDirPlaceholder, m.NginxCertDir)
} }
if luaDir := m.luaRuntimePath(); luaDir != "" { if luaDir := m.luaRuntimePath(); luaDir != "" {
rendered = strings.ReplaceAll(rendered, LuaDirPlaceholder, luaDir) rendered = strings.ReplaceAll(rendered, openrestyrender.LuaDirPlaceholder, luaDir)
rendered = strings.ReplaceAll(rendered, PowStaticDirPlaceholder, luaDir+"/pow/static") rendered = strings.ReplaceAll(rendered, openrestyrender.PowStaticDirPlaceholder, luaDir+"/pow/static")
} }
return rendered return rendered
} }
@@ -1063,19 +1056,19 @@ func (m *Manager) renderRouteConfig(content string) string {
func (m *Manager) renderMainConfig(content string) string { func (m *Manager) renderMainConfig(content string) string {
rendered := content rendered := content
if includePath := m.routeConfigIncludePath(); includePath != "" { if includePath := m.routeConfigIncludePath(); includePath != "" {
rendered = strings.ReplaceAll(rendered, RouteConfigPlaceholder, includePath) rendered = strings.ReplaceAll(rendered, openrestyrender.RouteConfigPlaceholder, includePath)
} }
if accessLogPath := m.accessLogRuntimePath(); accessLogPath != "" { if accessLogPath := m.accessLogRuntimePath(); accessLogPath != "" {
rendered = strings.ReplaceAll(rendered, AccessLogPlaceholder, accessLogPath) rendered = strings.ReplaceAll(rendered, openrestyrender.AccessLogPlaceholder, accessLogPath)
} }
if luaDir := m.luaRuntimePath(); luaDir != "" { if luaDir := m.luaRuntimePath(); luaDir != "" {
rendered = strings.ReplaceAll(rendered, LuaDirPlaceholder, luaDir) rendered = strings.ReplaceAll(rendered, openrestyrender.LuaDirPlaceholder, luaDir)
} }
if listen := strings.TrimSpace(m.OpenrestyObservabilityListen); listen != "" { if listen := strings.TrimSpace(m.OpenrestyObservabilityListen); listen != "" {
rendered = strings.ReplaceAll(rendered, ObservabilityListenPlaceholder, listen) rendered = strings.ReplaceAll(rendered, openrestyrender.ObservabilityListenPlaceholder, listen)
} }
if m.OpenrestyObservabilityPort > 0 { if m.OpenrestyObservabilityPort > 0 {
rendered = strings.ReplaceAll(rendered, ObservabilityPortPlaceholder, fmt.Sprintf("%d", m.OpenrestyObservabilityPort)) rendered = strings.ReplaceAll(rendered, openrestyrender.ObservabilityPortPlaceholder, fmt.Sprintf("%d", m.OpenrestyObservabilityPort))
} }
if resolverDirective := strings.TrimSpace(m.OpenrestyResolverDirective); resolverDirective != "" { if resolverDirective := strings.TrimSpace(m.OpenrestyResolverDirective); resolverDirective != "" {
rendered = strings.ReplaceAll(rendered, ResolverDirectivePlaceholder, resolverDirective) rendered = strings.ReplaceAll(rendered, ResolverDirectivePlaceholder, resolverDirective)