mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-23 03:17:43 +08:00
This ensures that child contexts are passed around between the handlers so that traces show the call hierarchy correctly.
45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package corehttp
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"html"
|
|
"net/http"
|
|
"time"
|
|
|
|
files "github.com/ipfs/go-ipfs-files"
|
|
"github.com/ipfs/go-ipfs/tracing"
|
|
ipath "github.com/ipfs/interface-go-ipfs-core/path"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func (i *gatewayHandler) serveUnixFS(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, begin time.Time, logger *zap.SugaredLogger) {
|
|
ctx, span := tracing.Span(ctx, "Gateway", "ServeUnixFS", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
|
|
defer span.End()
|
|
// Handling UnixFS
|
|
dr, err := i.api.Unixfs().Get(ctx, resolvedPath)
|
|
if err != nil {
|
|
webError(w, "ipfs cat "+html.EscapeString(contentPath.String()), err, http.StatusNotFound)
|
|
return
|
|
}
|
|
defer dr.Close()
|
|
|
|
// Handling Unixfs file
|
|
if f, ok := dr.(files.File); ok {
|
|
logger.Debugw("serving unixfs file", "path", contentPath)
|
|
i.serveFile(ctx, w, r, resolvedPath, contentPath, f, begin)
|
|
return
|
|
}
|
|
|
|
// Handling Unixfs directory
|
|
dir, ok := dr.(files.Directory)
|
|
if !ok {
|
|
internalWebError(w, fmt.Errorf("unsupported UnixFS type"))
|
|
return
|
|
}
|
|
logger.Debugw("serving unixfs directory", "path", contentPath)
|
|
i.serveDirectory(ctx, w, r, resolvedPath, contentPath, dir, begin, logger)
|
|
}
|