separate concerns
Brian Tiger Chow committed
Jan 22, 2015 at 01:27 UTC
fadede6cb2b5edfd293e3324a803e486e005a602
4 files changed
+67
-38
core/corehttp/commands.go
new
+25
@@ -0,0 +1,25 @@
1
+package corehttp
2
+
3
+import (
4
+ "net/http"
5
+ "os"
6
+
7
+ commands "github.com/jbenet/go-ipfs/commands"
8
+ cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
9
+ core "github.com/jbenet/go-ipfs/core"
10
+ corecommands "github.com/jbenet/go-ipfs/core/commands"
11
+)
12
+
13
+const (
14
+ // TODO rename
15
+ originEnvKey = "API_ORIGIN"
16
+)
17
+
18
+func CommandsOption(cctx commands.Context) ServeOption {
19
+ return func(n *core.IpfsNode, mux *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
24
+ }
25
+}
core/corehttp/corehttp.go
+1
-38
@@ -3,24 +3,18 @@ package corehttp
3
import (
4
"fmt"
5
"net/http"
6
- "os"
6
7
manners "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/braintree/manners"
8
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
9
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
11
- commands "github.com/jbenet/go-ipfs/commands"
12
- cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
10
core "github.com/jbenet/go-ipfs/core"
14
- corecommands "github.com/jbenet/go-ipfs/core/commands"
11
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
12
)
13
14
var log = eventlog.Logger("core/server")
15
16
const (
21
- // TODO rename
22
- originEnvKey = "API_ORIGIN"
23
- webuiPath = "/ipfs/QmTWvqK9dYvqjAMAcCeUun8b45Fwu7wPhEN9B9TsGbkXfJ"
17
+// TODO rename
18
)
19
20
type ServeOption func(*core.IpfsNode, *http.ServeMux) error
@@ -35,29 +29,6 @@ func ListenAndServe(n *core.IpfsNode, addr ma.Multiaddr, options ...ServeOption)
29
return listenAndServe("API", n, addr, mux)
30
}
31
38
-func CommandsOption(cctx commands.Context) ServeOption {
39
- return func(n *core.IpfsNode, mux *http.ServeMux) error {
40
- origin := os.Getenv(originEnvKey)
41
- cmdHandler := cmdsHttp.NewHandler(cctx, corecommands.Root, origin)
42
- mux.Handle(cmdsHttp.ApiPath+"/", cmdHandler)
43
- return nil
44
- }
45
-}
46
-
47
-func GatewayOption(n *core.IpfsNode, mux *http.ServeMux) error {
48
- gateway, err := newGatewayHandler(n)
49
- if err != nil {
50
- return err
51
- }
52
- mux.Handle("/ipfs/", gateway)
53
- return nil
54
-}
55
-
56
-func WebUIOption(n *core.IpfsNode, mux *http.ServeMux) error {
57
- mux.Handle("/webui/", &redirectHandler{webuiPath})
58
- return nil
59
-}
60
-
32
func listenAndServe(name string, node *core.IpfsNode, addr ma.Multiaddr, mux *http.ServeMux) error {
33
_, host, err := manet.DialArgs(addr)
34
if err != nil {
@@ -90,11 +61,3 @@ func listenAndServe(name string, node *core.IpfsNode, addr ma.Multiaddr, mux *ht
61
log.Infof("server at %s terminated", addr)
62
return serverError
63
}
93
-
94
-type redirectHandler struct {
95
- path string
96
-}
97
-
98
-func (i *redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
99
- http.Redirect(w, r, i.path, 302)
100
-}
core/corehttp/gateway.go
new
+16
@@ -0,0 +1,16 @@
1
+package corehttp
2
+
3
+import (
4
+ "net/http"
5
+
6
+ core "github.com/jbenet/go-ipfs/core"
7
+)
8
+
9
+func GatewayOption(n *core.IpfsNode, mux *http.ServeMux) error {
10
+ gateway, err := newGatewayHandler(n)
11
+ if err != nil {
12
+ return err
13
+ }
14
+ mux.Handle("/ipfs/", gateway)
15
+ return nil
16
+}
core/corehttp/webui.go
new
+25
@@ -0,0 +1,25 @@
1
+package corehttp
2
+
3
+import (
4
+ "net/http"
5
+
6
+ core "github.com/jbenet/go-ipfs/core"
7
+)
8
+
9
+const (
10
+ // TODO rename
11
+ webuiPath = "/ipfs/QmTWvqK9dYvqjAMAcCeUun8b45Fwu7wPhEN9B9TsGbkXfJ"
12
+)
13
+
14
+func WebUIOption(n *core.IpfsNode, mux *http.ServeMux) error {
15
+ mux.Handle("/webui/", &redirectHandler{webuiPath})
16
+ return nil
17
+}
18
+
19
+type redirectHandler struct {
20
+ path string
21
+}
22
+
23
+func (i *redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
24
+ http.Redirect(w, r, i.path, 302)
25
+}