Skip to content

Commit 5908bbc

Browse files
committed
refactor: prepare gateway for extraction
1 parent a3c70a1 commit 5908bbc

38 files changed

+689
-556
lines changed

assets/README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,3 @@
33
This directory contains the go-ipfs assets:
44

55
* Getting started documentation (`init-doc`).
6-
* Directory listing HTML template (`dir-index-html`).
7-
8-
## Re-generating
9-
10-
Edit the source files and use `go generate` from within the
11-
assets directory:
12-
13-
```
14-
go generate .
15-
```

assets/assets.go

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
1-
//go:generate npm run build --prefix ./dir-index-html/
21
package assets
32

43
import (
54
"embed"
65
"fmt"
7-
"io"
8-
"io/fs"
96
gopath "path"
10-
"strconv"
117

128
"github.com/ipfs/kubo/core"
139
"github.com/ipfs/kubo/core/coreapi"
1410

15-
"github.com/cespare/xxhash"
1611
cid "github.com/ipfs/go-cid"
1712
"github.com/ipfs/go-libipfs/files"
1813
options "github.com/ipfs/interface-go-ipfs-core/options"
1914
"github.com/ipfs/interface-go-ipfs-core/path"
2015
)
2116

22-
//go:embed init-doc dir-index-html/dir-index.html dir-index-html/knownIcons.txt
17+
//go:embed init-doc
2318
var Asset embed.FS
2419

