@cryptotaxi247 / kubo / commits / fadedf9e6

refactor(core) extract corehttp package

PACKAGE DOCUMENTATION package corehttp FUNCTIONS func GatewayOption(n *core.IpfsNode, mux *http.ServeMux) error func ListenAndServe(n *core.IpfsNode, addr ma.Multiaddr, options ...ServeOption) error func WebUIOption(n *core.IpfsNode, mux *http.ServeMux) error TYPES type ServeOption func(*core.IpfsNode, *http.ServeMux) error func DaemonOption(cctx commands.Context) ServeOption

Brian Tiger Chow committed Jan 22, 2015 at 01:05 UTC fadedf9e68278402c18b7e8c3d79552414b85e6b
3 files changed +109 -81
cmd/ipfs/daemon.go
+7 -79
@@ -2,16 +2,11 @@ package main
2
3 import (
4 "fmt"
5 - "net/http"
6 - "os"
5
8 - manners "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/braintree/manners"
6 ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
10 - manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
7 cmds "github.com/jbenet/go-ipfs/commands"
12 - cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
13 - core "github.com/jbenet/go-ipfs/core"
8 commands "github.com/jbenet/go-ipfs/core/commands"
9 + corehttp "github.com/jbenet/go-ipfs/core/corehttp"
10 fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
11 util "github.com/jbenet/go-ipfs/util"
12 "github.com/jbenet/go-ipfs/util/debugerror"
@@ -24,10 +19,6 @@ const (
19 ipnsMountKwd = "mount-ipns"
20 // apiAddrKwd = "address-api"
21 // swarmAddrKwd = "address-swarm"
27 -
28 - originEnvKey = "API_ORIGIN"
29 -
30 - webuiPath = "/ipfs/QmTWvqK9dYvqjAMAcCeUun8b45Fwu7wPhEN9B9TsGbkXfJ"
22 )
23
24 var daemonCmd = &cmds.Command{
@@ -153,80 +144,17 @@ func daemonFunc(req cmds.Request) (interface{}, error) {
144
145 if gatewayMaddr != nil {
146 go func() {
156 - err := listenAndServeGateway(node, gatewayMaddr)
147 + err := corehttp.ListenAndServe(node, gatewayMaddr, corehttp.GatewayOption)
148 if err != nil {
149 log.Error(err)
150 }
151 }()
152 }
153
163 - return nil, listenAndServeAPI(node, req, apiMaddr)
164 -}
165 -
166 -func listenAndServeAPI(node *core.IpfsNode, req cmds.Request, addr ma.Multiaddr) error {
167 - origin := os.Getenv(originEnvKey)
168 - cmdHandler := cmdsHttp.NewHandler(*req.Context(), commands.Root, origin)
169 - gateway, err := NewGatewayHandler(node)
170 - if err != nil {
171 - return err
172 - }
173 -
174 - mux := http.NewServeMux()
175 - mux.Handle(cmdsHttp.ApiPath+"/", cmdHandler)
176 - mux.Handle("/ipfs/", gateway)
177 - mux.Handle("/webui/", &redirectHandler{webuiPath})
178 - return listenAndServe("API", node, addr, mux)
179 -}
180 -
181 -// the gateway also listens on its own address:port in addition to the API listener
182 -func listenAndServeGateway(node *core.IpfsNode, addr ma.Multiaddr) error {
183 - gateway, err := NewGatewayHandler(node)
184 - if err != nil {
185 - return err
186 - }
187 -
188 - mux := http.NewServeMux()
189 - mux.Handle("/ipfs/", gateway)
190 - return listenAndServe("gateway", node, addr, mux)
191 -}
192 -
193 -func listenAndServe(name string, node *core.IpfsNode, addr ma.Multiaddr, mux *http.ServeMux) error {
194 - _, host, err := manet.DialArgs(addr)
195 - if err != nil {
196 - return err
197 - }
198 -
199 - server := manners.NewServer()
200 -
201 - // if the server exits beforehand
202 - var serverError error
203 - serverExited := make(chan struct{})
204 -
205 - go func() {
206 - fmt.Printf("%s server listening on %s\n", name, addr)
207 - serverError = server.ListenAndServe(host, mux)
208 - close(serverExited)
209 - }()
210 -
211 - // wait for server to exit.
212 - select {
213 - case <-serverExited:
214 -
215 - // if node being closed before server exits, close server
216 - case <-node.Closing():
217 - log.Infof("server at %s terminating...", addr)
218 - server.Shutdown <- true
219 - <-serverExited // now, DO wait until server exit
154 + var opts = []corehttp.ServeOption{
155 + corehttp.CommandsOption(*req.Context()),
156 + corehttp.WebUIOption,
157 + corehttp.GatewayOption,
158 }
221 -
222 - log.Infof("server at %s terminated", addr)
223 - return serverError
224 -}
225 -
226 -type redirectHandler struct {
227 - path string
228 -}
229 -
230 -func (i *redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
231 - http.Redirect(w, r, i.path, 302)
159 + return nil, corehttp.ListenAndServe(node, apiMaddr, opts...)
160 }
core/corehttp/corehttp.go new
+100
@@ -0,0 +1,100 @@
1 +package corehttp
2 +
3 +import (
4 + "fmt"
5 + "net/http"
6 + "os"
7 +
8 + manners "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/braintree/manners"
9 + ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
10 + 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"
13 + core "github.com/jbenet/go-ipfs/core"
14 + corecommands "github.com/jbenet/go-ipfs/core/commands"
15 + eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
16 +)
17 +
18 +var log = eventlog.Logger("core/server")
19 +
20 +const (
21 + // TODO rename
22 + originEnvKey = "API_ORIGIN"
23 + webuiPath = "/ipfs/QmTWvqK9dYvqjAMAcCeUun8b45Fwu7wPhEN9B9TsGbkXfJ"
24 +)
25 +
26 +type ServeOption func(*core.IpfsNode, *http.ServeMux) error
27 +
28 +func ListenAndServe(n *core.IpfsNode, addr ma.Multiaddr, options ...ServeOption) error {
29 + mux := http.NewServeMux()
30 + for _, option := range options {
31 + if err := option(n, mux); err != nil {
32 + return err
33 + }
34 + }
35 + return listenAndServe("API", n, addr, mux)
36 +}
37 +
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 +
61 +func listenAndServe(name string, node *core.IpfsNode, addr ma.Multiaddr, mux *http.ServeMux) error {
62 + _, host, err := manet.DialArgs(addr)
63 + if err != nil {
64 + return err
65 + }
66 +
67 + server := manners.NewServer()
68 +
69 + // if the server exits beforehand
70 + var serverError error
71 + serverExited := make(chan struct{})
72 +
73 + go func() {
74 + fmt.Printf("%s server listening on %s\n", name, addr)
75 + serverError = server.ListenAndServe(host, mux)
76 + close(serverExited)
77 + }()
78 +
79 + // wait for server to exit.
80 + select {
81 + case <-serverExited:
82 +
83 + // if node being closed before server exits, close server
84 + case <-node.Closing():
85 + log.Infof("server at %s terminating...", addr)
86 + server.Shutdown <- true
87 + <-serverExited // now, DO wait until server exit
88 + }
89 +
90 + log.Infof("server at %s terminated", addr)
91 + return serverError
92 +}
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_handler.go renamed
+2 -2
@@ -1,4 +1,4 @@
1 -package main
1 +package corehttp
2
3 import (
4 "html/template"
@@ -42,7 +42,7 @@ type gatewayHandler struct {
42 dirList *template.Template
43 }
44
45 -func NewGatewayHandler(node *core.IpfsNode) (*gatewayHandler, error) {
45 +func newGatewayHandler(node *core.IpfsNode) (*gatewayHandler, error) {
46 i := &gatewayHandler{
47 node: node,
48 }