-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
281 lines (250 loc) · 6.33 KB
/
main.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// main.go is a dual-function program
// go run main.go will generate the index.html
// go build; ./now will run a live-reload server
//go:generate go run data/embed.go
package main
import (
"bytes"
"flag"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"github.com/gorilla/websocket"
log "github.com/schollz/logger"
"github.com/shurcooL/github_flavored_markdown"
)
var folderToWatch, indexPage string
var port int
var renderMarkdown bool
func main() {
var debug bool
flag.IntVar(&port, "p", 8003, "port to serve")
flag.BoolVar(&debug, "debug", false, "debug mode")
flag.BoolVar(&renderMarkdown, "style", false, "whether to add default styling to render markdown")
flag.StringVar(&indexPage, "index", "index.html", "index page to render on /")
flag.StringVar(&folderToWatch, "f", "", "folder to watch (default: current)")
flag.Parse()
if folderToWatch == "" {
folderToWatch = "."
}
if filepath.Ext(indexPage) == ".md" {
renderMarkdown = true
}
if debug {
log.SetLevel("debug")
} else {
log.SetLevel("info")
}
var err error
err = serve()
if err != nil {
log.Error(err)
}
}
func MarkdownToHTML(fname string) template.HTML {
markdown, err := ioutil.ReadFile(fname)
if err != nil {
return template.HTML(err.Error())
}
html := github_flavored_markdown.Markdown([]byte(markdown))
return template.HTML(string(html))
}
var funcMap template.FuncMap
func init() {
funcMap = template.FuncMap{
"MarkdownToHTML": MarkdownToHTML,
}
}
func serve() (err error) {
go watchFileSystem()
log.Infof("listening on :%d", port)
http.HandleFunc("/", handler)
return http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
t := time.Now().UTC()
err := handle(w, r)
if err != nil {
log.Error(err)
}
log.Infof("%v %v %v %s\n", r.RemoteAddr, r.Method, r.URL.Path, time.Since(t))
}
func handle(w http.ResponseWriter, r *http.Request) (err error) {
// very special paths
if r.URL.Path == "/robots.txt" {
// special path
w.Write([]byte(`User-agent: *
Disallow: /`))
} else if r.URL.Path == "/favicon.ico" {
// TODO
} else if r.URL.Path == "/sitemap.xml" {
// TODO
} else if r.URL.Path == "/ws" {
// special path /ws
return handleWebsocket(w, r)
} else if r.URL.Path == "/"+jsFile {
w.Header().Set("Content-Type", "text/javascript")
w.Write([]byte(js))
} else {
if r.URL.Path == "/" {
r.URL.Path = "/" + indexPage
}
urlPath := r.URL.Path
var b []byte
b, err = ioutil.ReadFile(path.Join(folderToWatch, path.Clean(r.URL.Path[1:])))
if err != nil {
// try to see if index is nested
b, err = ioutil.ReadFile(path.Join(folderToWatch, path.Clean(r.URL.Path[1:]), "index.html"))
if err != nil {
err = fmt.Errorf("could not find file")
return
} else {
urlPath = path.Join(path.Clean(r.URL.Path[1:]), "index.html")
}
}
var kind string
if len(b) > 512 {
kind = http.DetectContentType(b)
} else {
kind = http.DetectContentType(b[:512])
}
if strings.HasPrefix(kind, "application/octet-stream") || strings.HasPrefix(kind, "text/plain") {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
switch filepath.Ext(urlPath) {
case ".js":
kind = "text/javascript"
case ".css":
kind = "text/css"
case ".md":
kind = "text/plain"
case ".html":
kind = "text/html"
}
}
if strings.HasPrefix(kind, "text/html") {
mainTemplate, errTemplate := template.New("main").Funcs(funcMap).Parse(string(b))
if errTemplate == nil {
var buf bytes.Buffer
err = mainTemplate.Execute(&buf, nil)
b = buf.Bytes()
} else {
log.Warn("problem as template: ", errTemplate)
}
}
if renderMarkdown {
b = []byte(strings.Replace(defaultHTML, "XX", string(MarkdownToHTML(r.URL.Path[1:])), 1))
kind = "text/html"
}
if strings.HasPrefix(kind, "text/html") {
b = bytes.Replace(b,
[]byte("</body>"),
[]byte(fmt.Sprintf(`<script src="/%s"></script></body>`, jsFile)),
1,
)
}
w.Header().Set("Content-Type", kind)
w.Write(b)
}
return
}
var wsupgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
type Payload struct {
Message string `json:"message"`
}
type Connections struct {
cs map[string]*websocket.Conn
sync.RWMutex
}
var wsConnections Connections
func handleWebsocket(w http.ResponseWriter, r *http.Request) (err error) {
// handle websockets on this page
c, errUpgrade := wsupgrader.Upgrade(w, r, nil)
if errUpgrade != nil {
return errUpgrade
}
defer c.Close()
log.Debugf("%s connected\n", c.RemoteAddr().String())
wsConnections.Lock()
if len(wsConnections.cs) == 0 {
wsConnections.cs = make(map[string]*websocket.Conn)
}
wsConnections.cs[c.RemoteAddr().String()] = c
wsConnections.Unlock()
defer func() {
wsConnections.Lock()
delete(wsConnections.cs, c.RemoteAddr().String())
wsConnections.Unlock()
}()
var p Payload
for {
err := c.ReadJSON(&p)
if err != nil {
log.Debug("read:", err)
break
}
log.Debugf("recv: %v", p)
}
return
}
func watchFileSystem() (err error) {
// creates a new file watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
lastEvent := time.Now()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
_, fname := filepath.Split(event.Name)
log.Debugf("modified file: %s", fname)
if time.Since(lastEvent).Nanoseconds() > (50*time.Millisecond).Nanoseconds() && !strings.HasPrefix(fname, ".") && !strings.HasSuffix(fname, "~") {
lastEvent = time.Now()
log.Infof("reloading after %s", strings.ToLower(event.Op.String()))
wsConnections.Lock()
for c := range wsConnections.cs {
wsConnections.cs[c].WriteJSON(Payload{Message: "reload"})
}
wsConnections.Unlock()
}
// if event.Op&fsnotify.Write == fsnotify.Write {
// log.Println("modified file:", event.Name)
// }
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Error("error:", err)
}
}
}()
filepath.Walk(folderToWatch, func(path string, fi os.FileInfo, err error) error {
if fi.Mode().IsDir() {
log.Debugf("watching %s", path)
return watcher.Add(path)
}
return nil
})
<-done
return
}