-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.go
211 lines (175 loc) · 4 KB
/
server.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package lrserver
import (
"context"
_ "embed"
"fmt"
"log"
"net"
"net/http"
"os"
"strconv"
"github.com/gorilla/websocket"
)
//go:embed livereload_v4.0.2/livereload.min.js
var JS string
// Server contains a single lrserver instance's data
type Server struct {
name string
port uint16
server *http.Server
connSet *connSet
statusLog *log.Logger
liveCSS bool
}
// New creates a new Server instance
func New(name string, port uint16) *Server {
// Create router
router := http.NewServeMux()
logPrefix := fmt.Sprintf("[%s]", name)
// Create server
s := &Server{
name: name,
server: &http.Server{
Handler: router,
ErrorLog: log.New(os.Stderr, logPrefix, 0),
},
connSet: &connSet{conns: make(map[*conn]struct{})},
statusLog: log.New(os.Stdout, logPrefix, 0),
liveCSS: true,
}
s.setPort(port)
// Handle JS
router.HandleFunc("/livereload.js", jsHandler(s))
// Handle reload requests
router.HandleFunc("/livereload", webSocketHandler(s))
return s
}
func (s *Server) Close() error {
if s.server != nil {
return s.server.Close()
}
return nil
}
func (s *Server) Shutdown(ctx context.Context) error {
if s.server != nil {
return s.server.Shutdown(ctx)
}
return nil
}
func (s *Server) ListenAndServe() error {
// Create listener
l, err := net.Listen("tcp", makeAddr(s.port))
if err != nil {
return err
}
// Set assigned port if necessary
if s.port == 0 {
port, err := makePort(l.Addr().String())
if err != nil {
return err
}
s.setPort(port)
}
s.logStatus("listening on " + s.server.Addr)
return s.server.Serve(l)
}
// Reload sends a reload message to the client
func (s *Server) Reload(file string) {
s.logStatus("requesting reload: " + file)
for conn := range s.connSet.conns {
conn.reloadChan <- file
}
}
// Alert sends an alert message to the client
func (s *Server) Alert(msg string) {
s.logStatus("requesting alert: " + msg)
for conn := range s.connSet.conns {
conn.alertChan <- msg
}
}
// Name gets the server name
func (s *Server) Name() string {
return s.name
}
// Port gets the port that the server is listening on
func (s *Server) Port() uint16 {
return s.port
}
func (s *Server) IsConnected() bool {
for conn := range s.connSet.conns {
if conn.handshake {
return true
}
}
return false
}
// LiveCSS gets the live CSS preference
func (s *Server) LiveCSS() bool {
return s.liveCSS
}
// StatusLog gets the server's status logger,
// which writes to os.Stdout by default
func (s *Server) StatusLog() *log.Logger {
return s.statusLog
}
// ErrorLog gets the server's error logger,
// which writes to os.Stderr by default
func (s *Server) ErrorLog() *log.Logger {
return s.server.ErrorLog
}
// SetLiveCSS sets the live CSS preference
func (s *Server) SetLiveCSS(n bool) {
s.liveCSS = n
}
// SetStatusLog sets the server's status logger,
// which can be set to nil
func (s *Server) SetStatusLog(l *log.Logger) {
s.statusLog = l
}
// SetErrorLog sets the server's error logger,
// which can be set to nil
func (s *Server) SetErrorLog(l *log.Logger) {
s.server.ErrorLog = l
}
func (s *Server) setPort(port uint16) {
s.port = port
s.server.Addr = makeAddr(port)
}
func (s *Server) newConn(wsConn *websocket.Conn) {
c := &conn{
conn: wsConn,
server: s,
handshake: false,
reloadChan: make(chan string),
alertChan: make(chan string),
closeChan: make(chan closeSignal),
}
s.connSet.add(c)
go c.start()
}
func (s *Server) logStatus(msg ...interface{}) {
if s.statusLog != nil {
s.statusLog.Println(msg...)
}
}
func (s *Server) logError(msg ...interface{}) {
if s.server.ErrorLog != nil {
s.server.ErrorLog.Println(msg...)
}
}
// makeAddr converts uint16(x) to ":x"
func makeAddr(port uint16) string {
return fmt.Sprintf(":%d", port)
}
// makePort converts ":x" to uint16(x)
func makePort(addr string) (uint16, error) {
_, portString, err := net.SplitHostPort(addr)
if err != nil {
return 0, err
}
port64, err := strconv.ParseUint(portString, 10, 16)
if err != nil {
return 0, err
}
return uint16(port64), nil
}