From 1d0fee79a3908ea77109ee051fcb4913d2f92ec4 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 15 May 2018 10:28:01 -0500 Subject: [PATCH] avatar proxy - allow more requests and turn logging off --- app/rest/api/middleware.go | 7 +++++++ app/rest/api/rest.go | 6 +++++- app/rest/avatar/avatar.go | 3 ++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/rest/api/middleware.go b/app/rest/api/middleware.go index b57aa9ef..3bdbb9ae 100644 --- a/app/rest/api/middleware.go +++ b/app/rest/api/middleware.go @@ -77,6 +77,7 @@ const ( LogAll LoggerFlag = iota LogUser LogBody + LogNone ) const maxBody = 1024 @@ -97,6 +98,12 @@ func Logger(flags ...LoggerFlag) func(http.Handler) http.Handler { f := func(h http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { + + if inFlags(LogNone) { // skip logging + h.ServeHTTP(w, r) + return + } + ww := middleware.NewWrapResponseWriter(w, 1) body, user := func() (body string, user string) { diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index af7912bf..e0eb3226 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -82,7 +82,11 @@ func (s *Rest) Run(port int) { } }) - router.Mount(s.Authenticator.AvatarProxy.Routes()) // mount avatars controller to /api/v1/avatar/{file.img} + avatarMiddlewares := []func(http.Handler) http.Handler{ + Logger(LogNone), + tollbooth_chi.LimitHandler(tollbooth.NewLimiter(100, nil)), + } + router.Mount(s.Authenticator.AvatarProxy.Routes(avatarMiddlewares...)) // mount avatars controller to /api/v1/avatar/{file.img} // api routes router.Route("/api/v1", func(rapi chi.Router) { diff --git a/app/rest/avatar/avatar.go b/app/rest/avatar/avatar.go index 79753631..c18d2fe5 100644 --- a/app/rest/avatar/avatar.go +++ b/app/rest/avatar/avatar.go @@ -85,8 +85,9 @@ func (p *Proxy) Put(u store.User) (avatarURL string, err error) { } // Routes returns auth routes for given provider -func (p *Proxy) Routes() (string, chi.Router) { +func (p *Proxy) Routes(middlewares ...func(http.Handler) http.Handler) (string, chi.Router) { router := chi.NewRouter() + router.Use(middlewares...) // GET /123456789.image router.Get("/{avatar}", func(w http.ResponseWriter, r *http.Request) {