injectLiveReload tries to check if this application runs under https://github.com/kataras/iris-cli and if so then it checks if the livereload is enabled and then injects the watch listener (js script) on every HTML response. It has a slight performance cost but this (iris-cli with watch and liverelo
(r Party)
| 29 | // |
| 30 | // tryInjectLiveReload runs right before Build -> BuildRouter. |
| 31 | func injectLiveReload(r Party) (bool, error) { |
| 32 | conf := struct { |
| 33 | Running bool `yaml:"Running,omitempty"` |
| 34 | LiveReload struct { |
| 35 | Disable bool `yaml:"Disable"` |
| 36 | Port int `yaml:"Port"` |
| 37 | } `yaml:"LiveReload"` |
| 38 | }{} |
| 39 | // defaults to disabled here. |
| 40 | conf.LiveReload.Disable = true |
| 41 | |
| 42 | wd, err := os.Getwd() |
| 43 | if err != nil { |
| 44 | return false, err |
| 45 | } |
| 46 | |
| 47 | for _, path := range []string{".iris.yml" /*, "../.iris.yml", "../../.iris.yml" */} { |
| 48 | path = filepath.Join(wd, path) |
| 49 | |
| 50 | if _, err := os.Stat(path); err == nil { |
| 51 | inFile, err := os.OpenFile(path, os.O_RDONLY, 0600) |
| 52 | if err != nil { |
| 53 | return false, err |
| 54 | } |
| 55 | |
| 56 | dec := yaml.NewDecoder(inFile) |
| 57 | err = dec.Decode(&conf) |
| 58 | inFile.Close() |
| 59 | if err != nil { |
| 60 | return false, err |
| 61 | } |
| 62 | |
| 63 | break |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if !conf.Running || conf.LiveReload.Disable { |
| 68 | return false, nil |
| 69 | } |
| 70 | |
| 71 | scriptReloadJS := []byte(fmt.Sprintf(`<script>(function () { |
| 72 | const scheme = document.location.protocol == "https:" ? "wss" : "ws"; |
| 73 | const endpoint = scheme + "://" + document.location.hostname + ":%d/livereload"; |
| 74 | |
| 75 | w = new WebSocket(endpoint); |
| 76 | w.onopen = function () { |
| 77 | console.info("LiveReload: initialization"); |
| 78 | }; |
| 79 | w.onclose = function () { |
| 80 | console.info("LiveReload: terminated"); |
| 81 | }; |
| 82 | w.onmessage = function (message) { |
| 83 | // NOTE: full-reload, at least for the moment. Also if backend changed its port then we will get 404 here. |
| 84 | window.location.reload(); |
| 85 | }; |
| 86 | }());</script>`, conf.LiveReload.Port)) |
| 87 | |
| 88 | bodyCloseTag := []byte("</body>") |
no test coverage detected
searching dependent graphs…