|
1 | 1 | package corehttp |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "errors" |
4 | 6 | "fmt" |
5 | 7 | "github.com/ipfs/go-blockservice" |
| 8 | + "github.com/ipfs/go-cid" |
6 | 9 | offline "github.com/ipfs/go-ipfs-exchange-offline" |
7 | 10 | offlineroute "github.com/ipfs/go-ipfs-routing/offline" |
| 11 | + "github.com/ipfs/go-libipfs/files" |
8 | 12 | "github.com/ipfs/go-namesys" |
| 13 | + iface "github.com/ipfs/interface-go-ipfs-core" |
| 14 | + "github.com/ipfs/interface-go-ipfs-core/path" |
9 | 15 | "github.com/ipfs/kubo/core/node" |
10 | 16 | "github.com/libp2p/go-libp2p/core/routing" |
| 17 | + "io" |
11 | 18 | "net" |
12 | 19 | "net/http" |
13 | 20 |
|
@@ -145,9 +152,90 @@ func newGatewayAPI(n *core.IpfsNode) (gateway.API, error) { |
145 | 152 | } |
146 | 153 | } |
147 | 154 |
|
148 | | - return gateway.NewBlocksGateway(bserv, gateway.WithValueStore(vsRouting), gateway.WithNameSystem(nsys)) |
| 155 | + gw, err := gateway.NewBlocksGateway(bserv, gateway.WithValueStore(vsRouting), gateway.WithNameSystem(nsys)) |
| 156 | + if err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + return &offlineGatewayErrWrapper{gwimpl: gw}, nil |
| 160 | +} |
| 161 | + |
| 162 | +type offlineGatewayErrWrapper struct { |
| 163 | + gwimpl gateway.API |
| 164 | +} |
| 165 | + |
| 166 | +func offlineErrWrap(err error) error { |
| 167 | + if errors.Is(err, iface.ErrOffline) { |
| 168 | + return fmt.Errorf("%s : %w", err.Error(), gateway.ErrServiceUnavailable) |
| 169 | + } |
| 170 | + return err |
| 171 | +} |
| 172 | + |
| 173 | +func (o *offlineGatewayErrWrapper) Get(ctx context.Context, path gateway.ImmutablePath) (gateway.ContentPathMetadata, files.Node, error) { |
| 174 | + md, n, err := o.gwimpl.Get(ctx, path) |
| 175 | + err = offlineErrWrap(err) |
| 176 | + return md, n, err |
| 177 | +} |
| 178 | + |
| 179 | +func (o *offlineGatewayErrWrapper) GetRange(ctx context.Context, path gateway.ImmutablePath, ranges ...gateway.GetRange) (gateway.ContentPathMetadata, files.File, error) { |
| 180 | + md, n, err := o.gwimpl.GetRange(ctx, path, ranges...) |
| 181 | + err = offlineErrWrap(err) |
| 182 | + return md, n, err |
| 183 | +} |
| 184 | + |
| 185 | +func (o *offlineGatewayErrWrapper) GetAll(ctx context.Context, path gateway.ImmutablePath) (gateway.ContentPathMetadata, files.Node, error) { |
| 186 | + md, n, err := o.gwimpl.GetAll(ctx, path) |
| 187 | + err = offlineErrWrap(err) |
| 188 | + return md, n, err |
| 189 | +} |
| 190 | + |
| 191 | +func (o *offlineGatewayErrWrapper) GetBlock(ctx context.Context, path gateway.ImmutablePath) (gateway.ContentPathMetadata, files.File, error) { |
| 192 | + md, n, err := o.gwimpl.GetBlock(ctx, path) |
| 193 | + err = offlineErrWrap(err) |
| 194 | + return md, n, err |
149 | 195 | } |
150 | 196 |
|
| 197 | +func (o *offlineGatewayErrWrapper) Head(ctx context.Context, path gateway.ImmutablePath) (gateway.ContentPathMetadata, files.Node, error) { |
| 198 | + md, n, err := o.gwimpl.Head(ctx, path) |
| 199 | + err = offlineErrWrap(err) |
| 200 | + return md, n, err |
| 201 | +} |
| 202 | + |
| 203 | +func (o *offlineGatewayErrWrapper) ResolvePath(ctx context.Context, path gateway.ImmutablePath) (gateway.ContentPathMetadata, error) { |
| 204 | + md, err := o.gwimpl.ResolvePath(ctx, path) |
| 205 | + err = offlineErrWrap(err) |
| 206 | + return md, err |
| 207 | +} |
| 208 | + |
| 209 | +func (o *offlineGatewayErrWrapper) GetCAR(ctx context.Context, path gateway.ImmutablePath) (gateway.ContentPathMetadata, io.ReadCloser, <-chan error, error) { |
| 210 | + md, data, errCh, err := o.gwimpl.GetCAR(ctx, path) |
| 211 | + err = offlineErrWrap(err) |
| 212 | + return md, data, errCh, err |
| 213 | +} |
| 214 | + |
| 215 | +func (o *offlineGatewayErrWrapper) IsCached(ctx context.Context, path path.Path) bool { |
| 216 | + return o.gwimpl.IsCached(ctx, path) |
| 217 | +} |
| 218 | + |
| 219 | +func (o *offlineGatewayErrWrapper) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte, error) { |
| 220 | + rec, err := o.gwimpl.GetIPNSRecord(ctx, c) |
| 221 | + err = offlineErrWrap(err) |
| 222 | + return rec, err |
| 223 | +} |
| 224 | + |
| 225 | +func (o *offlineGatewayErrWrapper) ResolveMutable(ctx context.Context, path path.Path) (gateway.ImmutablePath, error) { |
| 226 | + imPath, err := o.gwimpl.ResolveMutable(ctx, path) |
| 227 | + err = offlineErrWrap(err) |
| 228 | + return imPath, err |
| 229 | +} |
| 230 | + |
| 231 | +func (o *offlineGatewayErrWrapper) GetDNSLinkRecord(ctx context.Context, s string) (path.Path, error) { |
| 232 | + p, err := o.gwimpl.GetDNSLinkRecord(ctx, s) |
| 233 | + err = offlineErrWrap(err) |
| 234 | + return p, err |
| 235 | +} |
| 236 | + |
| 237 | +var _ gateway.API = (*offlineGatewayErrWrapper)(nil) |
| 238 | + |
151 | 239 | var defaultPaths = []string{"/ipfs/", "/ipns/", "/api/", "/p2p/"} |
152 | 240 |
|
153 | 241 | var subdomainGatewaySpec = &gateway.Specification{ |
|
0 commit comments