# HG changeset patch # User sqwishy # Date 1385542521 28800 # Wed Nov 27 00:55:21 2013 -0800 # Node ID 0a96ceefae55b8d79a719f470c17816ddc0f9159 # Parent 717587fdfd3ba4e2d4349fd3544923b9cc5a45fe serving websocket connections again diff --git a/main.go b/main.go --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ 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) } diff --git a/resources.go b/resources.go --- a/resources.go +++ b/resources.go @@ -34,7 +34,7 @@ type Message struct { Event string `json:",omitempty"` //URL string <- todo, do this - Path string + Path WebPath Payload interface{} `json:",omitempty"` } diff --git a/server.go b/server.go --- a/server.go +++ b/server.go @@ -20,9 +20,10 @@ *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 @@ 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 @@ 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) 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 @@ 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 @@ } 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) serveWS(ws *websocket.Conn) { defer ws.Close() for { @@ -161,6 +164,7 @@ break } } + // TODO remove connection } func (h *GoquiturHandler) serveOneWS(ws *websocket.Conn) error { @@ -174,25 +178,23 @@ 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 @@ } // 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, }) } -*/