-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.go
120 lines (119 loc) · 3.21 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package server
//import (
// "errors"
// "fmt"
// "net/http"
// "strings"
// "time"
//
// log "github.com/sirupsen/logrus"
// "github.com/didip/tollbooth"
// "github.com/gorilla/context"
// . "github.com/jgautheron/exago/src/api/config"
// "github.com/jgautheron/exago/src/api/requestlock"
// "github.com/julienschmidt/httprouter"
//)
//
//const (
// rateLimitCount = 20
//)
//
//var (
// ErrTooManyCalls = errors.New("Too many calls in a short period of time")
// ErrRateLimitExceeded = errors.New("Rate limit exceeded")
//)
//
//func (s *Server) recoverHandler(next http.Handler) http.Handler {
// fn := func(w http.ResponseWriter, r *http.Request) {
// defer func() {
// if err := recover(); err != nil {
// log.Errorf("panic: %+v", err)
// http.Error(w, http.StatusText(500), 500)
// }
// }()
//
// next.ServeHTTP(w, r)
// }
// return http.HandlerFunc(fn)
//}
//
//func (s *Server) checkValidRepository(next http.Handler) http.Handler {
// fn := func(w http.ResponseWriter, r *http.Request) {
// ps := context.Get(r, "params").(httprouter.Params)
// repo := ps.ByName("repository")[1:]
//
// data, err := s.config.RepositoryLoader.IsValid(repo)
// if err != nil {
// writeError(w, r, err)
// return
// }
//
// HTMLURL := strings.Replace(data["html_url"].(string), "https://", "", 1)
// rp := strings.Split(HTMLURL, "/")
// context.Set(r, "provider", rp[0])
// context.Set(r, "owner", rp[1])
// context.Set(r, "project", rp[2])
// context.Set(r, "repository", HTMLURL)
//
// rp = strings.Split(repo, "/")
// if len(rp) > 3 {
// context.Set(r, "path", strings.Join(rp[3:], "/"))
// }
//
// next.ServeHTTP(w, r)
// }
// return http.HandlerFunc(fn)
//}
//
//func (s *Server) requestLock(next http.Handler) http.Handler {
// fn := func(w http.ResponseWriter, r *http.Request) {
// ps := context.Get(r, "params").(httprouter.Params)
// rp := fmt.Sprintf("%s/%s/%s", ps.ByName("registry"), ps.ByName("owner"), ps.ByName("repository"))
//
// if requestlock.Contains(rp, getIP(r.RemoteAddr)) {
// writeError(w, r, ErrTooManyCalls)
// return
// }
//
// next.ServeHTTP(w, r)
// }
// return http.HandlerFunc(fn)
//}
//
//func (s *Server) setLogger(next http.Handler) http.Handler {
// fn := func(w http.ResponseWriter, r *http.Request) {
// ps := context.Get(r, "params").(httprouter.Params)
//
// lgr := logger.WithFields(log.Fields{
// "repository": ps.ByName("repository")[1:],
// "ip": getIP(r.RemoteAddr),
// })
// context.Set(r, "lgr", lgr)
// next.ServeHTTP(w, r)
// }
// return http.HandlerFunc(fn)
//}
//
//func (s *Server) rateLimit(next http.Handler) http.Handler {
// limiter := tollbooth.NewLimiter(rateLimitCount, time.Hour*4)
// limiter.Methods = []string{"GET"}
//
// fn := func(w http.ResponseWriter, r *http.Request) {
// lgr := context.Get(r, "lgr").(*log.Entry)
//
// if r.Header.Get("Origin") == Config.AllowOrigin {
// next.ServeHTTP(w, r)
// return
// }
//
// httpErr := tollbooth.LimitByRequest(limiter, r)
// if httpErr != nil {
// w.Header().Add("Content-Type", limiter.MessageContentType)
// lgr.WithField("URL", r.URL.String()).Error("Rate limit exceeded")
// writeError(w, r, ErrRateLimitExceeded)
// return
// }
// next.ServeHTTP(w, r)
// }
// return http.HandlerFunc(fn)
//}