-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
handler.go
147 lines (127 loc) · 3.11 KB
/
handler.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/gorilla/websocket"
"github.com/russross/blackfriday/v2"
)
type message struct {
Title string `json:"title"`
HTML string `json:"html"`
Lines []int `json:"lines"`
}
func createMessage(html []byte, title string, lines []int) []byte {
msg := message{
Title: title,
HTML: injectAnchor(html),
Lines: lines,
}
bytes, err := json.Marshal(msg)
if err != nil {
logPrintln(err)
return []byte{}
}
return bytes
}
func injectAnchor(html []byte) string {
var out string
for i, str := range strings.Split(string(html), "\n") {
out += fmt.Sprintf("<a id=\"%d\"></a>", i) + str + "\n"
}
return out
}
type markupHandler struct {
initialFileName string
fw *fsnotify.Watcher
subscribers []*websocket.Conn
current []byte
}
// TODO: It should be return err
func newMarkupServer(filename string) *markupHandler {
watcher, err := fsnotify.NewWatcher()
if err != nil {
logPrintln(err)
panic(err)
}
return &markupHandler{initialFileName: filename, fw: watcher}
}
func (m *markupHandler) Close() error {
return m.fw.Close()
}
// TODO: It should be return err
func (m *markupHandler) AddFile(filename string) {
if err := m.fw.Add(filename); err != nil {
logPrintln(err)
panic(err)
}
}
func (m *markupHandler) Start() {
for {
select {
case event := <-m.fw.Events:
logPrintln("event:", event)
result := markdownFileToHTML(event.Name)
lines := diffLines(string(m.current), string(result))
m.current = result
msg := createMessage(result, m.initialFileName, lines)
for i, ws := range m.subscribers {
if err := ws.WriteMessage(websocket.TextMessage, msg); err != nil {
logPrintln("WriteMessage Error:", err)
m.subscribers = append(m.subscribers[:i], m.subscribers[i+1:]...)
}
}
case err := <-m.fw.Errors:
logPrintln("Watch Error:", err)
return
}
}
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
func (m *markupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
logPrintln(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
m.AddFile(m.initialFileName)
result := markdownFileToHTML(m.initialFileName)
m.current = result
msg := createMessage(result, m.initialFileName, []int{})
if err = ws.WriteMessage(websocket.TextMessage, msg); err != nil {
logPrintln(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
m.subscribers = append(m.subscribers, ws)
for {
if _, _, err := ws.ReadMessage(); err != nil {
logPrintln(err)
break
}
}
ws.Close()
}
func markdownFileToHTML(filename string) []byte {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
logPrintln(err)
panic(err)
}
return blackfriday.Run(
bytes,
blackfriday.WithExtensions(blackfriday.CommonExtensions),
blackfriday.WithRenderer(newRenderer(blackfriday.HTMLRendererParameters{
Flags: blackfriday.CommonHTMLFlags,
})),
)
}