cdn.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package controller
  2. import (
  3. "bytes"
  4. "fmt"
  5. "kng_feed_api/helper"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "github.com/gin-gonic/gin"
  10. )
  11. func SendXmlFeedToClient(context *gin.Context) {
  12. var ra = context.Request.RemoteAddr
  13. var reqUrl = context.Request.RequestURI
  14. var preString = fmt.Sprintf(`"DIALOG_ID":%s, "MESSAGE":"Запрошен %s c ip адреса [%s]"`, "", reqUrl, ra)
  15. var jsonStr = []byte(preString)
  16. sendBitrix24Log(jsonStr)
  17. sendFile(context, "feed.xml")
  18. }
  19. func SendCsvDeltaToClient(context *gin.Context) {
  20. sendFile(context, "delta.csv")
  21. }
  22. func sendBitrix24Log(jsonStr []byte) {
  23. url := "https://kng.bitrix24.ru/rest/1/vu5dqvx5sifh1334/im.message.add"
  24. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  25. req.Header.Set("Content-Type", "application/json")
  26. client := &http.Client{}
  27. resp, err := client.Do(req)
  28. defer resp.Body.Close()
  29. if err != nil {
  30. fmt.Println(err)
  31. }
  32. }
  33. func sendFile(context *gin.Context, fileMark string) {
  34. var fileName = filepath.Join(helper.RootDir(), fileMark)
  35. _, err := os.Stat(fileName)
  36. if err != nil {
  37. if os.IsNotExist(err) {
  38. context.JSON(http.StatusInternalServerError,
  39. gin.H{
  40. "description": "The file does not exist yet. Contact the administrator.",
  41. "err": "ERR-001",
  42. },
  43. )
  44. return
  45. } else {
  46. context.JSON(http.StatusInternalServerError,
  47. gin.H{
  48. "description": err.Error(),
  49. "err": "unknown error",
  50. },
  51. )
  52. return
  53. }
  54. }
  55. context.FileAttachment(fileName, fileMark)
  56. context.Status(http.StatusOK)
  57. }