@@ -3,6 +3,8 @@ package self_check
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "path/filepath"
7
+ "runtime"
6
8
"strings"
7
9
"time"
8
10
@@ -12,6 +14,15 @@ import (
12
14
"github.com/tufanbarisyildirim/gonginx/parser"
13
15
)
14
16
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
+
15
26
// CheckNginxConfIncludeSites checks if nginx.conf include sites-enabled
16
27
func CheckNginxConfIncludeSites () error {
17
28
path := nginx .GetConfEntryPath ()
@@ -34,7 +45,7 @@ func CheckNginxConfIncludeSites() error {
34
45
// find include sites-enabled
35
46
for _ , directive := range v .GetBlock ().GetDirectives () {
36
47
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/*" ) {
38
49
return nil
39
50
}
40
51
}
@@ -67,7 +78,7 @@ func CheckNginxConfIncludeStreams() error {
67
78
// find include sites-enabled
68
79
for _ , directive := range v .GetBlock ().GetDirectives () {
69
80
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/*" ) {
71
82
return nil
72
83
}
73
84
}
@@ -107,7 +118,7 @@ func FixNginxConfIncludeSites() error {
107
118
// add include sites-enabled/* to http block
108
119
includeDirective := & config.Directive {
109
120
Name : "include" ,
110
- Parameters : []config.Parameter {{Value : nginx . GetConfPath ("sites-enabled/*" )}},
121
+ Parameters : []config.Parameter {{Value : resolvePath ("sites-enabled/*" )}},
111
122
}
112
123
113
124
realBlock := v .GetBlock ().(* config.HTTP )
@@ -119,7 +130,7 @@ func FixNginxConfIncludeSites() error {
119
130
}
120
131
121
132
// 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/*" ))... )
123
134
return os .WriteFile (path , content , 0644 )
124
135
}
125
136
@@ -152,7 +163,7 @@ func FixNginxConfIncludeStreams() error {
152
163
// add include streams-enabled/* to stream block
153
164
includeDirective := & config.Directive {
154
165
Name : "include" ,
155
- Parameters : []config.Parameter {{Value : nginx . GetConfPath ("streams-enabled/*" )}},
166
+ Parameters : []config.Parameter {{Value : resolvePath ("streams-enabled/*" )}},
156
167
}
157
168
realBlock := v .GetBlock ().(* config.Block )
158
169
realBlock .Directives = append (realBlock .Directives , includeDirective )
@@ -163,7 +174,7 @@ func FixNginxConfIncludeStreams() error {
163
174
}
164
175
165
176
// 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/*" ))... )
167
178
return os .WriteFile (path , content , 0644 )
168
179
}
169
180
@@ -189,7 +200,7 @@ func CheckNginxConfIncludeConfD() error {
189
200
// find include conf.d
190
201
for _ , directive := range v .GetBlock ().GetDirectives () {
191
202
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/*" ) {
193
204
return nil
194
205
}
195
206
}
@@ -229,7 +240,7 @@ func FixNginxConfIncludeConfD() error {
229
240
// add include conf.d/*.conf to http block
230
241
includeDirective := & config.Directive {
231
242
Name : "include" ,
232
- Parameters : []config.Parameter {{Value : nginx . GetConfPath ("conf.d/*.conf" )}},
243
+ Parameters : []config.Parameter {{Value : resolvePath ("conf.d/*.conf" )}},
233
244
}
234
245
235
246
realBlock := v .GetBlock ().(* config.HTTP )
@@ -241,6 +252,6 @@ func FixNginxConfIncludeConfD() error {
241
252
}
242
253
243
254
// 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" ))... )
245
256
return os .WriteFile (path , content , 0644 )
246
257
}
0 commit comments