forked from piranha/gostatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
187 lines (155 loc) · 3.71 KB
/
core.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// This functionality is made by looking at source of devd:
// https://github.com/cortesi/devd/blob/master/livereload/livereload.go
// which is copyrighted under MIT by Aldo Cortesi
package hotreload
import (
"net/http"
"time"
"sync"
"fmt"
"strings"
"regexp"
"github.com/gorilla/websocket"
)
const (
// EndpointPath is a path to websocket endpoint
EndpointPath = "/.gostatic.hotreload"
// ScriptPath is a path to hotreload support js
ScriptPath = "/.gostatic.hotreload.js"
)
type Server struct {
sync.Mutex
conns map[*websocket.Conn]bool
filemods <-chan string
dirs []string
files []string
}
func NewServer(path string) (*Server, error) {
filemods, err := fileWatcher([]string{path}, []string{})
if err != nil {
return nil, err
}
s := &Server{
conns: make(map[*websocket.Conn]bool),
filemods: filemods,
}
go s.run()
return s, nil
}
func (s *Server) run() {
for {
fns := []string{}
fns = append(fns, <-s.filemods)
time.Sleep(16 * time.Millisecond)
for len(s.filemods) > 0 {
fns = append(fns, <-s.filemods)
}
cmd := "css"
for _, fn := range fns {
if !strings.HasSuffix(fn, ".css") {
cmd = "page"
}
}
s.Lock()
for conn := range s.conns {
if conn == nil {
continue
}
err := conn.WriteMessage(websocket.TextMessage, []byte(cmd))
if err != nil {
fmt.Errorf("error: %s", err)
delete(s.conns, conn)
}
}
s.Unlock()
}
s.Lock()
for conn := range s.conns {
delete(s.conns, conn)
conn.Close()
}
s.Unlock()
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool { return true },
}
func (s *Server) Upgrade(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Errorf("can't establish WebSocket connection: %s", err)
http.Error(w, "Can't upgrade.", 500)
return
}
s.Lock()
s.conns[conn] = true
s.Unlock()
}
type Injector struct {
http.ResponseWriter
}
var htmlRe = regexp.MustCompile("\btext/html\b")
var injectorRe = regexp.MustCompile(`<\/head>`)
var payload = []byte(`<script src="/.gostatic.hotreload.js" async></script></head>`)
func (inj *Injector) WriteHeader(statusCode int) {
inj.ResponseWriter.Header().Del("Content-Length")
inj.ResponseWriter.WriteHeader(statusCode)
}
func (inj *Injector) Write(data []byte) (int, error) {
ctypes, ok := inj.ResponseWriter.Header()["Content-Type"]
isHTML := false
if ok {
for _, ctype := range ctypes {
if strings.HasPrefix(ctype, "text/html") {
isHTML = true
}
}
}
if isHTML {
data = injectorRe.ReplaceAllLiteral(data, payload)
}
return inj.ResponseWriter.Write(data)
}
// ServeHTTP serves files from `dir` and adds hotreload support to html files
func ServeHTTP(source, port string, hotreload bool) error {
server, err := NewServer(source)
if err != nil {
return err
}
http.HandleFunc(EndpointPath, server.Upgrade)
http.HandleFunc(ScriptPath, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/javascript")
w.Header().Set("Cache-Control", "no-store")
w.Write(Script)
})
fs := http.FileServer(http.Dir(source))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
if hotreload {
injector := Injector{w}
fs.ServeHTTP(&injector, r)
} else {
fs.ServeHTTP(w, r)
}
})
return http.ListenAndServe(":"+port, nil)
}
// Watch starts file watcher
func Watch(dirs, files []string, callback func()) error {
filemods, err := fileWatcher(dirs, files)
if err != nil {
return err
}
go func() {
for {
<-filemods
time.Sleep(10 * time.Millisecond)
for len(filemods) > 0 {
<-filemods
}
callback()
}
}()
return nil
}