bilibili-backup/library/net/http/blademaster/middleware/supervisor/supervisor.go

57 lines
1.0 KiB
Go
Raw Normal View History

2019-04-22 10:59:20 +08:00
package supervisor
import (
"time"
"go-common/library/ecode"
bm "go-common/library/net/http/blademaster"
)
// Config supervisor conf.
type Config struct {
On bool // all post/put/delete method off.
Begin time.Time // begin time
End time.Time // end time
}
// Supervisor supervisor midleware.
type Supervisor struct {
conf *Config
on bool
}
// New new and return supervisor midleware.
func New(c *Config) (s *Supervisor) {
s = &Supervisor{
conf: c,
}
s.Reload(c)
return
}
// Reload reload supervisor conf.
func (s *Supervisor) Reload(c *Config) {
if c == nil {
return
}
s.on = c.On && c.Begin.Before(c.End)
s.conf = c // NOTE datarace but no side effect.
}
func (s *Supervisor) ServeHTTP(c *bm.Context) {
if s.on {
now := time.Now()
method := c.Request.Method
if s.forbid(method, now) {
c.JSON(nil, ecode.ServiceUpdate)
c.Abort()
return
}
}
}
func (s *Supervisor) forbid(method string, now time.Time) bool {
// only allow GET request.
return method != "GET" && now.Before(s.conf.End) && now.After(s.conf.Begin)
}