Skip to content

Commit

Permalink
allow separate limiter for updates and posts, agressive default limit
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jan 17, 2019
1 parent 953ff03 commit 2a3051c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions backend/app/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type ServerCommand struct {
EditDuration time.Duration `long:"edit-time" env:"EDIT_TIME" default:"5m" description:"edit window"`
Port int `long:"port" env:"REMARK_PORT" default:"8080" description:"port"`
WebRoot string `long:"web-root" env:"REMARK_WEB_ROOT" default:"./web" description:"web root directory"`
UpdateLimit float64 `long:"update-limit" env:"UPDATE_LIMIT" default:"0.5" description:"updates/sec limit"`

Auth struct {
TTL struct {
Expand Down Expand Up @@ -269,6 +270,7 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) {
Cache: loadingCache,
NotifyService: notifyService,
SSLConfig: sslConfig,
UpdateLimiter: s.UpdateLimit,
}

srv.ScoreThresholds.Low, srv.ScoreThresholds.Critical = s.LowScore, s.CriticalScore
Expand Down
22 changes: 18 additions & 4 deletions backend/app/rest/api/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Rest struct {
Low int
Critical int
}
UpdateLimiter float64

SSLConfig SSLConfig
httpsServer *http.Server
Expand Down Expand Up @@ -229,16 +230,29 @@ func (s *Rest) routes() chi.Router {
rauth.Use(authMiddleware.Auth)
rauth.Use(logger.New(logger.Flags(logger.All), logger.Log(log.Default()),
logger.Prefix("[INFO]"), logger.IPfn(ipFn)).Handler)
rauth.Post("/comment", s.createCommentCtrl)
rauth.Put("/comment/{id}", s.updateCommentCtrl)
rauth.Get("/user", s.userInfoCtrl)
rauth.Put("/vote/{id}", s.voteCtrl)
rauth.Get("/userdata", s.userAllDataCtrl)
rauth.Post("/deleteme", s.deleteMeCtrl)

// admin routes, admin users only
rauth.Mount("/admin", s.adminService.routes(authMiddleware.AdminOnly))
})

// protected routes, throttled to 10/s by default, th
rapi.Group(func(rauth chi.Router) {
lmt := 10.0
if s.UpdateLimiter > 0 {
lmt = s.UpdateLimiter
}
rauth.Use(tollbooth_chi.LimitHandler(tollbooth.NewLimiter(lmt, nil)))
rauth.Use(authMiddleware.Auth)
rauth.Use(logger.New(logger.Flags(logger.All), logger.Log(log.Default()),
logger.Prefix("[DEBUG]"), logger.IPfn(ipFn)).Handler)

rauth.Put("/comment/{id}", s.updateCommentCtrl)
rauth.Post("/comment", s.createCommentCtrl)
rauth.Put("/vote/{id}", s.voteCtrl)
rauth.Post("/deleteme", s.deleteMeCtrl)
})
})

// respond to /robots.txt with the list of allowed paths
Expand Down

0 comments on commit 2a3051c

Please sign in to comment.