@@ -3,6 +3,8 @@ package self_check
33import (
44 "fmt"
55 "os"
6+ "path/filepath"
7+ "runtime"
68 "strings"
79 "time"
810
@@ -12,6 +14,15 @@ import (
1214 "github.com/tufanbarisyildirim/gonginx/parser"
1315)
1416
17+ func resolvePath (path ... string ) string {
18+ // fix #1046
19+ if runtime .GOOS == "windows" {
20+ return strings .TrimLeft (filepath .ToSlash (strings .ReplaceAll (nginx .GetConfPath (path ... ), nginx .GetNginxExeDir (), "" )), "/" )
21+ }
22+
23+ return nginx .GetConfPath (path ... )
24+ }
25+
1526// CheckNginxConfIncludeSites checks if nginx.conf include sites-enabled
1627func CheckNginxConfIncludeSites () error {
1728 path := nginx .GetConfEntryPath ()
@@ -34,7 +45,7 @@ func CheckNginxConfIncludeSites() error {
3445 // find include sites-enabled
3546 for _ , directive := range v .GetBlock ().GetDirectives () {
3647 if directive .GetName () == "include" && len (directive .GetParameters ()) > 0 &&
37- directive .GetParameters ()[0 ].Value == nginx . GetConfPath ( "sites-enabled/*" ) {
48+ strings . Contains ( directive .GetParameters ()[0 ].Value , "sites-enabled/*" ) {
3849 return nil
3950 }
4051 }
@@ -67,7 +78,7 @@ func CheckNginxConfIncludeStreams() error {
6778 // find include sites-enabled
6879 for _ , directive := range v .GetBlock ().GetDirectives () {
6980 if directive .GetName () == "include" && len (directive .GetParameters ()) > 0 &&
70- directive .GetParameters ()[0 ].Value == nginx . GetConfPath ( "streams-enabled/*" ) {
81+ strings . Contains ( directive .GetParameters ()[0 ].Value , "streams-enabled/*" ) {
7182 return nil
7283 }
7384 }
@@ -107,7 +118,7 @@ func FixNginxConfIncludeSites() error {
107118 // add include sites-enabled/* to http block
108119 includeDirective := & config.Directive {
109120 Name : "include" ,
110- Parameters : []config.Parameter {{Value : nginx . GetConfPath ("sites-enabled/*" )}},
121+ Parameters : []config.Parameter {{Value : resolvePath ("sites-enabled/*" )}},
111122 }
112123
113124 realBlock := v .GetBlock ().(* config.HTTP )
@@ -119,7 +130,7 @@ func FixNginxConfIncludeSites() error {
119130 }
120131
121132 // if no http block, append http block with include sites-enabled/*
122- content = append (content , fmt .Appendf (nil , "\n http {\n \t include %s;\n }\n " , nginx . GetConfPath ("sites-enabled/*" ))... )
133+ content = append (content , fmt .Appendf (nil , "\n http {\n \t include %s;\n }\n " , resolvePath ("sites-enabled/*" ))... )
123134 return os .WriteFile (path , content , 0644 )
124135}
125136
@@ -152,7 +163,7 @@ func FixNginxConfIncludeStreams() error {
152163 // add include streams-enabled/* to stream block
153164 includeDirective := & config.Directive {
154165 Name : "include" ,
155- Parameters : []config.Parameter {{Value : nginx . GetConfPath ("streams-enabled/*" )}},
166+ Parameters : []config.Parameter {{Value : resolvePath ("streams-enabled/*" )}},
156167 }
157168 realBlock := v .GetBlock ().(* config.Block )
158169 realBlock .Directives = append (realBlock .Directives , includeDirective )
@@ -163,7 +174,7 @@ func FixNginxConfIncludeStreams() error {
163174 }
164175
165176 // if no stream block, append stream block with include streams-enabled/*
166- content = append (content , fmt .Appendf (nil , "\n stream {\n \t include %s;\n }\n " , nginx . GetConfPath ("streams-enabled/*" ))... )
177+ content = append (content , fmt .Appendf (nil , "\n stream {\n \t include %s;\n }\n " , resolvePath ("streams-enabled/*" ))... )
167178 return os .WriteFile (path , content , 0644 )
168179}
169180
@@ -189,7 +200,7 @@ func CheckNginxConfIncludeConfD() error {
189200 // find include conf.d
190201 for _ , directive := range v .GetBlock ().GetDirectives () {
191202 if directive .GetName () == "include" && len (directive .GetParameters ()) > 0 &&
192- strings .HasPrefix (directive .GetParameters ()[0 ].Value , nginx . GetConfPath ( "conf.d" ) ) {
203+ strings .Contains (directive .GetParameters ()[0 ].Value , "conf.d/*" ) {
193204 return nil
194205 }
195206 }
@@ -229,7 +240,7 @@ func FixNginxConfIncludeConfD() error {
229240 // add include conf.d/*.conf to http block
230241 includeDirective := & config.Directive {
231242 Name : "include" ,
232- Parameters : []config.Parameter {{Value : nginx . GetConfPath ("conf.d/*.conf" )}},
243+ Parameters : []config.Parameter {{Value : resolvePath ("conf.d/*.conf" )}},
233244 }
234245
235246 realBlock := v .GetBlock ().(* config.HTTP )
@@ -241,6 +252,6 @@ func FixNginxConfIncludeConfD() error {
241252 }
242253
243254 // if no http block, append http block with include conf.d/*.conf
244- content = append (content , fmt .Appendf (nil , "\n http {\n \t include %s;\n }\n " , nginx . GetConfPath ("conf.d/*.conf" ))... )
255+ content = append (content , fmt .Appendf (nil , "\n http {\n \t include %s;\n }\n " , resolvePath ("conf.d/*.conf" ))... )
245256 return os .WriteFile (path , content , 0644 )
246257}
0 commit comments