corehttp.go 2.86 KB
Newer Older
1 2 3 4
/*
Package corehttp provides utilities for the webui, gateways, and other
high-level HTTP interfaces to IPFS.
*/
5 6 7
package corehttp

import (
8
	"net"
9
	"net/http"
10
	"time"
11

12 13 14 15
	ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	manet "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
	core "github.com/ipfs/go-ipfs/core"
	eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
16 17 18 19
)

var log = eventlog.Logger("core/server")

20 21 22 23 24
// ServeOption registers any HTTP handlers it provides on the given mux.
// It returns the mux to expose to future options, which may be a new mux if it
// is interested in mediating requests to future options, or the same mux
// initially passed in if not.
type ServeOption func(*core.IpfsNode, *http.ServeMux) (*http.ServeMux, error)
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// makeHandler turns a list of ServeOptions into a http.Handler that implements
// all of the given options, in order.
func makeHandler(n *core.IpfsNode, options ...ServeOption) (http.Handler, error) {
	topMux := http.NewServeMux()
	mux := topMux
	for _, option := range options {
		var err error
		mux, err = option(n, mux)
		if err != nil {
			return nil, err
		}
	}
	return topMux, nil
}

41 42 43 44 45 46 47 48 49 50 51
// ListenAndServe runs an HTTP server listening at |listeningMultiAddr| with
// the given serve options. The address must be provided in multiaddr format.
//
// TODO intelligently parse address strings in other formats so long as they
// unambiguously map to a valid multiaddr. e.g. for convenience, ":8080" should
// map to "/ip4/0.0.0.0/tcp/8080".
func ListenAndServe(n *core.IpfsNode, listeningMultiAddr string, options ...ServeOption) error {
	addr, err := ma.NewMultiaddr(listeningMultiAddr)
	if err != nil {
		return err
	}
52 53 54
	handler, err := makeHandler(n, options...)
	if err != nil {
		return err
55
	}
56
	return listenAndServe(n, addr, handler)
57 58
}

59
func listenAndServe(node *core.IpfsNode, addr ma.Multiaddr, handler http.Handler) error {
60
	netarg, host, err := manet.DialArgs(addr)
61 62 63 64
	if err != nil {
		return err
	}

65 66 67 68
	list, err := net.Listen(netarg, host)
	if err != nil {
		return err
	}
69 70 71 72 73

	// if the server exits beforehand
	var serverError error
	serverExited := make(chan struct{})

74 75 76
	node.Children().Add(1)
	defer node.Children().Done()

77
	go func() {
78
		serverError = http.Serve(list, handler)
79 80 81 82 83 84 85 86 87 88
		close(serverExited)
	}()

	// wait for server to exit.
	select {
	case <-serverExited:

	// if node being closed before server exits, close server
	case <-node.Closing():
		log.Infof("server at %s terminating...", addr)
89

90
		list.Close()
91 92 93 94 95 96

	outer:
		for {
			// wait until server exits
			select {
			case <-serverExited:
97 98
				// if the server exited as we are closing, we really dont care about errors
				serverError = nil
99 100 101 102 103
				break outer
			case <-time.After(5 * time.Second):
				log.Infof("waiting for server at %s to terminate...", addr)
			}
		}
104 105 106 107 108
	}

	log.Infof("server at %s terminated", addr)
	return serverError
}