github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/core/corehttp/logs.go (about) 1 package corehttp 2 3 import ( 4 "io" 5 "net" 6 "net/http" 7 8 core "github.com/ipfs/go-ipfs/core" 9 "github.com/ipfs/go-ipfs/thirdparty/eventlog" 10 ) 11 12 type writeErrNotifier struct { 13 w io.Writer 14 errs chan error 15 } 16 17 func newWriteErrNotifier(w io.Writer) (io.Writer, <-chan error) { 18 ch := make(chan error, 1) 19 return &writeErrNotifier{ 20 w: w, 21 errs: ch, 22 }, ch 23 } 24 25 func (w *writeErrNotifier) Write(b []byte) (int, error) { 26 n, err := w.w.Write(b) 27 if err != nil { 28 select { 29 case w.errs <- err: 30 default: 31 } 32 } 33 if f, ok := w.w.(http.Flusher); ok { 34 f.Flush() 35 } 36 return n, err 37 } 38 39 func LogOption() ServeOption { 40 return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { 41 mux.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) { 42 w.WriteHeader(200) 43 wnf, errs := newWriteErrNotifier(w) 44 eventlog.WriterGroup.AddWriter(wnf) 45 log.Event(n.Context(), "log API client connected") 46 <-errs 47 }) 48 return mux, nil 49 } 50 }