@@ -126,35 +126,27 @@ func fileExists(filename string) bool {
126126 return ! info .IsDir ()
127127}
128128
129- // copy a file, whilst doing some search/replace on the data.
130- func copyReplace (src string , dst string ,
131- search []byte , replace []byte ,
132- search2 []byte , replace2 []byte ,
133- ) {
134- data , err := os .ReadFile (src )
129+ func mustReadFile (path string ) []byte {
130+ data , err := os .ReadFile (path )
135131 if err != nil {
136132 // Error Reading the file
137133 logger .Log (logger .LevelError , nil , err , "reading file" )
138134 os .Exit (1 )
139135 }
140136
141- data1 := bytes .ReplaceAll (data , search , replace )
142- data2 := bytes .ReplaceAll (data1 , search2 , replace2 )
143- fileMode := 0o600
137+ return data
138+ }
144139
145- err = os .WriteFile (dst , data2 , fs .FileMode (fileMode ))
140+ func mustWriteFile (path string , data []byte ) {
141+ err := os .WriteFile (path , data , fs .FileMode (0o600 ))
146142 if err != nil {
147143 // Error writing the file
148144 logger .Log (logger .LevelError , nil , err , "writing file" )
149145 os .Exit (1 )
150146 }
151147}
152148
153- // make sure the base-url is updated in the index.html file.
154- func baseURLReplace (staticDir string , baseURL string ) {
155- indexBaseURL := path .Join (staticDir , "index.baseUrl.html" )
156- index := path .Join (staticDir , "index.html" )
157-
149+ func makeBaseURLReplacements (data []byte , baseURL string ) []byte {
158150 replaceURL := baseURL
159151 if baseURL == "" {
160152 // We have to do the replace when baseURL == "" because of the case when
@@ -163,20 +155,45 @@ func baseURLReplace(staticDir string, baseURL string) {
163155 replaceURL = "/"
164156 }
165157
166- if ! fileExists (indexBaseURL ) {
167- copyReplace (index , indexBaseURL , []byte ("" ), []byte ("" ), []byte ("" ), []byte ("" ))
168- }
158+ // Replacement for headlampBaseUrl - matched from the known index.html content
159+ data = bytes .ReplaceAll (
160+ data ,
161+ []byte ("headlampBaseUrl = __baseUrl__" ),
162+ []byte (fmt .Sprintf ("headlampBaseUrl = '%s'" , replaceURL )),
163+ )
169164
170- copyReplace (indexBaseURL ,
171- index ,
172- []byte ("headlampBaseUrl = './'" ),
173- []byte ("headlampBaseUrl = '" + replaceURL + "'" ),
174- // Replace any resource that has "./" in it
165+ // Replace any resource that has "./" in it
166+ data = bytes .ReplaceAll (
167+ data ,
175168 []byte ("./" ),
176- []byte (baseURL + "/" ))
169+ []byte (fmt .Sprintf ("%s/" , baseURL )),
170+ )
177171
178172 // Insert baseURL in css url() imports, they don't have "./" in them
179- copyReplace (index , index , []byte ("url(" ), []byte ("url(" + baseURL + "/" ), []byte ("" ), []byte ("" ))
173+ data = bytes .ReplaceAll (
174+ data ,
175+ []byte ("url(" ),
176+ []byte (fmt .Sprintf ("url(%s/" , baseURL )),
177+ )
178+
179+ return data
180+ }
181+
182+ // make sure the base-url is updated in the index.html file.
183+ func baseURLReplace (staticDir string , baseURL string ) {
184+ indexBaseURL := path .Join (staticDir , "index.baseUrl.html" )
185+ index := path .Join (staticDir , "index.html" )
186+
187+ // keep a copy of the untouched index.html file as the source for replacements
188+ if ! fileExists (indexBaseURL ) {
189+ d := mustReadFile (index )
190+ mustWriteFile (indexBaseURL , d )
191+ }
192+
193+ // replace baseURL starting from the original copy, incase we run this multiple times
194+ data := mustReadFile (indexBaseURL )
195+ output := makeBaseURLReplacements (data , baseURL )
196+ mustWriteFile (index , output )
180197}
181198
182199func getOidcCallbackURL (r * http.Request , config * HeadlampConfig ) string {
0 commit comments