func NewHttp() *Http { return &Http{ Txns: util.NewBroadcast(), reqGauge: metrics.NewGauge(), reqMeter: metrics.NewMeter(), reqTimer: metrics.NewTimer(), } }
func NewController() *Controller { ctl := &Controller{ Updates: util.NewBroadcast(), Cmds: make(chan Command), Wait: new(sync.WaitGroup), Shutdown: make(chan int), } return ctl }
// public interface func NewController() *Controller { ctl := &Controller{ Logger: log.NewPrefixLogger("controller"), updates: util.NewBroadcast(), cmds: make(chan command), views: make([]mvc.View, 0), state: make(chan mvc.State), } return ctl }
func NewWebView(ctl *ui.Controller, state ui.State, port int) *WebView { v := &WebView{ wsMessages: util.NewBroadcast(), } switch p := state.GetProtocol().(type) { case *proto.Http: NewWebHttpView(v, ctl, p) } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/http/in", 302) }) http.HandleFunc("/_ws", func(w http.ResponseWriter, r *http.Request) { conn, err := websocket.Upgrade(w, r.Header, nil, 1024, 1024) if err != nil { http.Error(w, "Failed websocket upgrade", 400) log.Warn("Failed websocket upgrade: %v", err) return } msgs := v.wsMessages.Reg() defer v.wsMessages.UnReg(msgs) for m := range msgs { err := conn.WriteMessage(websocket.OpText, m.([]byte)) if err != nil { // connection is closed break } } }) http.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) { parts := strings.Split(r.URL.Path, "/") name := parts[len(parts)-1] fn, ok := static.AssetMap[name] if !ok { http.NotFound(w, r) return } w.Write(fn()) }) log.Info("Serving web interface on localhost:%d", port) go http.ListenAndServe(fmt.Sprintf(":%d", port), nil) return v }
func NewWebView(ctl mvc.Controller, addr string) *WebView { wv := &WebView{ Logger: log.NewPrefixLogger("view", "web"), wsMessages: util.NewBroadcast(), ctl: ctl, } // for now, always redirect to the http view http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/http/in", 302) }) // handle web socket connections http.HandleFunc("/_ws", func(w http.ResponseWriter, r *http.Request) { conn, err := websocket.Upgrade(w, r, nil, 1024, 1024) if err != nil { http.Error(w, "Failed websocket upgrade", 400) wv.Warn("Failed websocket upgrade: %v", err) return } msgs := wv.wsMessages.Reg() defer wv.wsMessages.UnReg(msgs) for m := range msgs { err := conn.WriteMessage(websocket.TextMessage, m.([]byte)) if err != nil { // connection is closed break } } }) // serve static assets http.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) { // buf, err := assets.Asset(path.Join("assets", "client", r.URL.Path[1:])) buf, err := ioutil.ReadFile(path.Join("assets", "client", r.URL.Path[1:])) if err != nil { wv.Warn("Error serving static file: %s", err.Error()) http.NotFound(w, r) return } w.Write(buf) }) wv.Info("Serving web interface on %s", addr) wv.ctl.Go(func() { http.ListenAndServe(addr, nil) }) return wv }
func NewTermView(ctl mvc.Controller) *TermView { // initialize terminal display termbox.Init() w, _ := termbox.Size() v := &TermView{ ctl: ctl, updates: ctl.Updates().Reg(), redraw: util.NewBroadcast(), flush: make(chan int), shutdown: make(chan int), Logger: log.NewPrefixLogger("view", "term"), area: NewArea(0, 0, w, 10), } ctl.Go(v.run) ctl.Go(v.input) return v }