[feat] 完善 Router.Call Router.Get 方法

This commit is contained in:
what 2024-11-26 17:35:11 +08:00
parent bccb0382d8
commit f1774e5ca2

View File

@ -80,8 +80,8 @@ type RouteMiddleware interface {
} }
type Router interface { type Router interface {
Call(r *http.Request, code string, params map[string]any, opts ...RouteFilterOption) (HttpResponse, error) Call(r *http.Request, code string, params map[string]any, opts ...RouteMatchOption) (HttpResponse, error)
Get(uuid string, opts ...RouteFilterOption) (Route, bool) Get(uuid string, opts ...RouteMatchOption) (Route, bool)
Register(cr chi.Router) Register(cr chi.Router)
RefreshRoutes() error RefreshRoutes() error
} }
@ -108,22 +108,22 @@ type RouteParam interface {
InjectRequestToGlobalParams(*http.Request, GlobalParams) error InjectRequestToGlobalParams(*http.Request, GlobalParams) error
} }
type RouteFilterOption func(opts *RouteFilterOptions) type RouteMatchOption func(opts *RouteMatches)
type RouteFilterOptions struct { type RouteMatches struct {
categories []RouteCategory categories []RouteCategory
services []RouteService services []RouteService
} }
func (this RouteFilterOptions) RouteCategories() []RouteCategory { func (this RouteMatches) RouteCategories() []RouteCategory {
return this.categories return this.categories
} }
func (this RouteFilterOptions) RouteServices() []RouteService { func (this RouteMatches) RouteServices() []RouteService {
return this.services return this.services
} }
func RouteFilter[T RouteCategory | RouteService](v T) RouteFilterOption { func RouteMatch[T RouteCategory | RouteService](v T) RouteMatchOption {
return func(opt *RouteFilterOptions) { return func(opt *RouteMatches) {
switch value := any(v).(type) { switch value := any(v).(type) {
case RouteCategory: case RouteCategory:
opt.categories = append(opt.categories, value) opt.categories = append(opt.categories, value)