gateway.go 1.28 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3
package corehttp

import (
Jeromy's avatar
Jeromy committed
4
	"fmt"
5
	"net"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
6 7
	"net/http"

8
	core "github.com/ipfs/go-ipfs/core"
9
	coreapi "github.com/ipfs/go-ipfs/core/coreapi"
10
	config "github.com/ipfs/go-ipfs/repo/config"
Jeromy's avatar
Jeromy committed
11
	id "gx/ipfs/QmUYzZRJcuUxLSnSzF1bSyw1jYbNAULkBrbS6rnr7F72uK/go-libp2p/p2p/protocol/identify"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
12 13
)

14
type GatewayConfig struct {
15 16 17
	Headers      map[string][]string
	Writable     bool
	PathPrefixes []string
18 19
}

Lars Gierth's avatar
Lars Gierth committed
20
func GatewayOption(writable bool, paths ...string) ServeOption {
21
	return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
22 23 24 25 26
		cfg, err := n.Repo.Config()
		if err != nil {
			return nil, err
		}

27 28
		gateway := newGatewayHandler(n, GatewayConfig{
			Headers:      cfg.Gateway.HTTPHeaders,
Lars Gierth's avatar
Lars Gierth committed
29
			Writable:     writable,
30
			PathPrefixes: cfg.Gateway.PathPrefixes,
31
		}, coreapi.NewUnixfsAPI(n))
32

33 34
		for _, p := range paths {
			mux.Handle(p+"/", gateway)
35
		}
36
		return mux, nil
37
	}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
38
}
39

Jeromy's avatar
Jeromy committed
40
func VersionOption() ServeOption {
41
	return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
Jeromy's avatar
Jeromy committed
42
		mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
43 44
			fmt.Fprintf(w, "Commit: %s\n", config.CurrentCommit)
			fmt.Fprintf(w, "Client Version: %s\n", id.ClientVersion)
45
			fmt.Fprintf(w, "Protocol Version: %s\n", id.LibP2PVersion)
Jeromy's avatar
Jeromy committed
46 47 48 49
		})
		return mux, nil
	}
}