req/routing.go

135 lines
3.5 KiB
Go
Raw Normal View History

2024-05-08 21:35:26 +08:00
package req
import (
"net/http"
"github.com/go-chi/chi/v5"
)
type RouteMethod string
type RouteCategory string
type RouteService string
type RouteParamType string
type RouteParamCategory string
type RouteCtx struct {
Name string
}
const (
GET RouteMethod = "GET"
POST RouteMethod = "POST"
PUT RouteMethod = "PUT"
DELETE RouteMethod = "DELETE"
)
const (
WS RouteCategory = "ws"
GRPC RouteCategory = "grpc"
FUNC RouteCategory = "func"
SHOW RouteCategory = "show"
QUERY RouteCategory = "query"
STORE RouteCategory = "store"
DESTROY RouteCategory = "destroy"
STRUCTURE RouteCategory = "structure"
)
const (
API RouteService = "api"
FSM RouteService = "fsm"
LIST RouteService = "list"
XLSX RouteService = "xlsx"
IMPORT RouteService = "import"
PRINT RouteService = "print"
LAYOUT RouteService = "layout"
GRID_LAYOUT RouteService = "grid-layout"
GRID_LAYOUT_FORM RouteService = "grid-layout-form"
ECHART RouteService = "echart"
APPROVAL RouteService = "approval"
SELECTOR RouteService = "selector"
LIST_DETAIL RouteService = "list-detail"
LIST_DATA_STORE RouteService = "list-data-store"
LIST_GRID_LAYOUT RouteService = "list-grid-layout"
LIST_CONDITION_LAYOUT RouteService = "list-condition-layout"
LIST_OPERATIONS_ACCESS RouteService = "list-operations-access"
)
const (
2024-05-09 11:35:23 +08:00
ReqString RouteParamType = "string"
ReqBool RouteParamType = "bool"
ReqNumber RouteParamType = "number"
ReqInteger RouteParamType = "integer"
ReqFloat RouteParamType = "float"
ReqJson RouteParamType = "json"
ReqArray RouteParamType = "array"
ReqAny RouteParamType = "any"
2024-05-08 21:35:26 +08:00
)
const (
ROUTER RouteParamCategory = "router"
HEADER RouteParamCategory = "header"
PARAM RouteParamCategory = "param"
)
func (k RouteCtx) String() string {
return k.Name
}
type RouteMiddleware interface {
HttpMiddleware(r Route) func(next http.Handler) http.Handler
}
type Router interface {
Call(r *http.Request, code string, params map[string]any, opts ...RouteFilterOption) (HttpResponse, error)
Get(uuid string, opts ...RouteFilterOption) (Route, bool)
2024-05-08 21:35:26 +08:00
Register(cr chi.Router)
RefreshRoutes() error
}
type Route interface {
GetUuid() string
GetCode() string
GetPrimaryKey() string
GetUri() string
GetUris() []string
GetParamValues(*http.Request, ...RouteParam) (GlobalParams, error)
GetResource() Resource
GetCategory() RouteCategory
GetService() RouteService
GetRoles() []string
MakeRequest(r *http.Request, params map[string]any) (*http.Request, error)
}
type RouteParam interface {
GetCode() string
GetDataType() RouteParamType
GetCategory() RouteParamCategory
IsRequired() bool
InjectRequestToGlobalParams(*http.Request, GlobalParams) error
}
type RouteFilterOption func(opts *RouteFilterOptions)
type RouteFilterOptions struct {
categories []RouteCategory
services []RouteService
}
func (this RouteFilterOptions) RouteCategories() []RouteCategory {
return this.categories
}
func (this RouteFilterOptions) RouteServices() []RouteService {
return this.services
}
func RouteFilter[T RouteCategory | RouteService](v T) RouteFilterOption {
return func(opt *RouteFilterOptions) {
switch value := any(v).(type) {
case RouteCategory:
opt.categories = append(opt.categories, value)
case RouteService:
opt.services = append(opt.services, value)
}
}
}