serving websocket connections again
3 files changed, 42 insertions(+), 33 deletions(-)

M main.go
M resources.go
M server.go
M main.go +1 -1
@@ 10,7 10,7 @@ func main() {
     if err != nil {
         log.Fatal(err)
     }
-    log.Println("Loaded goquitur, beginning to serve webstuffs")
+    log.Println("Loaded goquitur, beginning to serve webstuffs, port 8123")
     if err := http.ListenAndServe(":8123", web); err != nil {
         log.Fatal(err)
     }

          
M resources.go +1 -1
@@ 34,7 34,7 @@ type Error struct {
 type Message struct {
     Event       string      `json:",omitempty"`
     //URL         string <- todo, do this
-    Path        string
+    Path        WebPath
     Payload     interface{} `json:",omitempty"`
 }
 

          
M server.go +40 -31
@@ 20,9 20,10 @@ type GoquiturHandler struct {
     *http.ServeMux
     watchdir        string
     watcher         *fsnotify.Watcher
-    maplock         *sync.Mutex
-    urlmap          map[string][]*websocket.Conn
-    submap          map[*websocket.Conn][]string
+    sublock         *sync.Mutex
+    urlsubs          map[WebPath][]*websocket.Conn
+    // what was I supposed to be using this for again?
+    suburls          map[*websocket.Conn][]WebPath
 }
 
 func NewGoquiturHandler(watchdir string) (*GoquiturHandler, error) {

          
@@ 43,12 44,12 @@ func NewGoquiturHandler(watchdir string)
         ServeMux: http.NewServeMux(),
         watchdir: watchdir,
         watcher: watcher,
-        maplock: new(sync.Mutex),
-        urlmap: make(map[string][]*websocket.Conn),
-        submap: make(map[*websocket.Conn][]string),
+        sublock: new(sync.Mutex),
+        urlsubs: make(map[WebPath][]*websocket.Conn),
+        suburls: make(map[*websocket.Conn][]WebPath),
     }
     h.ServeMux.Handle("/", http.FileServer(http.Dir("static")))
-    //h.ServeMux.Handle("/ws", websocket.Handler(h.serveWS))
+    h.ServeMux.Handle("/ws", websocket.Handler(h.serveWS))
 
     evchan, errchan := processWatcher(h.watcher)
 

          
@@ 66,7 67,12 @@ func (h *GoquiturHandler) ValidatePath(p
     return ValidFSPath(path), nil
 }
 
-func (h *GoquiturHandler) locationFromPath(path FSPath) (*Location, error) {
+func (h *GoquiturHandler) LocationFromWebPath(path WebPath) (*Location, error) {
+    fspath := filepath.Join(h.watchdir, string(path))
+    return &Location{path, FSPath(fspath)}, nil
+}
+
+func (h *GoquiturHandler) LocationFromFilePath(path FSPath) (*Location, error) {
     webpath, err := filepath.Rel(h.watchdir, string(path))
     if err != nil {
         return nil, err

          
@@ 75,9 81,9 @@ func (h *GoquiturHandler) locationFromPa
 }
 
 func (h *GoquiturHandler) Subscribers(path WebPath) []*websocket.Conn {
-    h.maplock.Lock()
-    defer h.maplock.Unlock()
-    if v, ok := h.urlmap[string(path)]; ok == true {
+    h.sublock.Lock()
+    defer h.sublock.Unlock()
+    if v, ok := h.urlsubs[path]; ok == true {
         return v
     }
     return make([]*websocket.Conn, 0)

          
@@ 95,7 101,7 @@ func (h *GoquiturHandler) handleEvents(e
                 log.Println("Error validating event:", err)
                 continue
             }
-            loc, err := h.locationFromPath(fspath)
+            loc, err := h.LocationFromFilePath(fspath)
             if err != nil {
                 log.Println("Error handling fs event:", err)
                 continue

          
@@ 122,7 128,7 @@ func (h *GoquiturHandler) handleEvents(e
                         }
                         msg := &Message{
                             Event: ev.Type,
-                            Path: string(loc.WebPath),
+                            Path: loc.WebPath,
                             Payload: payload,
                         }
                         data, err = json.Marshal(msg)

          
@@ 149,9 155,6 @@ func (h *GoquiturHandler) handleEvents(e
     }
 }
 
-////////////////////////////////////////////////////////////////////////////////
-/*
-
 func (h *GoquiturHandler) serveWS(ws *websocket.Conn) {
     defer ws.Close()
     for {

          
@@ 161,6 164,7 @@ func (h *GoquiturHandler) serveWS(ws *we
             break
         }
     }
+    // TODO remove connection
 }
 
 func (h *GoquiturHandler) serveOneWS(ws *websocket.Conn) error {

          
@@ 174,25 178,23 @@ func (h *GoquiturHandler) serveOneWS(ws 
 
     log.Println("request:", r)
 
-    res, err := h.resFromWebPath(r.Path)
-    if err == nil {
-        err = h.validateResource(res)
-    }
+    loc, err := h.LocationFromWebPath(WebPath(r.Path))
     if err != nil {
         return websocket.JSON.Send(ws, &Error{
-            Message: "Invalid path in request",
+            Message: "Unable to resolve location",
             Details: err.Error(),
         })
     }
-    // Open the resource
-    ores, err := h.openResource(res)
+    validpath, err := h.ValidatePath(loc.FSPath)
     if err != nil {
         return websocket.JSON.Send(ws, &Error{
-            Message: "Could not read resource",
+            Message: "Invalid path",
             Details: err.Error(),
         })
     }
-    payload, err := ores.Payload()
+
+    // Get the payload
+    payload, err := getPayload(validpath)
     if err != nil {
         return websocket.JSON.Send(ws, &Error{
             Message: "Could not read resource",

          
@@ 201,17 203,24 @@ func (h *GoquiturHandler) serveOneWS(ws 
     }
 
     // Subscribe to the resource
-    subs, ok := h.subs[res.FSPath]
+    h.sublock.Lock()
+    defer h.sublock.Unlock()
+
+    subs, ok := h.urlsubs[loc.WebPath]
     if ok == false {
         subs = make([]*websocket.Conn, 0)
     }
-    subs = append(subs, ws)
-    h.subs[res.WebPath] = subs
+    h.urlsubs[loc.WebPath] = append(subs, ws)
+
+    urls, ok := h.suburls[ws]
+    if ok == false {
+        urls = make([]WebPath, 0)
+    }
+    h.suburls[ws] = append(urls, loc.WebPath)
 
     // Send the resource
-    return websocket.JSON.Send(ws, &State{
-        Path: res.WebPath,
+    return websocket.JSON.Send(ws, &Message{
+        Path: loc.WebPath,
         Payload: payload,
     })
 }
-*/