From fadede6cb2b5edfd293e3324a803e486e005a602 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow <brian.holderchow@gmail.com> Date: Thu, 22 Jan 2015 01:27:40 -0800 Subject: [PATCH] separate concerns --- core/corehttp/commands.go | 25 +++++++++++++++++++++++++ core/corehttp/corehttp.go | 39 +-------------------------------------- core/corehttp/gateway.go | 16 ++++++++++++++++ core/corehttp/webui.go | 25 +++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 38 deletions(-) create mode 100644 core/corehttp/commands.go create mode 100644 core/corehttp/gateway.go create mode 100644 core/corehttp/webui.go diff --git a/core/corehttp/commands.go b/core/corehttp/commands.go new file mode 100644 index 000000000..3b9ac262c --- /dev/null +++ b/core/corehttp/commands.go @@ -0,0 +1,25 @@ +package corehttp + +import ( + "net/http" + "os" + + commands "github.com/jbenet/go-ipfs/commands" + cmdsHttp "github.com/jbenet/go-ipfs/commands/http" + core "github.com/jbenet/go-ipfs/core" + corecommands "github.com/jbenet/go-ipfs/core/commands" +) + +const ( + // TODO rename + originEnvKey = "API_ORIGIN" +) + +func CommandsOption(cctx commands.Context) ServeOption { + return func(n *core.IpfsNode, mux *http.ServeMux) error { + origin := os.Getenv(originEnvKey) + cmdHandler := cmdsHttp.NewHandler(cctx, corecommands.Root, origin) + mux.Handle(cmdsHttp.ApiPath+"/", cmdHandler) + return nil + } +} diff --git a/core/corehttp/corehttp.go b/core/corehttp/corehttp.go index 14d2d231c..1fe007675 100644 --- a/core/corehttp/corehttp.go +++ b/core/corehttp/corehttp.go @@ -3,24 +3,18 @@ package corehttp import ( "fmt" "net/http" - "os" manners "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/braintree/manners" ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net" - commands "github.com/jbenet/go-ipfs/commands" - cmdsHttp "github.com/jbenet/go-ipfs/commands/http" core "github.com/jbenet/go-ipfs/core" - corecommands "github.com/jbenet/go-ipfs/core/commands" eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog" ) var log = eventlog.Logger("core/server") const ( - // TODO rename - originEnvKey = "API_ORIGIN" - webuiPath = "/ipfs/QmTWvqK9dYvqjAMAcCeUun8b45Fwu7wPhEN9B9TsGbkXfJ" +// TODO rename ) type ServeOption func(*core.IpfsNode, *http.ServeMux) error @@ -35,29 +29,6 @@ func ListenAndServe(n *core.IpfsNode, addr ma.Multiaddr, options ...ServeOption) return listenAndServe("API", n, addr, mux) } -func CommandsOption(cctx commands.Context) ServeOption { - return func(n *core.IpfsNode, mux *http.ServeMux) error { - origin := os.Getenv(originEnvKey) - cmdHandler := cmdsHttp.NewHandler(cctx, corecommands.Root, origin) - mux.Handle(cmdsHttp.ApiPath+"/", cmdHandler) - return nil - } -} - -func GatewayOption(n *core.IpfsNode, mux *http.ServeMux) error { - gateway, err := newGatewayHandler(n) - if err != nil { - return err - } - mux.Handle("/ipfs/", gateway) - return nil -} - -func WebUIOption(n *core.IpfsNode, mux *http.ServeMux) error { - mux.Handle("/webui/", &redirectHandler{webuiPath}) - return nil -} - func listenAndServe(name string, node *core.IpfsNode, addr ma.Multiaddr, mux *http.ServeMux) error { _, host, err := manet.DialArgs(addr) if err != nil { @@ -90,11 +61,3 @@ func listenAndServe(name string, node *core.IpfsNode, addr ma.Multiaddr, mux *ht log.Infof("server at %s terminated", addr) return serverError } - -type redirectHandler struct { - path string -} - -func (i *redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, i.path, 302) -} diff --git a/core/corehttp/gateway.go b/core/corehttp/gateway.go new file mode 100644 index 000000000..c3f0a5593 --- /dev/null +++ b/core/corehttp/gateway.go @@ -0,0 +1,16 @@ +package corehttp + +import ( + "net/http" + + core "github.com/jbenet/go-ipfs/core" +) + +func GatewayOption(n *core.IpfsNode, mux *http.ServeMux) error { + gateway, err := newGatewayHandler(n) + if err != nil { + return err + } + mux.Handle("/ipfs/", gateway) + return nil +} diff --git a/core/corehttp/webui.go b/core/corehttp/webui.go new file mode 100644 index 000000000..b1fd08559 --- /dev/null +++ b/core/corehttp/webui.go @@ -0,0 +1,25 @@ +package corehttp + +import ( + "net/http" + + core "github.com/jbenet/go-ipfs/core" +) + +const ( + // TODO rename + webuiPath = "/ipfs/QmTWvqK9dYvqjAMAcCeUun8b45Fwu7wPhEN9B9TsGbkXfJ" +) + +func WebUIOption(n *core.IpfsNode, mux *http.ServeMux) error { + mux.Handle("/webui/", &redirectHandler{webuiPath}) + return nil +} + +type redirectHandler struct { + path string +} + +func (i *redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + http.Redirect(w, r, i.path, 302) +} -- GitLab