router.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package router
  2. import (
  3. "net"
  4. "net/http"
  5. "regexp"
  6. "strings"
  7. nanoid "github.com/matoous/go-nanoid/v2"
  8. log "github.com/sirupsen/logrus"
  9. )
  10. const (
  11. xRequestIDHeader = "X-Request-ID"
  12. )
  13. var (
  14. requestIDRe = regexp.MustCompile(`^[A-Za-z0-9_\-]+$`)
  15. )
  16. type RouteHandler func(string, http.ResponseWriter, *http.Request)
  17. type route struct {
  18. Method string
  19. Prefix string
  20. Handler RouteHandler
  21. Exact bool
  22. }
  23. type Router struct {
  24. prefix string
  25. Routes []*route
  26. }
  27. func (r *route) isMatch(req *http.Request) bool {
  28. if r.Method != req.Method {
  29. return false
  30. }
  31. if r.Exact {
  32. return req.URL.Path == r.Prefix
  33. }
  34. return strings.HasPrefix(req.URL.Path, r.Prefix)
  35. }
  36. func New(prefix string) *Router {
  37. return &Router{
  38. prefix: prefix,
  39. Routes: make([]*route, 0),
  40. }
  41. }
  42. func (r *Router) Add(method, prefix string, handler RouteHandler, exact bool) {
  43. r.Routes = append(
  44. r.Routes,
  45. &route{Method: method, Prefix: r.prefix + prefix, Handler: handler, Exact: exact},
  46. )
  47. }
  48. func (r *Router) GET(prefix string, handler RouteHandler, exact bool) {
  49. r.Add(http.MethodGet, prefix, handler, exact)
  50. }
  51. func (r *Router) OPTIONS(prefix string, handler RouteHandler, exact bool) {
  52. r.Add(http.MethodOptions, prefix, handler, exact)
  53. }
  54. func (r *Router) HEAD(prefix string, handler RouteHandler, exact bool) {
  55. r.Add(http.MethodHead, prefix, handler, exact)
  56. }
  57. func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
  58. req, timeoutCancel := startRequestTimer(req)
  59. defer timeoutCancel()
  60. reqID := req.Header.Get(xRequestIDHeader)
  61. if len(reqID) == 0 || !requestIDRe.MatchString(reqID) {
  62. reqID, _ = nanoid.New()
  63. }
  64. rw.Header().Set("Server", "imgproxy")
  65. rw.Header().Set(xRequestIDHeader, reqID)
  66. if ip := req.Header.Get("CF-Connecting-IP"); len(ip) != 0 {
  67. replaceRemoteAddr(req, ip)
  68. } else if ip := req.Header.Get("X-Forwarded-For"); len(ip) != 0 {
  69. if index := strings.Index(ip, ","); index > 0 {
  70. ip = ip[:index]
  71. }
  72. replaceRemoteAddr(req, ip)
  73. } else if ip := req.Header.Get("X-Real-IP"); len(ip) != 0 {
  74. replaceRemoteAddr(req, ip)
  75. }
  76. LogRequest(reqID, req)
  77. for _, rr := range r.Routes {
  78. if rr.isMatch(req) {
  79. rr.Handler(reqID, rw, req)
  80. return
  81. }
  82. }
  83. log.Warningf("Route for %s is not defined", req.URL.Path)
  84. rw.WriteHeader(404)
  85. }
  86. func replaceRemoteAddr(req *http.Request, ip string) {
  87. _, port, err := net.SplitHostPort(req.RemoteAddr)
  88. if err != nil {
  89. port = "80"
  90. }
  91. req.RemoteAddr = net.JoinHostPort(strings.TrimSpace(ip), port)
  92. }