1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-06 19:44:01 +08:00
Files
kubo/core/corehttp/gateway.go
Łukasz Magiera 6cee21d39a Fix offline gateway directory logic
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
2019-01-04 02:39:17 +01:00

57 lines
1.4 KiB
Go

package corehttp
import (
"fmt"
"net"
"net/http"
version "github.com/ipfs/go-ipfs"
core "github.com/ipfs/go-ipfs/core"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
id "gx/ipfs/QmRBaUEQEeFWywfrZJ64QgsmvcqgLSK3VbvGMR2NM2Edpf/go-libp2p/p2p/protocol/identify"
)
type GatewayConfig struct {
Headers map[string][]string
Writable bool
PathPrefixes []string
}
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()
if err != nil {
return nil, err
}
api, err := coreapi.NewCoreAPI(n, options.Api.Offline(cfg.Gateway.NoFetch))
if err != nil {
return nil, err
}
gateway := newGatewayHandler(n, GatewayConfig{
Headers: cfg.Gateway.HTTPHeaders,
Writable: writable,
PathPrefixes: cfg.Gateway.PathPrefixes,
}, api)
for _, p := range paths {
mux.Handle(p+"/", gateway)
}
return mux, nil
}
}
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) {
fmt.Fprintf(w, "Commit: %s\n", version.CurrentCommit)
fmt.Fprintf(w, "Client Version: %s\n", id.ClientVersion)
fmt.Fprintf(w, "Protocol Version: %s\n", id.LibP2PVersion)
})
return mux, nil
}
}