2018-07-07 20:10:56 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2018-09-28 08:54:13 +00:00
|
|
|
"git.xdrm.io/go/aicra/driver"
|
2018-07-07 20:10:56 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
// CreateRegistry creates an empty registry
|
|
|
|
func CreateRegistry() Registry {
|
|
|
|
return make(Registry)
|
|
|
|
}
|
2018-07-07 20:10:56 +00:00
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
// Add adds a new middleware for a path
|
|
|
|
func (reg Registry) Add(_path string, _element driver.Middleware) {
|
|
|
|
reg[_path] = _element
|
2018-07-07 20:10:56 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 23:34:21 +00:00
|
|
|
// Run executes all middlewares (default browse order)
|
2018-09-28 08:54:13 +00:00
|
|
|
func (reg Registry) Run(req http.Request) []string {
|
2018-07-07 20:10:56 +00:00
|
|
|
|
|
|
|
/* (1) Initialise scope */
|
2018-09-28 08:54:13 +00:00
|
|
|
scope := make([]string, 0)
|
2018-07-07 20:10:56 +00:00
|
|
|
|
|
|
|
/* (2) Execute each middleware */
|
2018-10-01 17:27:38 +00:00
|
|
|
for _, mw := range reg {
|
|
|
|
mw.Inspect(req, &scope)
|
2018-07-07 20:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return scope
|
|
|
|
|
|
|
|
}
|