forked from jaschaephraim/lrserver
/
server.go
189 lines (157 loc) · 3.74 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
package lrserver
import (
"fmt"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"github.com/gorilla/websocket"
)
type Server struct {
name string
host string
port uint16
server *http.Server
conns connSet
js string
statusLog *log.Logger
liveCSS bool
}
// New ...
func New(name string, host string, port uint16) (*Server, error) {
// Create router
router := http.NewServeMux()
logPrefix := "[" + name + "] "
// Create server
s := &Server{
name: name,
host: host,
port: port,
server: &http.Server{
Handler: router,
ErrorLog: log.New(os.Stderr, logPrefix, 0),
},
conns: make(connSet),
statusLog: log.New(os.Stdout, logPrefix, 0),
liveCSS: true,
}
// Handle JS
router.HandleFunc("/livereload.js", jsHandler(s))
// Handle reload requests
router.HandleFunc("/livereload", webSocketHandler(s))
return s, nil
}
func (s *Server) ListenAndServe() error {
// Create listener
l, err := net.Listen("tcp", s.Addr())
if err != nil {
return err
}
// Set assigned port if necessary
if s.port == 0 {
addr := strings.Split(l.Addr().String(), ":")
port, _ := strconv.ParseUint(addr[1], 10, 16)
s.host, s.port = addr[0], uint16(port)
}
s.js = fmt.Sprintf(js, s.host, s.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.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.conns {
conn.alertChan <- msg
}
}
// Name gets the server name
func (s *Server) Name() string {
return s.name
}
// Addr get the host:port that the server is listening on
func (s *Server) Addr() string {
return fmt.Sprintf("%s:%d", s.Host(), s.Port())
}
// Host gets the host that the server is listening on
func (s *Server) Host() string {
return s.host
}
// Port gets the port that the server is listening on
func (s *Server) Port() uint16 {
return s.port
}
// 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) 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.conns.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
}