1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-10 09:52:20 +08:00
Files
kubo/core/corehttp/logs.go
Juan Batiz-Benet 3f1cbe2f43 corehttp: add net.Listener to ServeOption
ServeOptions take the node and muxer, they should get the listener
too as sometimes they need to operate on the listener address.

License: MIT
Signed-off-by: Juan Batiz-Benet <juan@benet.ai>
2015-08-02 08:16:51 +02:00

51 lines
970 B
Go

package corehttp
import (
"io"
"net"
"net/http"
core "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/thirdparty/eventlog"
)
type writeErrNotifier struct {
w io.Writer
errs chan error
}
func newWriteErrNotifier(w io.Writer) (io.Writer, <-chan error) {
ch := make(chan error, 1)
return &writeErrNotifier{
w: w,
errs: ch,
}, ch
}
func (w *writeErrNotifier) Write(b []byte) (int, error) {
n, err := w.w.Write(b)
if err != nil {
select {
case w.errs <- err:
default:
}
}
if f, ok := w.w.(http.Flusher); ok {
f.Flush()
}
return n, err
}
func LogOption() ServeOption {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
wnf, errs := newWriteErrNotifier(w)
eventlog.WriterGroup.AddWriter(wnf)
log.Event(n.Context(), "log API client connected")
<-errs
})
return mux, nil
}
}