..

ipfs-cluster的restfulapi服务处理请求

Add 操作为例。

curl -X POST -F file=@hello.md "http://127.0.0.1:9094/add"

ipfs-cluster-service 的 routes

func (api *API) routes() []route {
	{
		"Add",
		"POST",
		"/add",
		api.addHandler,
	},
}

触发 add 回调

func (api *API) addHandler(w http.ResponseWriter, r *http.Request) {
	reader, err := r.MultipartReader()
	if err != nil {
		api.sendResponse(w, http.StatusBadRequest, err, nil)
		return
	}

	params, err := types.AddParamsFromQuery(r.URL.Query())
	if err != nil {
		api.sendResponse(w, http.StatusBadRequest, err, nil)
		return
	}

	api.setHeaders(w)

	// any errors sent as trailer
	adderutils.AddMultipartHTTPHandler(
		r.Context(),
		api.rpcClient,
		params,
		reader,
		w,
		nil,
	)
}
func AddMultipartHTTPHandler() {
	add := adder.New(dags, params, output)
	root, err := add.FromMultipart(ctx, reader)
	if err != nil {
		logger.Error(err) 
		// Set trailer with error
		w.Header().Set("X-Stream-Error", err.Error())
	}
	wg.Wait()
	return root, err
}
// FromMultipart adds content from a multipart.Reader. The adder will
// no longer be usable after calling this method.
func (a *Adder) FromMultipart(ctx context.Context, r *multipart.Reader) (cid.Cid, error) {
	logger.Debugf("adding from multipart with params: %+v", a.params)

	f, err := files.NewFileFromPartReader(r, "multipart/form-data")
	if err != nil {
		return cid.Undef, err
	}
	defer f.Close()
	return a.FromFiles(ctx, f)
}

最终生成 cid,返回给之前的 client 请求。


Nothing