25-
// AssetHash a non-cryptographic hash of all embedded assets
26-
var AssetHash string
27-
2820
// initDocPaths lists the paths for the docs we want to seed during --init
2921
var initDocPaths = []string{
3022
gopath.Join("init-doc", "about"),
@@ -36,32 +28,6 @@ var initDocPaths = []string{
3628
gopath.Join("init-doc", "ping"),
3729
}
3830

39-
func init() {
40-
sum := xxhash.New()
41-
err := fs.WalkDir(Asset, ".", func(path string, d fs.DirEntry, err error) error {
42-
if err != nil {
43-
return err
44-
}
45-
46-
if d.IsDir() {
47-
return nil
48-
}
49-
50-
file, err := Asset.Open(path)
51-
if err != nil {
52-
return err
53-
}
54-
defer file.Close()
55-
_, err = io.Copy(sum, file)
56-
return err
57-
})
58-
if err != nil {
59-
panic("error creating asset sum: " + err.Error())
60-
}
61-
62-
AssetHash = strconv.FormatUint(sum.Sum64(), 32)
63-
}
64-
6531
// SeedInitDocs adds the list of embedded init documentation to the passed node, pins it and returns the root key
6632
func SeedInitDocs(nd *core.IpfsNode) (cid.Cid, error) {
6733
return addAssetList(nd, initDocPaths)

assets/dag-index-html/README.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

assets/dag-index-html/index.go

Lines changed: 0 additions & 81 deletions
This file was deleted.

assets/dir-index-html/README.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

assets/dir-index-html/index.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

assets/dir-index-html/package.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

assets/dir-index-html/test/go.mod

Lines changed: 0 additions & 3 deletions
This file was deleted.

assets/dir-index-html/test/main.go

Lines changed: 0 additions & 116 deletions
This file was deleted.

core/corehttp/gateway.go

Lines changed: 3 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,19 @@
11
package corehttp
22

33
import (
4-
"context"
54
"fmt"
65
"net"
76
"net/http"
8-
"sort"
97

10-
coreiface "github.com/ipfs/interface-go-ipfs-core"
118
options "github.com/ipfs/interface-go-ipfs-core/options"
12-
path "github.com/ipfs/interface-go-ipfs-core/path"
139
version "github.com/ipfs/kubo"
1410
core "github.com/ipfs/kubo/core"
1511
coreapi "github.com/ipfs/kubo/core/coreapi"
12+
"github.com/ipfs/kubo/core/corehttp/gateway"
1613
id "github.com/libp2p/go-libp2p/p2p/protocol/identify"
1714
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1815
)
1916

20-
type GatewayConfig struct {
21-
Headers map[string][]string
22-
Writable bool
23-
}
24-
25-
// NodeAPI defines the minimal set of API services required by a gateway handler
26-
type NodeAPI interface {
27-
// Unixfs returns an implementation of Unixfs API
28-
Unixfs() coreiface.UnixfsAPI
29-
30-
// Block returns an implementation of Block API
31-
Block() coreiface.BlockAPI
32-
33-
// Dag returns an implementation of Dag API
34-
Dag() coreiface.APIDagService
35-
36-
// Routing returns an implementation of Routing API.
37-
// Used for returning signed IPNS records, see IPIP-0328
38-
Routing() coreiface.RoutingAPI
39-
40-
// ResolvePath resolves the path using Unixfs resolver
41-
ResolvePath(context.Context, path.Path) (path.Resolved, error)
42-
}
43-
44-
// A helper function to clean up a set of headers:
45-
// 1. Canonicalizes.
46-
// 2. Deduplicates.
47-
// 3. Sorts.
48-
func cleanHeaderSet(headers []string) []string {
49-
// Deduplicate and canonicalize.
50-
m := make(map[string]struct{}, len(headers))
51-
for _, h := range headers {
52-
m[http.CanonicalHeaderKey(h)] = struct{}{}
53-
}
54-
result := make([]string, 0, len(m))
55-
for k := range m {
56-
result = append(result, k)
57-
}
58-
59-
// Sort
60-
sort.Strings(result)
61-
return result
62-
}
63-
6417
func GatewayOption(writable bool, paths ...string) ServeOption {
6518
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
6619
cfg, err := n.Repo.Config()
@@ -78,14 +31,14 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
7831
headers[http.CanonicalHeaderKey(h)] = v
7932
}
8033

81-
AddAccessControlHeaders(headers)
34+
gateway.AddAccessControlHeaders(headers)
8235

8336
offlineAPI, err := api.WithOptions(options.Api.Offline(true))
8437
if err != nil {
8538
return nil, err
8639
}
8740

88-
gateway := NewGatewayHandler(GatewayConfig{
41+
gateway := gateway.NewHandler(gateway.Config{
8942
Headers: headers,
9043
Writable: writable,
9144
}, api, offlineAPI)
@@ -99,50 +52,6 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
9952
}
10053
}
10154

102-
// AddAccessControlHeaders adds default headers used for controlling
103-
// cross-origin requests. This function adds several values to the
104-
// Access-Control-Allow-Headers and Access-Control-Expose-Headers entries.
105-
// If the Access-Control-Allow-Origin entry is missing a value of '*' is
106-
// added, indicating that browsers should allow requesting code from any
107-
// origin to access the resource.
108-
// If the Access-Control-Allow-Methods entry is missing a value of 'GET' is
109-
// added, indicating that browsers may use the GET method when issuing cross
110-
// origin requests.
111-
func AddAccessControlHeaders(headers map[string][]string) {
112-
// Hard-coded headers.
113-
const ACAHeadersName = "Access-Control-Allow-Headers"
114-
const ACEHeadersName = "Access-Control-Expose-Headers"
115-
const ACAOriginName = "Access-Control-Allow-Origin"
116-
const ACAMethodsName = "Access-Control-Allow-Methods"
117-
118-
if _, ok := headers[ACAOriginName]; !ok {
119-
// Default to *all*
120-
headers[ACAOriginName] = []string{"*"}
121-
}
122-
if _, ok := headers[ACAMethodsName]; !ok {
123-
// Default to GET
124-
headers[ACAMethodsName] = []string{http.MethodGet}
125-
}
126-
127-
headers[ACAHeadersName] = cleanHeaderSet(
128-
append([]string{
129-
"Content-Type",
130-
"User-Agent",
131-
"Range",
132-
"X-Requested-With",
133-
}, headers[ACAHeadersName]...))
134-
135-
headers[ACEHeadersName] = cleanHeaderSet(
136-
append([]string{
137-
"Content-Length",
138-
"Content-Range",
139-
"X-Chunked-Output",
140-
"X-Stream-Output",
141-
"X-Ipfs-Path",
142-
"X-Ipfs-Roots",
143-
}, headers[ACEHeadersName]...))
144-
}
145-
14655
func VersionOption() ServeOption {
14756
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
14857
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)