diff --git a/internal/app/app.go b/internal/app/app.go index d39b3b9..fc8aa6a 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -36,16 +36,19 @@ type App struct { listeners []net.Listener } +// NewApp creates a new App instance with the given config path. func NewApp(cfgPath string) *App { return &App{ cfgPath: cfgPath, } } +// SetPidFile sets the path to the PID file for the app. func (a *App) SetPidFile(path string) { a.pidFile = path } +// SetLogFile sets the path to the log file for the app. func (a *App) SetLogFile(path string) { a.logFile = path } diff --git a/internal/app/import.go b/internal/app/import.go index 066f9c6..f373d32 100644 --- a/internal/app/import.go +++ b/internal/app/import.go @@ -79,8 +79,8 @@ func importNginxConfig(path, outputPath string) error { fmt.Fprintf(os.Stderr, "warning: %s:line %d: %s\n", w.File, w.Line, w.Message) } - if err := config.Validate(result.Config); err != nil { - return fmt.Errorf("转换后配置验证失败: %w", err) + if validateErr := config.Validate(result.Config); validateErr != nil { + return fmt.Errorf("转换后配置验证失败: %w", validateErr) } yamlData, err := yaml.Marshal(result.Config) @@ -89,7 +89,9 @@ func importNginxConfig(path, outputPath string) error { } if outputPath == "" { - os.Stdout.Write(yamlData) + if _, err := os.Stdout.Write(yamlData); err != nil { + return fmt.Errorf("写入标准输出失败: %w", err) + } } else { if err := os.MkdirAll(filepath.Dir(outputPath), 0o755); err != nil { return fmt.Errorf("创建输出目录失败: %w", err) diff --git a/internal/converter/nginx/converter.go b/internal/converter/nginx/converter.go index af78e9c..5a47d40 100644 --- a/internal/converter/nginx/converter.go +++ b/internal/converter/nginx/converter.go @@ -10,6 +10,12 @@ import ( "rua.plus/lolly/internal/config" ) +const ( + gzipType = "gzip" + offValue = "off" + redirectType = "redirect" +) + // Warning represents a conversion warning for unsupported or partially supported directives. type Warning struct { Directive string @@ -44,18 +50,18 @@ type locationClassification struct { // unsupportedDirectives are known nginx directives that have no lolly equivalent. var unsupportedDirectives = map[string]string{ - "if": "the 'if' directive is not supported; consider using map or rewrite", - "map": "the 'map' directive is not supported; use variables config instead", - "set": "the 'set' directive is not supported; use variables config instead", - "limit_req": "the 'limit_req' directive is not supported; use rate_limit config instead", - "limit_conn": "the 'limit_conn' directive is not supported", - "add_header": "the 'add_header' directive is not supported; use security.headers config instead", - "more_set_headers": "the 'more_set_headers' directive is not supported; use security.headers config instead", - "auth_request": "the 'auth_request' directive is not supported; use security.auth_request config instead", - "split_clients": "the 'split_clients' directive is not supported", - "geo": "the 'geo' directive is not supported; use access.geoip config instead", - "range": "the 'range' directive is not supported", - "return": "the 'return' directive is not supported for non-redirect status codes; only 301/302 are supported", + "if": "the 'if' directive is not supported; consider using map or rewrite", + "map": "the 'map' directive is not supported; use variables config instead", + "set": "the 'set' directive is not supported; use variables config instead", + "limit_req": "the 'limit_req' directive is not supported; use rate_limit config instead", + "limit_conn": "the 'limit_conn' directive is not supported", + "add_header": "the 'add_header' directive is not supported; use security.headers config instead", + "more_set_headers": "the 'more_set_headers' directive is not supported; use security.headers config instead", + "auth_request": "the 'auth_request' directive is not supported; use security.auth_request config instead", + "split_clients": "the 'split_clients' directive is not supported", + "geo": "the 'geo' directive is not supported; use access.geoip config instead", + "range": "the 'range' directive is not supported", + "return": "the 'return' directive is not supported for non-redirect status codes; only 301/302 are supported", } // Convert converts a parsed nginx configuration to a lolly configuration. @@ -91,7 +97,8 @@ func Convert(nginxCfg *NginxConfig) (*ConvertResult, error) { var serverBlocks []Directive for i := range nginxCfg.Directives { d := &nginxCfg.Directives[i] - if d.Name == "http" { + switch d.Name { + case "http": // Check for unsupported directives at the http level. for j := range d.Block { bd := &d.Block[j] @@ -106,7 +113,7 @@ func Convert(nginxCfg *NginxConfig) (*ConvertResult, error) { }) } } - } else if d.Name == "server" { + case "server": serverBlocks = append(serverBlocks, *d) } } @@ -207,7 +214,7 @@ func convertServerBlock(d *Directive, upstreams map[string]*upstreamInfo, result if len(bd.Args) > 0 { server.SSL.Key = bd.Args[0] } - case "gzip": + case gzipType: parseGzip(bd, &server) case "gzip_types": server.Compression.Types = bd.Args @@ -223,7 +230,7 @@ func convertServerBlock(d *Directive, upstreams map[string]*upstreamInfo, result } case "server_tokens": if len(bd.Args) > 0 { - server.ServerTokens = bd.Args[0] != "off" + server.ServerTokens = bd.Args[0] != offValue } case "access_log": parseAccessLog(bd, result) @@ -441,7 +448,7 @@ func parseErrorPage(d *Directive, server *config.ServerConfig) { // parseAuthBasic parses an auth_basic directive. func parseAuthBasic(d *Directive, server *config.ServerConfig) { if len(d.Args) > 0 { - if d.Args[0] != "off" { + if d.Args[0] != offValue { server.Security.Auth.Type = "basic" server.Security.Auth.Realm = d.Args[0] } @@ -508,7 +515,7 @@ func classifyLocation(d *Directive, result *ConvertResult) locationClassificatio case hasRootOrAlias: class.LocType = "static" case hasRedirect: - class.LocType = "redirect" + class.LocType = redirectType default: class.LocType = "unsupported" } diff --git a/internal/converter/nginx/parser.go b/internal/converter/nginx/parser.go index 3b565cc..309b289 100644 --- a/internal/converter/nginx/parser.go +++ b/internal/converter/nginx/parser.go @@ -127,9 +127,7 @@ func (p *parser) parseDirectives() ([]Directive, error) { directives = append(directives, *d) // Drain any extra directives injected by include expansion. - for _, extra := range p.extraDirectives { - directives = append(directives, extra) - } + directives = append(directives, p.extraDirectives...) p.extraDirectives = nil } return directives, nil