gin_core.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package http_middleware
  2. import (
  3. "fmt"
  4. "git.aionnect.com/aionnect/go-common/utils/jws"
  5. "github.com/gin-gonic/gin"
  6. "regexp"
  7. "strings"
  8. )
  9. var tsPostfixPattern = regexp.MustCompile(`[?&](t=\d+&?)`) // 处理防重放防缓存的时间戳参数的正则
  10. // 日志和错误处理需要记录日志中的请求信息处理
  11. func splitUri(hostPrefix string, ctxt *gin.Context) (string, map[string]interface{}) {
  12. path := ctxt.Request.URL.Path
  13. method := ctxt.Request.Method
  14. raw := ctxt.Request.URL.RawQuery
  15. fields := make(map[string]interface{})
  16. uri := path
  17. if len(strings.TrimSpace(hostPrefix)) > 0 && !getHostPrefixPattern(hostPrefix).MatchString(path) {
  18. uri = strings.TrimSpace(hostPrefix) + "/" + strings.TrimLeft(path, "/")
  19. }
  20. query := string(raw)
  21. if sub := tsPostfixPattern.FindStringSubmatch(query); nil != sub && len(sub) > 1 {
  22. query = strings.Replace(query, sub[1], "", -1)
  23. }
  24. if raw != "" {
  25. path = uri + "?" + raw
  26. }
  27. if token := jws.GetToken(ctxt); len(token) > 0 {
  28. fields["token"] = token
  29. }
  30. fields["endpoint"] = fmt.Sprintf("%s %s", method, uri) // GET /service/test/test
  31. fields["uri"] = uri // /service/test/test
  32. fields["query"] = query // name=test&sex=1
  33. fields["method"] = method // GET
  34. req := fmt.Sprintf("%s %s", method, path) // GET /service/test/test?name=test&sex=1&t=32523535323
  35. return req, fields
  36. }
  37. func getHostPrefixPattern(hostPrefix string) *regexp.Regexp {
  38. return regexp.MustCompile("^" + strings.TrimSpace(hostPrefix)) // 处理子系统路径前缀的正则
  39. }