From 4ca39ae9b3f10aba737bcc877cc9976d2ecb7551 Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Fri, 16 Dec 2016 19:14:45 +0100 Subject: [PATCH] Renames of configuration keys and more docs License: MIT Signed-off-by: Hector Sanjuan --- cluster.go | 2 +- config.go | 41 ++++++++++++------ ipfs-cluster/main.go | 2 +- ipfs_connector.go => ipfs_http_connector.go | 8 ++-- ...tor_test.go => ipfs_http_connector_test.go | 0 ipfsmock_test.go | 13 +++++- raft.go | 4 +- http_api.go => rest_api.go | 42 +++++++++---------- http_api_test.go => rest_api_test.go | 0 9 files changed, 70 insertions(+), 42 deletions(-) rename ipfs_connector.go => ipfs_http_connector.go (97%) rename ipfs_connector_test.go => ipfs_http_connector_test.go (100%) rename http_api.go => rest_api.go (87%) rename http_api_test.go => rest_api_test.go (100%) diff --git a/cluster.go b/cluster.go index 223946dd..afb5ff7c 100644 --- a/cluster.go +++ b/cluster.go @@ -435,7 +435,7 @@ func makeHost(ctx context.Context, cfg *Config) (host.Host, error) { publicKey := privateKey.GetPublic() addr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", - cfg.ConsensusListenAddr, cfg.ConsensusListenPort)) + cfg.ClusterAddr, cfg.ClusterPort)) if err != nil { return nil, err } diff --git a/config.go b/config.go index e2271d7d..270c282b 100644 --- a/config.go +++ b/config.go @@ -6,18 +6,35 @@ import ( ) type Config struct { - IPFSHost string `json:"ipfs_host"` - IPFSPort int `json:"ipfs_port"` - APIListenAddr string `json:"cluster_api_listen_addr"` - APIListenPort int `json:"cluster_api_listen_port"` - IPFSAPIListenAddr string `json:"ipfs_api_listen_addr"` - IPFSAPIListenPort int `json:"ipfs_api_listen_port"` - ConsensusListenAddr string `json:"consensus_listen_addr"` - ConsensusListenPort int `json:"consensus_listen_port"` - ClusterPeers []string `json:"cluster_peers"` - ID string `json:"id"` - PrivateKey string `json:"private_key"` - RaftFolder string `json:"raft_folder"` + // Libp2p ID and private key for Cluster communication (including) + // the Consensus component. + ID string `json:"id"` + PrivateKey string `json:"private_key"` + + // List of multiaddresses of the peers of this cluster. + ClusterPeers []string `json:"cluster_peers"` + + // Listen parameters for the Cluster libp2p Host. Used by + // the Remote RPC and Consensus components. + ClusterAddr string `json:"cluster_addr"` + ClusterPort int `json:"cluster_port"` + + // Storage folder for snapshots, log store etc. Used by + // the Consensus component. + ConsensusDataFolder string `json:"consensus_data_folder"` + + // Listen parameters for the the Cluster HTTP API component. + APIAddr string `json:"api_addr"` + APIPort int `json:"api_port"` + + // Listen parameters for the IPFS Proxy. Used by the IPFS + // connector component. + IPFSAPIAddr string `json:"ipfs_api_addr"` + IPFSAPIPort int `json:"ipfs_api_port"` + + // Host/Port for the IPFS daemon. + IPFSAddr string `json:"ipfs_addr"` + IPFSPort int `json:"ipfs_port"` } func LoadConfig(path string) (*Config, error) { diff --git a/ipfs-cluster/main.go b/ipfs-cluster/main.go index c354d53a..c076ef94 100644 --- a/ipfs-cluster/main.go +++ b/ipfs-cluster/main.go @@ -40,7 +40,7 @@ func main() { state := ipfscluster.NewMapState() tracker := ipfscluster.NewMapPinTracker() - remote := NewLibp2pRemote() + remote := ipfscluster.NewLibp2pRemote() cluster, err := ipfscluster.NewCluster(clusterCfg, api, proxy, state, tracker, remote) if err != nil { diff --git a/ipfs_connector.go b/ipfs_http_connector.go similarity index 97% rename from ipfs_connector.go rename to ipfs_http_connector.go index 18798d41..39d7107d 100644 --- a/ipfs_connector.go +++ b/ipfs_http_connector.go @@ -49,7 +49,7 @@ type ipfsError struct { func NewIPFSHTTPConnector(cfg *Config) (*IPFSHTTPConnector, error) { ctx := context.Background() l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", - cfg.IPFSAPIListenAddr, cfg.IPFSAPIListenPort)) + cfg.IPFSAPIAddr, cfg.IPFSAPIPort)) if err != nil { return nil, err } @@ -62,10 +62,10 @@ func NewIPFSHTTPConnector(cfg *Config) (*IPFSHTTPConnector, error) { ipfs := &IPFSHTTPConnector{ ctx: ctx, - destHost: cfg.IPFSHost, + destHost: cfg.IPFSAddr, destPort: cfg.IPFSPort, - listenAddr: cfg.IPFSAPIListenAddr, - listenPort: cfg.IPFSAPIListenPort, + listenAddr: cfg.IPFSAPIAddr, + listenPort: cfg.IPFSAPIPort, handlers: make(map[string]func(http.ResponseWriter, *http.Request)), rpcCh: make(chan RPC, RPCMaxQueue), listener: l, diff --git a/ipfs_connector_test.go b/ipfs_http_connector_test.go similarity index 100% rename from ipfs_connector_test.go rename to ipfs_http_connector_test.go diff --git a/ipfsmock_test.go b/ipfsmock_test.go index cb434a8d..50e971f5 100644 --- a/ipfsmock_test.go +++ b/ipfsmock_test.go @@ -5,16 +5,20 @@ import ( "fmt" "net/http" "net/http/httptest" + "net/url" + "strconv" "strings" cid "github.com/ipfs/go-cid" ) // This is an ipfs daemon mock which should sustain the functionality used by -// used by ipfscluster. +// ipfscluster. type ipfsMock struct { server *httptest.Server + addr string + port int pinMap *MapState } @@ -42,6 +46,13 @@ func newIpfsMock() *ipfsMock { } ts := httptest.NewServer(http.HandlerFunc(m.handler)) m.server = ts + + url, _ := url.Parse(mock.server.URL) + h := strings.Split(url.Host, ":") + i, _ := strconv.Atoi(h[1]) + + m.port = i + m.addr = h[0] return m } diff --git a/raft.go b/raft.go index 55d93e68..9e9ac74e 100644 --- a/raft.go +++ b/raft.go @@ -56,14 +56,14 @@ func makeLibp2pRaft(cfg *Config, host host.Host, state State, op *clusterLogOp) raftCfg.Logger = nil } logger.Debug("creating file snapshot store") - snapshots, err := hashiraft.NewFileSnapshotStore(cfg.RaftFolder, maxSnapshots, nil) + snapshots, err := hashiraft.NewFileSnapshotStore(cfg.ConsensusDataFolder, maxSnapshots, nil) if err != nil { logger.Error("creating file snapshot store: ", err) return nil, nil, nil, err } logger.Debug("creating BoltDB log store") - logStore, err := raftboltdb.NewBoltStore(filepath.Join(cfg.RaftFolder, "raft.db")) + logStore, err := raftboltdb.NewBoltStore(filepath.Join(cfg.ConsensusDataFolder, "raft.db")) if err != nil { logger.Error("creating bolt store: ", err) return nil, nil, nil, err diff --git a/http_api.go b/rest_api.go similarity index 87% rename from http_api.go rename to rest_api.go index 597305ec..46788de2 100644 --- a/http_api.go +++ b/rest_api.go @@ -16,9 +16,9 @@ import ( mux "github.com/gorilla/mux" ) -// ClusterHTTPAPI implements a API and aims to provides +// RESTAPI implements an API and aims to provides // a RESTful HTTP API for Cluster. -type ClusterHTTPAPI struct { +type RESTAPI struct { ctx context.Context listenAddr string listenPort int @@ -70,11 +70,11 @@ type statusResp []statusCidResp // NewHTTPAPI creates a new object which is ready to be // started. -func NewHTTPAPI(cfg *Config) (*ClusterHTTPAPI, error) { +func NewHTTPAPI(cfg *Config) (*RESTAPI, error) { ctx := context.Background() l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", - cfg.APIListenAddr, - cfg.APIListenPort)) + cfg.APIAddr, + cfg.APIPort)) if err != nil { return nil, err } @@ -83,10 +83,10 @@ func NewHTTPAPI(cfg *Config) (*ClusterHTTPAPI, error) { s := &http.Server{Handler: router} s.SetKeepAlivesEnabled(true) // A reminder that this can be changed - api := &ClusterHTTPAPI{ + api := &RESTAPI{ ctx: ctx, - listenAddr: cfg.APIListenAddr, - listenPort: cfg.APIListenPort, + listenAddr: cfg.APIAddr, + listenPort: cfg.APIPort, listener: l, server: s, rpcCh: make(chan RPC, RPCMaxQueue), @@ -106,7 +106,7 @@ func NewHTTPAPI(cfg *Config) (*ClusterHTTPAPI, error) { return api, nil } -func (api *ClusterHTTPAPI) routes() []route { +func (api *RESTAPI) routes() []route { return []route{ route{ "Members", @@ -165,7 +165,7 @@ func (api *ClusterHTTPAPI) routes() []route { } } -func (api *ClusterHTTPAPI) run() { +func (api *RESTAPI) run() { api.wg.Add(1) go func() { defer api.wg.Done() @@ -180,7 +180,7 @@ func (api *ClusterHTTPAPI) run() { } // Shutdown stops any API listeners. -func (api *ClusterHTTPAPI) Shutdown() error { +func (api *RESTAPI) Shutdown() error { api.shutdownLock.Lock() defer api.shutdownLock.Unlock() @@ -202,11 +202,11 @@ func (api *ClusterHTTPAPI) Shutdown() error { // RpcChan can be used by Cluster to read any // requests from this component -func (api *ClusterHTTPAPI) RpcChan() <-chan RPC { +func (api *RESTAPI) RpcChan() <-chan RPC { return api.rpcCh } -func (api *ClusterHTTPAPI) versionHandler(w http.ResponseWriter, r *http.Request) { +func (api *RESTAPI) versionHandler(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(api.ctx) defer cancel() rRpc := NewRPC(VersionRPC, nil) @@ -217,7 +217,7 @@ func (api *ClusterHTTPAPI) versionHandler(w http.ResponseWriter, r *http.Request } } -func (api *ClusterHTTPAPI) memberListHandler(w http.ResponseWriter, r *http.Request) { +func (api *RESTAPI) memberListHandler(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(api.ctx) defer cancel() rRpc := NewRPC(MemberListRPC, nil) @@ -232,7 +232,7 @@ func (api *ClusterHTTPAPI) memberListHandler(w http.ResponseWriter, r *http.Requ } } -func (api *ClusterHTTPAPI) pinHandler(w http.ResponseWriter, r *http.Request) { +func (api *RESTAPI) pinHandler(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(api.ctx) defer cancel() @@ -245,7 +245,7 @@ func (api *ClusterHTTPAPI) pinHandler(w http.ResponseWriter, r *http.Request) { } } -func (api *ClusterHTTPAPI) unpinHandler(w http.ResponseWriter, r *http.Request) { +func (api *RESTAPI) unpinHandler(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(api.ctx) defer cancel() @@ -258,7 +258,7 @@ func (api *ClusterHTTPAPI) unpinHandler(w http.ResponseWriter, r *http.Request) } } -func (api *ClusterHTTPAPI) pinListHandler(w http.ResponseWriter, r *http.Request) { +func (api *RESTAPI) pinListHandler(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(api.ctx) defer cancel() rRpc := NewRPC(PinListRPC, nil) @@ -270,7 +270,7 @@ func (api *ClusterHTTPAPI) pinListHandler(w http.ResponseWriter, r *http.Request } -func (api *ClusterHTTPAPI) statusHandler(w http.ResponseWriter, r *http.Request) { +func (api *RESTAPI) statusHandler(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(api.ctx) defer cancel() rRpc := NewRPC(StatusRPC, nil) @@ -280,7 +280,7 @@ func (api *ClusterHTTPAPI) statusHandler(w http.ResponseWriter, r *http.Request) } } -func (api *ClusterHTTPAPI) statusCidHandler(w http.ResponseWriter, r *http.Request) { +func (api *RESTAPI) statusCidHandler(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(api.ctx) defer cancel() @@ -293,7 +293,7 @@ func (api *ClusterHTTPAPI) statusCidHandler(w http.ResponseWriter, r *http.Reque } } -func (api *ClusterHTTPAPI) syncHandler(w http.ResponseWriter, r *http.Request) { +func (api *RESTAPI) syncHandler(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(api.ctx) defer cancel() rRpc := NewRPC(LocalSyncRPC, nil) @@ -303,7 +303,7 @@ func (api *ClusterHTTPAPI) syncHandler(w http.ResponseWriter, r *http.Request) { } } -func (api *ClusterHTTPAPI) syncCidHandler(w http.ResponseWriter, r *http.Request) { +func (api *RESTAPI) syncCidHandler(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(api.ctx) defer cancel() diff --git a/http_api_test.go b/rest_api_test.go similarity index 100% rename from http_api_test.go rename to rest_api_test.go