func entriesGet(c *app.Context) { id := c.Seg(2) s, entry := api.EntryGet(c, id) if s != 200 { c.Error(s) return } c.JSON(entry) }
func entriesID(c *app.Context) { if c.Seg(2) == "random" { entriesRandom(c) return } switch c.R.Method { case "GET": entriesGet(c) return } c.Error(404) }
func socket(c *app.Context) { ws, err := grader.Upgrade(c.W, c.R, nil) defer ws.Close() if err != nil { return } slug := c.Seg(2) coll := c.DB.C("entries") var entry models.Entry if _, ok := hubs[slug]; !ok { hubs[slug] = NewHub() } hub := hubs[slug] cli := hub.Add(ws) go cli.Ping(30 * time.Second) for { if err = ws.ReadJSON(&entry); err != nil { break } coll.Update(bson.M{ "slug": slug, }, bson.M{ "$set": entry, }) for ws, _ := range hub.clients { ws.WriteJSON(entry) } } if len(hub.clients) == 0 { delete(hubs, slug) } hub.Remove(ws) }