mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-23 19:37:46 +08:00
separate concerns
This commit is contained in:
parent
fadedf9e68
commit
fadede6cb2
25
core/corehttp/commands.go
Normal file
25
core/corehttp/commands.go
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
|
||||
16
core/corehttp/gateway.go
Normal file
16
core/corehttp/gateway.go
Normal file
@ -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
|
||||
}
|
||||
25
core/corehttp/webui.go
Normal file
25
core/corehttp/webui.go
Normal file
@ -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)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user