buildIndexHTML reads index.html and injects the initial config as a script tag.
(staticFS fs.FS)
| 197 | |
| 198 | // buildIndexHTML reads index.html and injects the initial config as a script tag. |
| 199 | func (s *Server) buildIndexHTML(staticFS fs.FS) []byte { |
| 200 | raw, err := fs.ReadFile(staticFS, "index.html") |
| 201 | if err != nil { |
| 202 | return []byte("<!-- failed to read index.html -->") |
| 203 | } |
| 204 | |
| 205 | phases := s.config.Phases |
| 206 | if phases == nil { |
| 207 | phases = []PhaseInfo{} |
| 208 | } |
| 209 | cfg := ConfigResponse{ |
| 210 | ReadOnly: s.config.ReadOnly, |
| 211 | Version: s.config.Version, |
| 212 | Phases: phases, |
| 213 | } |
| 214 | cfgJSON, err := json.Marshal(cfg) |
| 215 | if err != nil { |
| 216 | return raw |
| 217 | } |
| 218 | |
| 219 | // Inject right before </head> |
| 220 | script := fmt.Sprintf(`<script>window.__TASKMD_CONFIG__=%s;</script>`, cfgJSON) |
| 221 | return bytes.Replace(raw, []byte("</head>"), []byte(script+"</head>"), 1) |
| 222 | } |
| 223 | |
| 224 | func (s *Server) mountFallback(mux *http.ServeMux) { |
| 225 | mux.HandleFunc("/{path...}", func(w http.ResponseWriter, r *http.Request) { |