configfile.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package controller
  2. import (
  3. "fmt"
  4. "git.cestong.com.cn/cecf/cecf-golib/pkg/apollo"
  5. "git.cestong.com.cn/cecf/cecf-golib/pkg/errors"
  6. gin2 "git.cestong.com.cn/cecf/cecf-golib/pkg/gin"
  7. "git.cestong.com.cn/cecf/config-center-server/pkg/model"
  8. "github.com/gin-gonic/gin"
  9. "github.com/go-playground/validator/v10"
  10. "gorm.io/gorm"
  11. )
  12. type ConfigFileController struct {
  13. db *gorm.DB
  14. val *validator.Validate
  15. apolloCli *apollo.HTTPClient
  16. }
  17. func NewConfigFileController(db *gorm.DB, val *validator.Validate, apolloCli *apollo.HTTPClient) *ConfigFileController {
  18. return &ConfigFileController{db: db, val: val, apolloCli: apolloCli}
  19. }
  20. type ConfigFileReqVO struct {
  21. Name string `json:"name" validate:"min=1"`
  22. Desc string `json:"desc"`
  23. Format string `json:"format" validate:"oneof=ini"`
  24. DefaultPath string `json:"defaultPath" validate:"min=1"`
  25. }
  26. func (cf *ConfigFileController) Create(c *gin.Context) (interface{}, error) {
  27. // req
  28. appIDStr := c.Param("appID")
  29. var req ConfigFileReqVO
  30. if errBind := c.ShouldBindJSON(&req); errBind != nil {
  31. return nil, errors.ErrBadRequest
  32. }
  33. if errVal := cf.val.Struct(&req); errVal != nil {
  34. return nil, errors.ErrBadRequest
  35. }
  36. // apollo req build
  37. apolloID := appIDStr
  38. apolloNamespace := fmt.Sprintf("%s.%s", req.Name, "properties")
  39. // filters
  40. {
  41. var sameNameCount int64
  42. if errCount := cf.db.Model(&model.ConfigFileModel{}).
  43. Where("apollo_app_id=? and apollo_namespace=?", apolloID, apolloNamespace).
  44. Count(&sameNameCount).Error; errCount != nil {
  45. return nil, errCount
  46. }
  47. if sameNameCount > 0 {
  48. return nil, errors.Wrap(errors.ErrBadRequest, "名称(%s)重复", req.Name)
  49. }
  50. }
  51. // get app model from db
  52. var appModel model.AppModel
  53. {
  54. if errGetAppModel := cf.db.First(&appModel, "apollo_app_id=?", appIDStr).Error; errGetAppModel != nil {
  55. if errGetAppModel == gorm.ErrRecordNotFound {
  56. return nil, errors.Wrap(errors.ErrBadRequest, "appID(%s)未找到")
  57. }
  58. return nil, errGetAppModel
  59. }
  60. }
  61. // apollo create
  62. if errCreateApollo := cf.apolloCli.CreateNameSpace(apolloID, req.Name); errCreateApollo != nil {
  63. return nil, errCreateApollo
  64. }
  65. var m model.ConfigFileModel
  66. m.AppModelID = appModel.ID
  67. m.ApolloAppID = appIDStr
  68. m.Name = req.Name
  69. m.Format = req.Format
  70. m.Desc = req.Desc
  71. m.ApolloNamespace = apolloNamespace
  72. m.DefaultPath = req.DefaultPath
  73. // save to db
  74. if errCreate := cf.db.Create(&m).Error; errCreate != nil {
  75. return nil, errCreate
  76. }
  77. // resp
  78. return gin2.SuccessGeneralVO(), nil
  79. }