app.go 824 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package controller
  2. import (
  3. "MeterService/controller/dto"
  4. "MeterService/core/api"
  5. "MeterService/core/utils"
  6. "MeterService/dataStruct"
  7. "MeterService/database/appApi"
  8. "github.com/gin-gonic/gin"
  9. )
  10. func RegisterApp(c *gin.Context) {
  11. app := &dto.App{}
  12. if err := c.BindJSON(app); err != nil {
  13. api.Fail(c, err.Error())
  14. return
  15. }
  16. db := appApi.NewAppApiDb()
  17. app.AppSecret = utils.GenerateRandomKey(32, 2)
  18. err := db.AddOrUpdate(app.ToDbApp())
  19. if err != nil {
  20. api.Fail(c, err.Error())
  21. return
  22. }
  23. apiMap := dataStruct.NewMapAppApi()
  24. apiMap.RemoveAppId(app.AppId)
  25. for _, v := range app.Apis {
  26. apiMap.Add(v.Type, &appApi.AppApi{
  27. AppId: app.AppId,
  28. Type: v.Type,
  29. Host: app.Host,
  30. Url: v.Url,
  31. })
  32. }
  33. resp := &dto.RespApp{
  34. AppId: app.AppId,
  35. AppSecret: app.AppSecret,
  36. }
  37. api.Ok2(c, resp)
  38. }