From 0c5faa2566f0b81186d30185ee55b9b842e2dc2c Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Thu, 10 Aug 2023 18:28:12 +1000 Subject: [PATCH 1/2] fix: advertise with correct publish address also: * add general 404 handler * add logging for all statuses, not just 200's --- cmd/frisbii/main.go | 9 ++++++++- frisbii.go | 5 +++++ logmiddlware.go | 9 ++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/cmd/frisbii/main.go b/cmd/frisbii/main.go index 6ff50f5..818dd81 100644 --- a/cmd/frisbii/main.go +++ b/cmd/frisbii/main.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "net/url" "os" "os/signal" "syscall" @@ -132,6 +133,12 @@ func action(c *cli.Context) error { return err } + httpath, err := multiaddr.NewComponent("httpath", url.PathEscape(IndexerHandlerPath)) + if err != nil { + return err + } + announceAddr := multiaddr.Join(frisbiiListenAddr.Maddr, httpath) + engine, err := engine.New( engine.WithPrivateKey(privKey), engine.WithProvider(peer.AddrInfo{ID: id, Addrs: []multiaddr.Multiaddr{frisbiiListenAddr.Maddr}}), @@ -140,7 +147,7 @@ func action(c *cli.Context) error { engine.WithHttpPublisherWithoutServer(), engine.WithHttpPublisherHandlerPath(IndexerHandlerPath), engine.WithHttpPublisherListenAddr(listenUrl.Host), - engine.WithHttpPublisherAnnounceAddr(frisbiiListenAddr.Maddr.String()), + engine.WithHttpPublisherAnnounceAddr(announceAddr.String()), ) if err != nil { return err diff --git a/frisbii.go b/frisbii.go index 4f2661a..1dbe696 100644 --- a/frisbii.go +++ b/frisbii.go @@ -70,12 +70,14 @@ func (fs *FrisbiiServer) Addr() net.Addr { func (fs *FrisbiiServer) Serve() error { fs.mux = http.NewServeMux() + fs.mux.Handle("/ipfs/", NewHttpIpfs(fs.ctx, fs.logWriter, fs.lsys, fs.maxResponseDuration, fs.maxResponseBytes)) server := &http.Server{ Addr: fs.Addr().String(), BaseContext: func(listener net.Listener) context.Context { return fs.ctx }, Handler: NewLogMiddleware(fs.mux, fs.logWriter), } + fs.mux.Handle("/", http.NotFoundHandler()) logger.Debugf("Serve() server on %s", fs.Addr().String()) return server.Serve(fs.listener) } @@ -86,6 +88,9 @@ func (fs *FrisbiiServer) SetIndexerProvider(handlerPath string, indexerProvider if err != nil { return err } + if handlerPath[len(handlerPath)-1] != '/' { + handlerPath += "/" + } fs.mux.HandleFunc(handlerPath, handlerFunc) logger.Debugf("SetIndexerProvider() handler on %s", handlerPath) return nil diff --git a/logmiddlware.go b/logmiddlware.go index 3824fe6..a865132 100644 --- a/logmiddlware.go +++ b/logmiddlware.go @@ -34,9 +34,7 @@ func (lm *LogMiddleware) ServeHTTP(res http.ResponseWriter, req *http.Request) { lres := NewLoggingResponseWriter(res, req, lm.logWriter) start := time.Now() defer func() { - if lres.status == http.StatusOK { - lres.Log(http.StatusOK, time.Since(start), lres.bytes, "") - } + lres.Log(lres.status, time.Since(start), lres.bytes, "") }() lm.next.ServeHTTP(lres, req) } @@ -49,6 +47,7 @@ type LoggingResponseWriter struct { req *http.Request status int bytes int + wrote bool } func NewLoggingResponseWriter(w http.ResponseWriter, req *http.Request, logWriter io.Writer) *LoggingResponseWriter { @@ -60,6 +59,10 @@ func NewLoggingResponseWriter(w http.ResponseWriter, req *http.Request, logWrite } func (w *LoggingResponseWriter) Log(status int, duration time.Duration, bytes int, msg string) { + if w.wrote { + return + } + w.wrote = true remoteAddr := w.req.RemoteAddr if ss := strings.Split(remoteAddr, ":"); len(ss) > 0 { remoteAddr = ss[0] From 3ea85ed4e4423a7edba77526c97a4270e1187eda Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Thu, 10 Aug 2023 20:17:05 +1000 Subject: [PATCH 2/2] v0.0.5 bump --- version.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.json b/version.json index edc249c..b9ab021 100644 --- a/version.json +++ b/version.json @@ -1,3 +1,3 @@ { - "version": "v0.0.4" + "version": "v0.0.5" }