Chore: replace macaron with web package (#40136)

* replace macaron with web package

* add web.go
This commit is contained in:
Serge Zaitsev
2021-10-11 14:30:59 +02:00
committed by GitHub
parent 78ebac0116
commit 57fcfd578d
70 changed files with 369 additions and 375 deletions
+26 -26
View File
@@ -6,41 +6,41 @@ import (
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/setting"
"gopkg.in/macaron.v1"
"github.com/grafana/grafana/pkg/web"
)
type Router interface {
Handle(method, pattern string, handlers []macaron.Handler)
Get(pattern string, handlers ...macaron.Handler)
Handle(method, pattern string, handlers []web.Handler)
Get(pattern string, handlers ...web.Handler)
}
// RouteRegister allows you to add routes and macaron.Handlers
// RouteRegister allows you to add routes and web.Handlers
// that the web server should serve.
type RouteRegister interface {
// Get adds a list of handlers to a given route with a GET HTTP verb
Get(string, ...macaron.Handler)
Get(string, ...web.Handler)
// Post adds a list of handlers to a given route with a POST HTTP verb
Post(string, ...macaron.Handler)
Post(string, ...web.Handler)
// Delete adds a list of handlers to a given route with a DELETE HTTP verb
Delete(string, ...macaron.Handler)
Delete(string, ...web.Handler)
// Put adds a list of handlers to a given route with a PUT HTTP verb
Put(string, ...macaron.Handler)
Put(string, ...web.Handler)
// Patch adds a list of handlers to a given route with a PATCH HTTP verb
Patch(string, ...macaron.Handler)
Patch(string, ...web.Handler)
// Any adds a list of handlers to a given route with any HTTP verb
Any(string, ...macaron.Handler)
Any(string, ...web.Handler)
// Group allows you to pass a function that can add multiple routes
// with a shared prefix route.
Group(string, func(RouteRegister), ...macaron.Handler)
Group(string, func(RouteRegister), ...web.Handler)
// Insert adds more routes to an existing Group.
Insert(string, func(RouteRegister), ...macaron.Handler)
Insert(string, func(RouteRegister), ...web.Handler)
// Register iterates over all routes added to the RouteRegister
// and add them to the `Router` pass as an parameter.
@@ -50,7 +50,7 @@ type RouteRegister interface {
Reset()
}
type RegisterNamedMiddleware func(name string) macaron.Handler
type RegisterNamedMiddleware func(name string) web.Handler
func ProvideRegister(cfg *setting.Cfg) *RouteRegisterImpl {
return NewRouteRegister(middleware.ProvideRouteOperationName, middleware.RequestMetrics(cfg))
@@ -61,7 +61,7 @@ func NewRouteRegister(namedMiddlewares ...RegisterNamedMiddleware) *RouteRegiste
return &RouteRegisterImpl{
prefix: "",
routes: []route{},
subfixHandlers: []macaron.Handler{},
subfixHandlers: []web.Handler{},
namedMiddlewares: namedMiddlewares,
}
}
@@ -69,12 +69,12 @@ func NewRouteRegister(namedMiddlewares ...RegisterNamedMiddleware) *RouteRegiste
type route struct {
method string
pattern string
handlers []macaron.Handler
handlers []web.Handler
}
type RouteRegisterImpl struct {
prefix string
subfixHandlers []macaron.Handler
subfixHandlers []web.Handler
namedMiddlewares []RegisterNamedMiddleware
routes []route
groups []*RouteRegisterImpl
@@ -90,7 +90,7 @@ func (rr *RouteRegisterImpl) Reset() {
rr.subfixHandlers = nil
}
func (rr *RouteRegisterImpl) Insert(pattern string, fn func(RouteRegister), handlers ...macaron.Handler) {
func (rr *RouteRegisterImpl) Insert(pattern string, fn func(RouteRegister), handlers ...web.Handler) {
// loop over all groups at current level
for _, g := range rr.groups {
// apply routes if the prefix matches the pattern
@@ -106,7 +106,7 @@ func (rr *RouteRegisterImpl) Insert(pattern string, fn func(RouteRegister), hand
}
}
func (rr *RouteRegisterImpl) Group(pattern string, fn func(rr RouteRegister), handlers ...macaron.Handler) {
func (rr *RouteRegisterImpl) Group(pattern string, fn func(rr RouteRegister), handlers ...web.Handler) {
group := &RouteRegisterImpl{
prefix: rr.prefix + pattern,
subfixHandlers: append(rr.subfixHandlers, handlers...),
@@ -135,8 +135,8 @@ func (rr *RouteRegisterImpl) Register(router Router) {
}
}
func (rr *RouteRegisterImpl) route(pattern, method string, handlers ...macaron.Handler) {
h := make([]macaron.Handler, 0)
func (rr *RouteRegisterImpl) route(pattern, method string, handlers ...web.Handler) {
h := make([]web.Handler, 0)
fullPattern := rr.prefix + pattern
for _, fn := range rr.namedMiddlewares {
@@ -159,26 +159,26 @@ func (rr *RouteRegisterImpl) route(pattern, method string, handlers ...macaron.H
})
}
func (rr *RouteRegisterImpl) Get(pattern string, handlers ...macaron.Handler) {
func (rr *RouteRegisterImpl) Get(pattern string, handlers ...web.Handler) {
rr.route(pattern, http.MethodGet, handlers...)
}
func (rr *RouteRegisterImpl) Post(pattern string, handlers ...macaron.Handler) {
func (rr *RouteRegisterImpl) Post(pattern string, handlers ...web.Handler) {
rr.route(pattern, http.MethodPost, handlers...)
}
func (rr *RouteRegisterImpl) Delete(pattern string, handlers ...macaron.Handler) {
func (rr *RouteRegisterImpl) Delete(pattern string, handlers ...web.Handler) {
rr.route(pattern, http.MethodDelete, handlers...)
}
func (rr *RouteRegisterImpl) Put(pattern string, handlers ...macaron.Handler) {
func (rr *RouteRegisterImpl) Put(pattern string, handlers ...web.Handler) {
rr.route(pattern, http.MethodPut, handlers...)
}
func (rr *RouteRegisterImpl) Patch(pattern string, handlers ...macaron.Handler) {
func (rr *RouteRegisterImpl) Patch(pattern string, handlers ...web.Handler) {
rr.route(pattern, http.MethodPatch, handlers...)
}
func (rr *RouteRegisterImpl) Any(pattern string, handlers ...macaron.Handler) {
func (rr *RouteRegisterImpl) Any(pattern string, handlers ...web.Handler) {
rr.route(pattern, "*", handlers...)
}
+7 -7
View File
@@ -5,14 +5,14 @@ import (
"strconv"
"testing"
"gopkg.in/macaron.v1"
"github.com/grafana/grafana/pkg/web"
)
type fakeRouter struct {
route []route
}
func (fr *fakeRouter) Handle(method, pattern string, handlers []macaron.Handler) {
func (fr *fakeRouter) Handle(method, pattern string, handlers []web.Handler) {
fr.route = append(fr.route, route{
pattern: pattern,
method: method,
@@ -20,7 +20,7 @@ func (fr *fakeRouter) Handle(method, pattern string, handlers []macaron.Handler)
})
}
func (fr *fakeRouter) Get(pattern string, handlers ...macaron.Handler) {
func (fr *fakeRouter) Get(pattern string, handlers ...web.Handler) {
fr.route = append(fr.route, route{
pattern: pattern,
method: http.MethodGet,
@@ -28,15 +28,15 @@ func (fr *fakeRouter) Get(pattern string, handlers ...macaron.Handler) {
})
}
func emptyHandlers(n int) []macaron.Handler {
var res []macaron.Handler
func emptyHandlers(n int) []web.Handler {
var res []web.Handler
for i := 1; n >= i; i++ {
res = append(res, emptyHandler(strconv.Itoa(i)))
}
return res
}
func emptyHandler(name string) macaron.Handler {
func emptyHandler(name string) web.Handler {
return struct{ name string }{name: name}
}
@@ -212,7 +212,7 @@ func TestNamedMiddlewareRouteRegister(t *testing.T) {
namedMiddlewares := map[string]bool{}
// Setup
rr := NewRouteRegister(func(name string) macaron.Handler {
rr := NewRouteRegister(func(name string) web.Handler {
namedMiddlewares[name] = true
return struct{ name string }{name: name}
+2 -2
View File
@@ -3,7 +3,7 @@ package routing
import (
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
"gopkg.in/macaron.v1"
"github.com/grafana/grafana/pkg/web"
)
var (
@@ -12,7 +12,7 @@ var (
}
)
func Wrap(action interface{}) macaron.Handler {
func Wrap(action interface{}) web.Handler {
return func(c *models.ReqContext) {
var res response.Response
val, err := c.Invoke(action)