You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

node.go 2.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package http
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "gitlink.org.cn/cloudream/common/consts/errorcode"
  6. "gitlink.org.cn/cloudream/common/pkgs/logger"
  7. cdssdk "gitlink.org.cn/cloudream/common/sdks/storage"
  8. )
  9. // NodeService 结构体代表了节点服务,它包含了一个Server实例。
  10. type NodeService struct {
  11. *Server
  12. }
  13. // NodeSvc 为Server结构体提供一个方法,返回一个NodeService的实例。
  14. // 这个方法主要用于在Server实例中访问NodeService。
  15. func (s *Server) NodeSvc() *NodeService {
  16. return &NodeService{
  17. Server: s,
  18. }
  19. }
  20. // GetNodesReq 结构体定义了获取节点信息请求的参数。
  21. // 它包含一个NodeIDs字段,该字段是需要查询的节点的ID列表,是必需的。
  22. type GetNodesReq struct {
  23. NodeIDs *[]cdssdk.NodeID `form:"nodeIDs" binding:"required"`
  24. }
  25. // GetNodesResp 结构体与cdssdk包中的NodeGetNodesResp类型相同,用于定义获取节点信息的响应。
  26. type GetNodesResp = cdssdk.NodeGetNodesResp
  27. // GetNodes 是一个处理获取节点信息请求的方法。
  28. // 它使用Gin框架的Context来处理HTTP请求,获取请求参数,并返回节点信息。
  29. // ctx *gin.Context: 代表当前的HTTP请求上下文。
  30. func (s *ObjectService) GetNodes(ctx *gin.Context) {
  31. // 初始化日志记录器,添加"HTTP"字段标识。
  32. log := logger.WithField("HTTP", "Node.GetNodes")
  33. var req GetNodesReq
  34. // 尝试绑定查询参数到请求结构体,如果出错则返回错误信息。
  35. if err := ctx.ShouldBindQuery(&req); err != nil {
  36. log.Warnf("binding body: %s", err.Error())
  37. // 参数绑定失败,返回400状态码和错误信息。
  38. ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
  39. return
  40. }
  41. // 调用NodeSvc获取节点信息,如果出错则返回操作失败的错误信息。
  42. nodes, err := s.svc.NodeSvc().GetNodes(*req.NodeIDs)
  43. if err != nil {
  44. log.Warnf("getting nodes: %s", err.Error())
  45. // 获取节点信息失败,返回操作失败的错误信息。
  46. ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get nodes failed"))
  47. return
  48. }
  49. // 节点信息获取成功,返回200状态码和节点信息。
  50. ctx.JSON(http.StatusOK, OK(GetNodesResp{Nodes: nodes}))
  51. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。