Skip to content

Commit

Permalink
refactor: prepare gateway for extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jan 27, 2023
1 parent a3c70a1 commit 5908bbc
Show file tree
Hide file tree
Showing 38 changed files with 689 additions and 556 deletions.
10 changes: 0 additions & 10 deletions assets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,3 @@
This directory contains the go-ipfs assets:

* Getting started documentation (`init-doc`).
* Directory listing HTML template (`dir-index-html`).

## Re-generating

Edit the source files and use `go generate` from within the
assets directory:

```
go generate .
```
36 changes: 1 addition & 35 deletions assets/assets.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
//go:generate npm run build --prefix ./dir-index-html/
package assets

import (
"embed"
"fmt"
"io"
"io/fs"
gopath "path"
"strconv"

"github.com/ipfs/kubo/core"
"github.com/ipfs/kubo/core/coreapi"

"github.com/cespare/xxhash"
cid "github.com/ipfs/go-cid"
"github.com/ipfs/go-libipfs/files"
options "github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
)

//go:embed init-doc dir-index-html/dir-index.html dir-index-html/knownIcons.txt
//go:embed init-doc
var Asset embed.FS

// AssetHash a non-cryptographic hash of all embedded assets
var AssetHash string

// initDocPaths lists the paths for the docs we want to seed during --init
var initDocPaths = []string{
gopath.Join("init-doc", "about"),
Expand All @@ -36,32 +28,6 @@ var initDocPaths = []string{
gopath.Join("init-doc", "ping"),
}

func init() {
sum := xxhash.New()
err := fs.WalkDir(Asset, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
return nil
}

file, err := Asset.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(sum, file)
return err
})
if err != nil {
panic("error creating asset sum: " + err.Error())
}

AssetHash = strconv.FormatUint(sum.Sum64(), 32)
}

// SeedInitDocs adds the list of embedded init documentation to the passed node, pins it and returns the root key
func SeedInitDocs(nd *core.IpfsNode) (cid.Cid, error) {
return addAssetList(nd, initDocPaths)
Expand Down
3 changes: 0 additions & 3 deletions assets/dag-index-html/README.md

This file was deleted.

81 changes: 0 additions & 81 deletions assets/dag-index-html/index.go

This file was deleted.

26 changes: 0 additions & 26 deletions assets/dir-index-html/README.md

This file was deleted.

1 change: 0 additions & 1 deletion assets/dir-index-html/index.go

This file was deleted.

17 changes: 0 additions & 17 deletions assets/dir-index-html/package.json

This file was deleted.

3 changes: 0 additions & 3 deletions assets/dir-index-html/test/go.mod

This file was deleted.

116 changes: 0 additions & 116 deletions assets/dir-index-html/test/main.go

This file was deleted.

97 changes: 3 additions & 94 deletions core/corehttp/gateway.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,19 @@
package corehttp

import (
"context"
"fmt"
"net"
"net/http"
"sort"

coreiface "github.com/ipfs/interface-go-ipfs-core"
options "github.com/ipfs/interface-go-ipfs-core/options"
path "github.com/ipfs/interface-go-ipfs-core/path"
version "github.com/ipfs/kubo"
core "github.com/ipfs/kubo/core"
coreapi "github.com/ipfs/kubo/core/coreapi"
"github.com/ipfs/kubo/core/corehttp/gateway"
id "github.com/libp2p/go-libp2p/p2p/protocol/identify"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

type GatewayConfig struct {
Headers map[string][]string
Writable bool
}

// NodeAPI defines the minimal set of API services required by a gateway handler
type NodeAPI interface {
// Unixfs returns an implementation of Unixfs API
Unixfs() coreiface.UnixfsAPI

// Block returns an implementation of Block API
Block() coreiface.BlockAPI

// Dag returns an implementation of Dag API
Dag() coreiface.APIDagService

// Routing returns an implementation of Routing API.
// Used for returning signed IPNS records, see IPIP-0328
Routing() coreiface.RoutingAPI

// ResolvePath resolves the path using Unixfs resolver
ResolvePath(context.Context, path.Path) (path.Resolved, error)
}

// A helper function to clean up a set of headers:
// 1. Canonicalizes.
// 2. Deduplicates.
// 3. Sorts.
func cleanHeaderSet(headers []string) []string {
// Deduplicate and canonicalize.
m := make(map[string]struct{}, len(headers))
for _, h := range headers {
m[http.CanonicalHeaderKey(h)] = struct{}{}
}
result := make([]string, 0, len(m))
for k := range m {
result = append(result, k)
}

// Sort
sort.Strings(result)
return result
}

func GatewayOption(writable bool, paths ...string) ServeOption {
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
cfg, err := n.Repo.Config()
Expand All @@ -78,14 +31,14 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
headers[http.CanonicalHeaderKey(h)] = v
}

AddAccessControlHeaders(headers)
gateway.AddAccessControlHeaders(headers)

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

gateway := NewGatewayHandler(GatewayConfig{
gateway := gateway.NewHandler(gateway.Config{
Headers: headers,
Writable: writable,
}, api, offlineAPI)
Expand All @@ -99,50 +52,6 @@ func GatewayOption(writable bool, paths ...string) ServeOption {
}
}

// AddAccessControlHeaders adds default headers used for controlling
// cross-origin requests. This function adds several values to the
// Access-Control-Allow-Headers and Access-Control-Expose-Headers entries.
// If the Access-Control-Allow-Origin entry is missing a value of '*' is
// added, indicating that browsers should allow requesting code from any
// origin to access the resource.
// If the Access-Control-Allow-Methods entry is missing a value of 'GET' is
// added, indicating that browsers may use the GET method when issuing cross
// origin requests.
func AddAccessControlHeaders(headers map[string][]string) {
// Hard-coded headers.
const ACAHeadersName = "Access-Control-Allow-Headers"
const ACEHeadersName = "Access-Control-Expose-Headers"
const ACAOriginName = "Access-Control-Allow-Origin"
const ACAMethodsName = "Access-Control-Allow-Methods"

if _, ok := headers[ACAOriginName]; !ok {
// Default to *all*
headers[ACAOriginName] = []string{"*"}
}
if _, ok := headers[ACAMethodsName]; !ok {
// Default to GET
headers[ACAMethodsName] = []string{http.MethodGet}
}

headers[ACAHeadersName] = cleanHeaderSet(
append([]string{
"Content-Type",
"User-Agent",
"Range",
"X-Requested-With",
}, headers[ACAHeadersName]...))

headers[ACEHeadersName] = cleanHeaderSet(
append([]string{
"Content-Length",
"Content-Range",
"X-Chunked-Output",
"X-Stream-Output",
"X-Ipfs-Path",
"X-Ipfs-Roots",
}, headers[ACEHeadersName]...))
}

func VersionOption() ServeOption {
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading

0 comments on commit 5908bbc

Please sign in to comment.