MCPcopy Index your code
hub / github.com/kataras/iris / injectLiveReload

Function injectLiveReload

cli.go:31–113  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

29//
30// tryInjectLiveReload runs right before Build -> BuildRouter.
31func 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>")

Callers 1

BuildMethod · 0.85

Calls 12

RecorderMethod · 0.80
NextMethod · 0.80
GetContentTypeMethod · 0.80
DecodeMethod · 0.65
CloseMethod · 0.65
UseRouterMethod · 0.65
BodyMethod · 0.65
SetMethod · 0.65
StatMethod · 0.45
SetBodyMethod · 0.45
WriteMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…