cdn.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package controller
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "kng_feed_api/helper"
  7. "log"
  8. "net/http"
  9. "os"
  10. "path/filepath"
  11. "github.com/gin-gonic/gin"
  12. )
  13. type BitrixRequest struct {
  14. DIALOG_ID string `json:"DIALOG_ID"`
  15. MESSAGE string `json:"MESSAGE"`
  16. }
  17. func SendXmlFeedToClient(context *gin.Context) {
  18. var ra = context.Request.RemoteAddr
  19. msg := BitrixRequest{
  20. DIALOG_ID: "chat128124",
  21. MESSAGE: fmt.Sprintf("Был запрошен полый фид c ip адреса [%s]", ra),
  22. }
  23. marshalled, err := json.Marshal(msg)
  24. if err != nil {
  25. log.Printf("impossible to marshall teacher: %s\n", err)
  26. }
  27. sendBitrix24Log(marshalled)
  28. sendFile(context, "feed.xml")
  29. }
  30. func SendCsvDeltaToClient(context *gin.Context) {
  31. var ra = context.Request.RemoteAddr
  32. msg := BitrixRequest{
  33. DIALOG_ID: "chat128124",
  34. MESSAGE: fmt.Sprintf("Был запрошен delta-фид c ip адреса [%s]", ra),
  35. }
  36. marshalled, err := json.Marshal(msg)
  37. if err != nil {
  38. log.Printf("impossible to marshall teacher: %s\n", err)
  39. }
  40. sendBitrix24Log(marshalled)
  41. sendFile(context, "delta.csv")
  42. }
  43. func sendBitrix24Log(jsonStr []byte) {
  44. url := "https://kng.bitrix24.ru/rest/1/vu5dqvx5sifh1334/im.message.add"
  45. req, err := http.NewRequest("POST", url, bytes.NewReader(jsonStr))
  46. req.Header.Set("Content-Type", "application/json")
  47. client := &http.Client{}
  48. resp, err := client.Do(req)
  49. defer resp.Body.Close()
  50. if err != nil {
  51. fmt.Println(err)
  52. }
  53. }
  54. func sendFile(context *gin.Context, fileMark string) {
  55. var fileName = filepath.Join(helper.RootDir(), fileMark)
  56. _, err := os.Stat(fileName)
  57. if err != nil {
  58. if os.IsNotExist(err) {
  59. context.JSON(http.StatusInternalServerError,
  60. gin.H{
  61. "description": "The file does not exist yet. Contact the administrator.",
  62. "err": "ERR-001",
  63. },
  64. )
  65. return
  66. } else {
  67. context.JSON(http.StatusInternalServerError,
  68. gin.H{
  69. "description": err.Error(),
  70. "err": "unknown error",
  71. },
  72. )
  73. return
  74. }
  75. }
  76. context.FileAttachment(fileName, fileMark)
  77. context.Status(http.StatusOK)
  78. }