@cryptotaxi247 / kubo / commits / fbd76ebb5

corehttp: ServeOption supports chaining muxes

Each option now additionally returns the mux to be used by future options. If every options returns the mux it was passed, the current behavior is unchanged. However, if the option returns an a new mux, it can mediate requests to handlers provided by future options: return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) { childMux := http.NewServeMux() mux.Handle("/", handlerThatDelegatesToChildMux) return childMux, nil } License: MIT Signed-off-by: Kevin Wallace <kevin@pentabarf.net>

Kevin Wallace committed Feb 2, 2015 at 22:50 UTC fbd76ebb5b5390a5037340003e77f06e04e3e87a
4 files changed +19 -17
core/corehttp/commands.go
+2 -2
@@ -16,10 +16,10 @@ const (
16 )
17
18 func CommandsOption(cctx commands.Context) ServeOption {
19 - return func(n *core.IpfsNode, mux *http.ServeMux) error {
19 + return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
20 origin := os.Getenv(originEnvKey)
21 cmdHandler := cmdsHttp.NewHandler(cctx, corecommands.Root, origin)
22 mux.Handle(cmdsHttp.ApiPath+"/", cmdHandler)
23 - return nil
23 + return mux, nil
24 }
25 }
core/corehttp/corehttp.go
+10 -8
@@ -12,11 +12,11 @@ import (
12
13 var log = eventlog.Logger("core/server")
14
15 -const (
16 -// TODO rename
17 -)
18 -
19 -type ServeOption func(*core.IpfsNode, *http.ServeMux) error
15 +// ServeOption registers any HTTP handlers it provides on the given mux.
16 +// It returns the mux to expose to future options, which may be a new mux if it
17 +// is interested in mediating requests to future options, or the same mux
18 +// initially passed in if not.
19 +type ServeOption func(*core.IpfsNode, *http.ServeMux) (*http.ServeMux, error)
20
21 // ListenAndServe runs an HTTP server listening at |listeningMultiAddr| with
22 // the given serve options. The address must be provided in multiaddr format.
@@ -29,13 +29,15 @@ func ListenAndServe(n *core.IpfsNode, listeningMultiAddr string, options ...Serv
29 if err != nil {
30 return err
31 }
32 - mux := http.NewServeMux()
32 + topMux := http.NewServeMux()
33 + mux := topMux
34 for _, option := range options {
34 - if err := option(n, mux); err != nil {
35 + mux, err = option(n, mux)
36 + if err != nil {
37 return err
38 }
39 }
38 - return listenAndServe(n, addr, mux)
40 + return listenAndServe(n, addr, topMux)
41 }
42
43 func listenAndServe(node *core.IpfsNode, addr ma.Multiaddr, mux *http.ServeMux) error {
core/corehttp/gateway.go
+5 -5
@@ -24,14 +24,14 @@ func NewGateway(conf GatewayConfig) *Gateway {
24 }
25
26 func (g *Gateway) ServeOption() ServeOption {
27 - return func(n *core.IpfsNode, mux *http.ServeMux) error {
27 + return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
28 gateway, err := newGatewayHandler(n, g.Config)
29 if err != nil {
30 - return err
30 + return nil, err
31 }
32 mux.Handle("/ipfs/", gateway)
33 mux.Handle("/ipns/", gateway)
34 - return nil
34 + return mux, nil
35 }
36 }
37
@@ -47,8 +47,8 @@ func GatewayOption(writable bool) ServeOption {
47 type Decider func(string) bool
48
49 type BlockList struct {
50 - mu sync.RWMutex
51 - Decider Decider
50 + mu sync.RWMutex
51 + Decider Decider
52 }
53
54 func (b *BlockList) ShouldAllow(s string) bool {
core/corehttp/redirect.go
+2 -2
@@ -8,9 +8,9 @@ import (
8
9 func RedirectOption(path string, redirect string) ServeOption {
10 handler := &redirectHandler{redirect}
11 - return func(n *core.IpfsNode, mux *http.ServeMux) error {
11 + return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
12 mux.Handle("/"+path, handler)
13 - return nil
13 + return mux, nil
14 }
15 }
16