router.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package server
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/giuem/ga-proxy/ga"
  6. )
  7. func handlePageView(c *gin.Context) {
  8. if len(c.Request.Referer()) == 0 || len(c.Query("ga")) == 0 {
  9. handleRedirect(c)
  10. return
  11. }
  12. c.Status(http.StatusOK)
  13. c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
  14. go ga.PageView(getCommonData(c))
  15. }
  16. func handleTiming(c *gin.Context) {
  17. if len(c.Request.Referer()) == 0 || len(c.Query("ga")) == 0 {
  18. handleRedirect(c)
  19. return
  20. }
  21. c.Status(http.StatusOK)
  22. c.Header("Cache-Control", "no-cache, no-store, must-revalidate")
  23. go ga.Timing(getCommonData(c), getTimingData(c))
  24. }
  25. func handlePing(c *gin.Context) {
  26. err := ga.Detect()
  27. if err != nil {
  28. if c.Request.Method == http.MethodHead {
  29. c.Status(http.StatusBadGateway)
  30. } else {
  31. c.JSON(http.StatusBadGateway, gin.H{"msg": err.Error()})
  32. }
  33. return
  34. }
  35. if c.Request.Method == http.MethodHead {
  36. c.Status(http.StatusOK)
  37. } else {
  38. c.JSON(http.StatusOK, gin.H{"msg": "ok"})
  39. }
  40. }
  41. func handleRedirect(c *gin.Context) {
  42. c.Redirect(http.StatusFound, "https://github.com/giuem/ga-proxy")
  43. }