..

ipfs-cluster-service的restapi服务

ipfs-cluster-service daemon 启动后,会创建一个用来处理 http restfulapi,处理来自 ctl 的请求,如 add 操作等。


restfulapi server 启动流程:

func daemon(c *cli.Context) error {
	cluster, err := createCluster(ctx, c, cfgHelper, host, pubsub, dht, store, raftStaging)
}
func createCluster() {
	api, err = rest.NewAPI(ctx, cfgs.Restapi)
}
func NewAPI(ctx context.Context, cfg *Config) (*API, error) {
	fmt.Println("调用 NewAPIWithHost ")
	return NewAPIWithHost(ctx, cfg, nil)
}
func NewAPIWithHost(ctx context.Context, cfg *Config, h host.Host) (*API, error) {
		api.run(ctx)
}
func (api *API) run(ctx context.Context) {
	api.wg.Add(len(api.httpListeners))
	for _, l := range api.httpListeners {
		go func(l net.Listener) {
			defer api.wg.Done()
			api.runHTTPServer(ctx, l)
		}(l)
	}

	if api.libp2pListener != nil {
		api.wg.Add(1)
		go func() {
			defer api.wg.Done()
			api.runLibp2pServer(ctx)
		}()
	}
}
// runs in goroutine from run()
func (api *API) runHTTPServer(ctx context.Context, l net.Listener) {
	select {
	case <-api.rpcReady:
	case <-api.ctx.Done():
		return
	}

	maddr, err := manet.FromNetAddr(l.Addr())
	if err != nil {
		logger.Error(err)
	}

	logger.Infof("REST API (HTTP): %s", maddr)
	err = api.server.Serve(l)
	if err != nil && !strings.Contains(err.Error(), "closed network connection") {
		logger.Error(err)
	}
}

默认监听 REST API (HTTP): /ip4/127.0.0.1/tcp/9094


Nothing