This repository has been archived by the owner on Aug 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cssmate.go
executable file
·142 lines (111 loc) · 2.86 KB
/
cssmate.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
package main
import (
"log"
"fmt"
"regexp"
"strings"
"net/http"
"encoding/json"
"github.com/go-martini/martini"
"github.com/howeyc/fsnotify"
)
var m *martini.ClassicMartini
func setupCssMate(p *int, hs *string, path *string) {
folder = *path
host = *hs
port = *p
bind = fmt.Sprintf("%s:%d", host, *p)
// assign folder and port
c := make(chan bool)
go h.run()
go func() {
setupMartini()
c <- true
}()
// here we setup martini
setupWatcher()
// here we can setup fsnotify without worrying about martini blocking us
<-c
// wait till martini is done
}
func setupMartini() {
m = martini.Classic()
m.Get("/websocket", wsHandler)
m.Get("/cssmate.js", jsHandler)
fmt.Println("[martini] listening on", bind)
log.Fatal(http.ListenAndServe(bind, m))
}
func setupWatcher() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
// if there is any errors back out
done := make(chan bool)
go func() {
for {
select {
case ev := <-watcher.Event:
if ev.IsModify() {
fileModified(ev)
}
case err := <-watcher.Error:
fmt.Println("error:", err)
}
}
}()
// setup the event loop to keep us going
err = watcher.Watch(folder)
if err != nil {
log.Fatal(err)
}
// setup the watcher and wait for errors
fmt.Println("[watcher] now watching for changes in", folder)
<-done
// wait till we're done
watcher.Close()
}
func wsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
// allow cross domain
ws, err := upgrader.Upgrade(w, r, w.Header())
if err != nil {
return
}
c := &connection{send: make(chan []byte, 256), websocket: ws}
h.register <- c
defer func() {
h.unregister <- c
}()
go c.writer()
c.reader()
}
func jsHandler(w http.ResponseWriter, r *http.Request) (int, string) {
data, err := Asset("embed/cssmate.js")
if err != nil || len(data) == 0 {
return 404, "404 page not found"
}
output := string(data)
hostRegex := regexp.MustCompile("\\/\\* inject\\:host \\*\\/")
portRegex := regexp.MustCompile("\\/\\* inject\\:port \\*\\/")
output = hostRegex.ReplaceAllLiteralString(output, fmt.Sprintf("'%s'", host))
output = portRegex.ReplaceAllLiteralString(output, fmt.Sprintf("%d", port))
// inject the config vars
return 200, output
}
func fileModified(ev *fsnotify.FileEvent) {
fn := strings.Replace(ev.Name, folder, "", 1)[1:]
for v := range h.connections {
if stringInSlice(fn, v.files) {
fmt.Println("[watcher] reloading", fn, "notifying clients")
var out message = message{"changed", fn}
output, jerr := json.Marshal(out)
if jerr != nil {
fmt.Println(jerr)
return
}
v.send <- output
// create a message to send to the client to tell them to reload x file
}
}
}