websockets talk
This commit is contained in:
parent
8900500b35
commit
429ba99406
2 changed files with 47 additions and 0 deletions
21
livemd.go
21
livemd.go
|
|
@ -11,6 +11,7 @@ import (
|
|||
"text/template"
|
||||
|
||||
"github.com/russross/blackfriday"
|
||||
"golang.org/x/net/websocket"
|
||||
fsnotify "gopkg.in/fsnotify.v1"
|
||||
)
|
||||
|
||||
|
|
@ -19,6 +20,7 @@ var SUFFIXES = [3]string{".md", ".mkd", ".markdown"}
|
|||
var toc []string
|
||||
var tocMutex sync.Mutex
|
||||
var rootTmpl *template.Template
|
||||
var pageTmpl *template.Template
|
||||
var path string
|
||||
|
||||
func init() {
|
||||
|
|
@ -27,6 +29,10 @@ func init() {
|
|||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
pageTmpl, err = template.New("page").Parse(pageTemplate)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func HasMarkdownSuffix(s string) bool {
|
||||
|
|
@ -65,6 +71,8 @@ func WatcherEventLoop(w *fsnotify.Watcher, done chan bool) {
|
|||
log.Println("Event:", event)
|
||||
// TODO(barakmich): On directory creation, stat path if directory, and watch it.
|
||||
if HasMarkdownSuffix(event.Name) {
|
||||
if event.Op == fsnotify.Write {
|
||||
}
|
||||
}
|
||||
|
||||
case err := <-w.Errors:
|
||||
|
|
@ -87,6 +95,17 @@ func RootFunc(w http.ResponseWriter, r *http.Request) {
|
|||
rootTmpl.Execute(w, string(bytes))
|
||||
}
|
||||
|
||||
func PageFunc(w http.ResponseWriter, r *http.Request) {
|
||||
subpath := strings.TrimPrefix(r.RequestURI, "/md")
|
||||
log.Println("New watcher on ", subpath)
|
||||
pageTmpl.Execute(w, subpath)
|
||||
}
|
||||
|
||||
func HandleListener(ws *websocket.Conn) {
|
||||
fmt.Println("WEBSOCKET!", ws.Request().RequestURI)
|
||||
ws.Close()
|
||||
}
|
||||
|
||||
func main() {
|
||||
path = os.Getenv("PWD")
|
||||
if len(os.Args) > 1 {
|
||||
|
|
@ -111,5 +130,7 @@ func main() {
|
|||
fmt.Println(toc)
|
||||
|
||||
http.HandleFunc("/", RootFunc)
|
||||
http.HandleFunc("/md/", PageFunc)
|
||||
http.Handle("/ws/", websocket.Handler(HandleListener))
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue