configCtrl.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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) Log(c *gin.Context) {
  17. common.GetLog("common").Info("test log")
  18. ctrl.RespOk(c, nil)
  19. }
  20. func (ctrl ConfigCtrl) All(c *gin.Context) {
  21. type retStruct struct {
  22. Name string `json:"name"`
  23. EndAt string `json:"end_at"`
  24. Config map[string]interface{} `json:"config"`
  25. }
  26. var ret retStruct
  27. req := &AllRequest{}
  28. err := c.BindQuery(&req)
  29. if err != nil {
  30. ctrl.RespError(c, common.New(common.ParamError))
  31. return
  32. }
  33. app := models.FindByAppKey(req.AppKey)
  34. if app == nil {
  35. c.AbortWithStatusJSON(404, map[string]string{"error": "不存在的app"})
  36. return
  37. }
  38. if app.EndAt.Before(time.Now()) {
  39. ctrl.RespError(c, common.New(common.Expired))
  40. return
  41. }
  42. conf := models.FindConfigByAppKey(app.ID, req.Env)
  43. json.Unmarshal(conf.Value, &ret.Config)
  44. ret.Name = app.Name
  45. ret.EndAt = app.EndAt.Format(time.DateTime)
  46. ctrl.RespOk(c, ret)
  47. }