configCtrl.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package controller
  2. import (
  3. "encoding/json"
  4. "github.com/gin-gonic/gin"
  5. "ppgo/common"
  6. models "ppgo/entity/model"
  7. "time"
  8. )
  9. type ConfigCtrl struct {
  10. common.BaseCtl
  11. }
  12. type AllRequest struct {
  13. AppKey string `form:"app_key" binding:"required"`
  14. Env string `form:"env" binding:"required"`
  15. }
  16. func (ctrl ConfigCtrl) All(c *gin.Context) {
  17. type retStruct struct {
  18. Name string `json:"name"`
  19. EndAt string `json:"end_at"`
  20. Config map[string]interface{} `json:"config"`
  21. }
  22. var ret retStruct
  23. req := &AllRequest{}
  24. err := c.BindQuery(&req)
  25. if err != nil {
  26. ctrl.RespError(c, common.New(common.ParamError))
  27. return
  28. }
  29. app := models.FindByAppKey(req.AppKey)
  30. if app == nil {
  31. c.AbortWithStatusJSON(404, map[string]string{"error": "不存在的app"})
  32. return
  33. }
  34. if app.EndAt.Before(time.Now()) {
  35. ctrl.RespError(c, common.New(common.Expired))
  36. return
  37. }
  38. conf := models.FindConfigByAppKey(app.ID, req.Env)
  39. json.Unmarshal(conf.Value, &ret.Config)
  40. ret.Name = app.Name
  41. ret.EndAt = app.EndAt.Format(time.DateTime)
  42. ctrl.RespOk(c, ret)
  43. }