func (s *LiveServer) HandleWebSocket(w http.ResponseWriter, r *http.Request) { conn, err, closed := websocket.Upgrade(w, r, s.websocketOrigin, "") if err != nil { log.StandardError(err) if !closed { serve400(w, r) } return } logRequest(HTTP_WEBSOCKET, http.StatusOK, r) conn.SetReadMaxSize(1 << 20) reads := make(chan []byte, 1) quit := make(chan bool, 1) writes := make(chan []byte, 1) tick := time.NewTicker(pingInterval) go readWebSocket(conn, reads, quit) defer func() { tick.Stop() conn.Close() }() for { select { case <-tick.C: err = conn.WriteControl(websocket.OpPing, pingData, time.Now().Add(pingInterval)) if err != nil { log.Error("websocket: failed on ping: %s", err) return } case read := <-reads: writes <- read case write := <-writes: w, err := conn.NextWriter(websocket.OpText) if err != nil { log.Error("websocket: failed on NextWriter: %s", err) return } n, err := w.Write(write) w.Close() if n != len(write) || err != nil { log.Error("websocket: failed on write: %s", err) return } case <-quit: return } } }
func (r *Repo) Load(callGithub githubCallFunc) error { log.Info("loading repo: %s", r.Path) url := "https://github.com/" + r.Path + "/tarball/master" resp, err := httpClient.Get(url) if err != nil { log.StandardError(err) return err } defer resp.Body.Close() zf, err := gzip.NewReader(resp.Body) if err != nil { log.Error("couldn't find a valid repo tarball at %s -- %s", url, err) return err } tr := tar.NewReader(zf) r.Avatars = map[string]string{} r.Orderings = map[string]*Ordering{} r.Planfiles = map[string]*Planfile{} for { hdr, err := tr.Next() if err == io.EOF { break } if err != nil { log.Error("reading tarball: %s", err) return err } filename, ext := rsplit(hdr.Name, ".") _, filename = rsplit(filename, "/") if ext == "md" || ext == "order" { log.Info("parsing: %s", filename) data, err := ioutil.ReadAll(tr) if err != nil { log.Error("reading tarball file %q: %s", hdr.Name, err) continue } if ext == "md" { r.AddPlanfile(filename+".md", data, callGithub) } else { r.AddOrdering(filename+".order", data) } } } log.Info("successfully loaded repo: %s", r.Path) return nil }
func StandardError(err error) { log.StandardError(err) Exit(1) }
func main() { opts := optparse.Parser("Usage: html2domly [options]", "v") outputFile := opts.String([]string{"-o", "--output"}, "../coffee/templates.coffee", "coffeescript file to compile to", "PATH") templatesSrcDir := opts.String([]string{"-i", "--input"}, "../etc/domly", "template source directory", "PATH") printJSON := opts.Bool([]string{"--print"}, false, "Print the JSON nicely to the output logger") os.Args[0] = "html2domly" opts.Parse(os.Args) log.AddConsoleLogger() var ( data []byte err error prettyStr string basename string pretty bytes.Buffer ) dir, err := os.Open(*templatesSrcDir) if err != nil { runtime.StandardError(err) } defer dir.Close() out, err := os.Create(*outputFile) if err != nil { runtime.StandardError(err) } defer out.Close() names, err := dir.Readdirnames(0) if err != nil { runtime.StandardError(err) } out.Write([]byte("define 'templates', (exports, root) ->\n")) for _, name := range names { if strings.HasSuffix(name, ".html") { basename = strings.TrimSuffix(name, ".html") } else { log.Error("file %v does not end in .html", name) continue } templatePath := filepath.Join(*templatesSrcDir, name) data, err = ui.ParseTemplate(templatePath) if err != nil { log.StandardError(err) } else { err := json.Indent(&pretty, data, ">", " ") if err != nil { log.StandardError(err) log.Info("%v", data) } else if *printJSON { prettyStr = pretty.String() pretty.Reset() log.Info("%v", prettyStr) } out.Write([]byte(fmt.Sprintf(" exports['%s'] = `%s`\n", basename, data))) log.Info("compiled '%s'", name) } } out.Write([]byte(" return")) outPath, _ := filepath.Abs(*outputFile) log.Info("compiled domly written to: %v", outPath) log.Wait() }