|
|
@ -21,22 +21,18 @@ const topCpuUsage = 1000
|
|
|
|
|
|
|
|
|
|
|
|
var ErrSignatureConfig = errors.New("bad config for Signature")
|
|
|
|
var ErrSignatureConfig = errors.New("bad config for Signature")
|
|
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
type engine struct {
|
|
|
|
Middleware func(next http.HandlerFunc) http.HandlerFunc
|
|
|
|
conf RestConf
|
|
|
|
|
|
|
|
routes []featuredRoutes
|
|
|
|
server struct {
|
|
|
|
unauthorizedCallback handler.UnauthorizedCallback
|
|
|
|
conf RtConf
|
|
|
|
unsignedCallback handler.UnsignedCallback
|
|
|
|
routes []featuredRoutes
|
|
|
|
middlewares []Middleware
|
|
|
|
unauthorizedCallback handler.UnauthorizedCallback
|
|
|
|
shedder load.Shedder
|
|
|
|
unsignedCallback handler.UnsignedCallback
|
|
|
|
priorityShedder load.Shedder
|
|
|
|
middlewares []Middleware
|
|
|
|
}
|
|
|
|
shedder load.Shedder
|
|
|
|
|
|
|
|
priorityShedder load.Shedder
|
|
|
|
func newEngine(c RestConf) *engine {
|
|
|
|
}
|
|
|
|
srv := &engine{
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func newServer(c RtConf) *server {
|
|
|
|
|
|
|
|
srv := &server{
|
|
|
|
|
|
|
|
conf: c,
|
|
|
|
conf: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.CpuThreshold > 0 {
|
|
|
|
if c.CpuThreshold > 0 {
|
|
|
@ -48,23 +44,23 @@ func newServer(c RtConf) *server {
|
|
|
|
return srv
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) AddRoutes(r featuredRoutes) {
|
|
|
|
func (s *engine) AddRoutes(r featuredRoutes) {
|
|
|
|
s.routes = append(s.routes, r)
|
|
|
|
s.routes = append(s.routes, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) SetUnauthorizedCallback(callback handler.UnauthorizedCallback) {
|
|
|
|
func (s *engine) SetUnauthorizedCallback(callback handler.UnauthorizedCallback) {
|
|
|
|
s.unauthorizedCallback = callback
|
|
|
|
s.unauthorizedCallback = callback
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) SetUnsignedCallback(callback handler.UnsignedCallback) {
|
|
|
|
func (s *engine) SetUnsignedCallback(callback handler.UnsignedCallback) {
|
|
|
|
s.unsignedCallback = callback
|
|
|
|
s.unsignedCallback = callback
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) Start() error {
|
|
|
|
func (s *engine) Start() error {
|
|
|
|
return s.StartWithRouter(router.NewPatRouter())
|
|
|
|
return s.StartWithRouter(router.NewPatRouter())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) StartWithRouter(router router.Router) error {
|
|
|
|
func (s *engine) StartWithRouter(router router.Router) error {
|
|
|
|
if err := s.bindRoutes(router); err != nil {
|
|
|
|
if err := s.bindRoutes(router); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -72,7 +68,7 @@ func (s *server) StartWithRouter(router router.Router) error {
|
|
|
|
return internal.StartHttp(s.conf.Host, s.conf.Port, router)
|
|
|
|
return internal.StartHttp(s.conf.Host, s.conf.Port, router)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) appendAuthHandler(fr featuredRoutes, chain alice.Chain,
|
|
|
|
func (s *engine) appendAuthHandler(fr featuredRoutes, chain alice.Chain,
|
|
|
|
verifier func(alice.Chain) alice.Chain) alice.Chain {
|
|
|
|
verifier func(alice.Chain) alice.Chain) alice.Chain {
|
|
|
|
if fr.jwt.enabled {
|
|
|
|
if fr.jwt.enabled {
|
|
|
|
if len(fr.jwt.prevSecret) == 0 {
|
|
|
|
if len(fr.jwt.prevSecret) == 0 {
|
|
|
@ -88,7 +84,7 @@ func (s *server) appendAuthHandler(fr featuredRoutes, chain alice.Chain,
|
|
|
|
return verifier(chain)
|
|
|
|
return verifier(chain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) bindFeaturedRoutes(router router.Router, fr featuredRoutes, metrics *stat.Metrics) error {
|
|
|
|
func (s *engine) bindFeaturedRoutes(router router.Router, fr featuredRoutes, metrics *stat.Metrics) error {
|
|
|
|
verifier, err := s.signatureVerifier(fr.signature)
|
|
|
|
verifier, err := s.signatureVerifier(fr.signature)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -103,7 +99,7 @@ func (s *server) bindFeaturedRoutes(router router.Router, fr featuredRoutes, met
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) bindRoute(fr featuredRoutes, router router.Router, metrics *stat.Metrics,
|
|
|
|
func (s *engine) bindRoute(fr featuredRoutes, router router.Router, metrics *stat.Metrics,
|
|
|
|
route Route, verifier func(chain alice.Chain) alice.Chain) error {
|
|
|
|
route Route, verifier func(chain alice.Chain) alice.Chain) error {
|
|
|
|
chain := alice.New(
|
|
|
|
chain := alice.New(
|
|
|
|
handler.TracingHandler,
|
|
|
|
handler.TracingHandler,
|
|
|
@ -128,7 +124,7 @@ func (s *server) bindRoute(fr featuredRoutes, router router.Router, metrics *sta
|
|
|
|
return router.Handle(route.Method, route.Path, handle)
|
|
|
|
return router.Handle(route.Method, route.Path, handle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) bindRoutes(router router.Router) error {
|
|
|
|
func (s *engine) bindRoutes(router router.Router) error {
|
|
|
|
metrics := s.createMetrics()
|
|
|
|
metrics := s.createMetrics()
|
|
|
|
|
|
|
|
|
|
|
|
for _, fr := range s.routes {
|
|
|
|
for _, fr := range s.routes {
|
|
|
@ -140,7 +136,7 @@ func (s *server) bindRoutes(router router.Router) error {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) createMetrics() *stat.Metrics {
|
|
|
|
func (s *engine) createMetrics() *stat.Metrics {
|
|
|
|
var metrics *stat.Metrics
|
|
|
|
var metrics *stat.Metrics
|
|
|
|
|
|
|
|
|
|
|
|
if len(s.conf.Name) > 0 {
|
|
|
|
if len(s.conf.Name) > 0 {
|
|
|
@ -152,7 +148,7 @@ func (s *server) createMetrics() *stat.Metrics {
|
|
|
|
return metrics
|
|
|
|
return metrics
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) getLogHandler() func(http.Handler) http.Handler {
|
|
|
|
func (s *engine) getLogHandler() func(http.Handler) http.Handler {
|
|
|
|
if s.conf.Verbose {
|
|
|
|
if s.conf.Verbose {
|
|
|
|
return handler.DetailedLogHandler
|
|
|
|
return handler.DetailedLogHandler
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -160,14 +156,14 @@ func (s *server) getLogHandler() func(http.Handler) http.Handler {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) getShedder(priority bool) load.Shedder {
|
|
|
|
func (s *engine) getShedder(priority bool) load.Shedder {
|
|
|
|
if priority && s.priorityShedder != nil {
|
|
|
|
if priority && s.priorityShedder != nil {
|
|
|
|
return s.priorityShedder
|
|
|
|
return s.priorityShedder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.shedder
|
|
|
|
return s.shedder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) signatureVerifier(signature signatureSetting) (func(chain alice.Chain) alice.Chain, error) {
|
|
|
|
func (s *engine) signatureVerifier(signature signatureSetting) (func(chain alice.Chain) alice.Chain, error) {
|
|
|
|
if !signature.enabled {
|
|
|
|
if !signature.enabled {
|
|
|
|
return func(chain alice.Chain) alice.Chain {
|
|
|
|
return func(chain alice.Chain) alice.Chain {
|
|
|
|
return chain
|
|
|
|
return chain
|
|
|
@ -207,7 +203,7 @@ func (s *server) signatureVerifier(signature signatureSetting) (func(chain alice
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *server) use(middleware Middleware) {
|
|
|
|
func (s *engine) use(middleware Middleware) {
|
|
|
|
s.middlewares = append(s.middlewares, middleware)
|
|
|
|
s.middlewares = append(s.middlewares, middleware)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|