diff --git a/go.mod b/go.mod index 1f0cd3b..42af7c4 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ module github.com/dubbogo/dubbo-go-pixiu-filter go 1.14 - -require github.com/alibaba/sentinel-golang v1.0.2 diff --git a/pkg/api/api.go b/pkg/api/api.go deleted file mode 100644 index 3aa7bc7..0000000 --- a/pkg/api/api.go +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package api - -import ( - "sync" -) - -var ( - CacheApi = sync.Map{} - EmptyApi = &API{} -) - -// API is api gateway concept, control request from browser、Mobile APP、third party people -type API struct { - Name string `json:"name" yaml:"name"` - ITypeStr string `json:"itype" yaml:"itype"` - IType ApiType `json:"-" yaml:"-"` - OTypeStr string `json:"otype" yaml:"otype"` - OType ApiType `json:"-" yaml:"-"` - Status Status `json:"status" yaml:"status"` - Metadata interface{} `json:"metadata" yaml:"metadata"` - Method string `json:"method" yaml:"method"` - RequestMethod `json:",omitempty" yaml:"-"` -} - -// NewApi -func NewApi() *API { - return &API{} -} - -// FindApi find a api, if not exist, return false -func (a *API) FindApi(name string) (*API, bool) { - if v, ok := CacheApi.Load(name); ok { - return v.(*API), true - } - - return nil, false -} - -// MatchMethod -func (a *API) MatchMethod(method string) bool { - i := RequestMethodValue[method] - if a.RequestMethod == RequestMethod(i) { - return true - } - - return false -} - -// IsOk api status equals Up -func (a *API) IsOk(name string) bool { - if v, ok := CacheApi.Load(name); ok { - return v.(*API).Status == Up - } - - return false -} - -// Offline api offline -func (a *API) Offline(name string) { - if v, ok := CacheApi.Load(name); ok { - v.(*API).Status = Down - } -} - -// Online api online -func (a *API) Online(name string) { - if v, ok := CacheApi.Load(name); ok { - v.(*API).Status = Up - } -} diff --git a/pkg/api/config/api_config.go b/pkg/api/config/api_config.go index eb63a94..ab684d3 100644 --- a/pkg/api/config/api_config.go +++ b/pkg/api/config/api_config.go @@ -21,10 +21,6 @@ import ( "time" ) -import ( - "github.com/dubbogo/dubbo-go-pixiu-filter/pkg/api/config/ratelimit" -) - // HTTPVerb defines the restful api http verb type HTTPVerb string @@ -59,39 +55,10 @@ const ( // APIConfig defines the data structure of the api gateway configuration type APIConfig struct { - Name string `json:"name" yaml:"name"` - Description string `json:"description" yaml:"description"` - Resources []Resource `json:"resources" yaml:"resources"` - Definitions []Definition `json:"definitions" yaml:"definitions"` - PluginFilePath string `json:"pluginFilePath" yaml:"pluginFilePath"` - PluginsGroup []PluginsGroup `json:"pluginsGroup" yaml:"pluginsGroup"` - RateLimit ratelimit.Config `json:"rateLimit" yaml:"rateLimit"` -} - -type Plugin struct { - ID int64 `json:"id,inline,omitempty" yaml:"id,omitempty"` - Name string `json:"name" yaml:"name"` - Version string `json:"version" yaml:"version"` - Priority int `json:"priority" yaml:"priority"` - ExternalLookupName string `json:"externalLookupName" yaml:"externalLookupName"` -} - -// PluginsGroup defines the plugins group info -type PluginsGroup struct { - ID int64 `json:"id,omitempty" yaml:"id,omitempty"` - GroupName string `json:"groupName" yaml:"groupName"` - Plugins []Plugin `json:"plugins" yaml:"plugins"` -} - -//PluginsConfig defines the pre & post plugins -type PluginsConfig struct { - PrePlugins PluginsInUse `json:"pre" yaml:"pre"` - PostPlugins PluginsInUse `json:"post" yaml:"post"` -} - -type PluginsInUse struct { - GroupNames []string `json:"groupNames" yaml:"groupNames"` - PluginNames []string `json:"pluginNames" yaml:"pluginNames"` + Name string `json:"name" yaml:"name"` + Description string `json:"description" yaml:"description"` + Resources []Resource `json:"resources" yaml:"resources"` + Definitions []Definition `json:"definitions" yaml:"definitions"` } // Resource defines the API path @@ -101,8 +68,7 @@ type Resource struct { Path string `json:"path" yaml:"path"` Timeout time.Duration `json:"timeout" yaml:"timeout"` Description string `json:"description" yaml:"description"` - Filters []string `json:"filters" yaml:"filters"` - Plugins PluginsConfig `json:"plugins" yaml:"plugins"` + Filters []Filter `json:"filters" yaml:"filters"` Methods []Method `json:"methods" yaml:"methods"` Resources []Resource `json:"resources,omitempty" yaml:"resources,omitempty"` Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"` @@ -137,6 +103,12 @@ func (r *Resource) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } +// Filter filter with config +type Filter struct { + Name string `json:"name,omitempty" yaml:"name,omitempty"` + Config map[string]interface{} `json:"config,omitempty" yaml:"config,omitempty"` +} + // Method defines the method of the api type Method struct { ID int `json:"id,omitempty" yaml:"id,omitempty"` @@ -144,7 +116,7 @@ type Method struct { Enable bool `json:"enable" yaml:"enable"` // true means the method is up and false means method is down Timeout time.Duration `json:"timeout" yaml:"timeout"` Mock bool `json:"mock" yaml:"mock"` - Filters []string `json:"filters" yaml:"filters"` + Filters []Filter `json:"filters" yaml:"filters"` HTTPVerb `json:"httpVerb" yaml:"httpVerb"` InboundRequest `json:"inboundRequest" yaml:"inboundRequest"` IntegrationRequest `json:"integrationRequest" yaml:"integrationRequest"` diff --git a/pkg/api/config/ratelimit/config.go b/pkg/api/config/ratelimit/config.go deleted file mode 100644 index e0cf620..0000000 --- a/pkg/api/config/ratelimit/config.go +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ratelimit - -import ( - "github.com/alibaba/sentinel-golang/core/flow" -) - -// Config rate limit config -type Config struct { - Resources []Resource `json:"resources" yaml:"resources"` - Rules []Rule `json:"rules" yaml:"rules"` - LogPath string `json:"logPath,omitempty" yaml:"logPath,omitempty"` -} - -// Resource API group for rate limit, all API in group is considered to be the same resource -type Resource struct { - ID int64 `json:"id,omitempty" yaml:"id,omitempty"` - Name string `json:"name,omitempty" yaml:"name,omitempty"` - Items []Item `json:"items" yaml:"items"` -} - -// Item API item for group -type Item struct { - MatchStrategy MatchStrategy `json:"matchStrategy,omitempty" yaml:"matchStrategy,omitempty"` - Pattern string `json:"pattern,omitempty" yaml:"pattern"` -} - -// Rule api group 's rate-limit rule -type Rule struct { - ID int64 `json:"id,omitempty" yaml:"id,omitempty"` - FlowRule flow.Rule `json:"flowRule,omitempty" yaml:"flowRule,omitempty"` - Enable bool `json:"enable,omitempty" yaml:"enable,omitempty"` -} - -// MatchStrategy API match strategy -type MatchStrategy int32 - -const ( - EXACT MatchStrategy = 0 - REGEX = 1 - ANT_PATH = 2 -) diff --git a/pkg/context/context.go b/pkg/context/context.go index 3839660..4dccf7f 100644 --- a/pkg/context/context.go +++ b/pkg/context/context.go @@ -18,7 +18,6 @@ package context import ( - "github.com/dubbogo/dubbo-go-pixiu-filter/pkg/api" "github.com/dubbogo/dubbo-go-pixiu-filter/pkg/router" ) @@ -27,7 +26,7 @@ type Context interface { Next() Abort() AbortWithError(string, error) - AppendFilterFunc(ff ...FilterFunc) + AppendFilter(ff ...func(Context)) Status(code int) StatusCode() int @@ -42,8 +41,6 @@ type Context interface { API(router.API) GetAPI() *router.API - Api(api *api.API) - GetApi() *api.API GetClientIP() string GetApplicationName() string diff --git a/pkg/context/filter.go b/pkg/context/filter.go deleted file mode 100644 index f16c34e..0000000 --- a/pkg/context/filter.go +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package context - -// FilterFunc filter func, filter -type FilterFunc func(Context) - -// FilterChain filter chain -type FilterChain []FilterFunc diff --git a/pkg/filter/filter.go b/pkg/filter/filter.go index 6ecaa04..3f017a0 100644 --- a/pkg/filter/filter.go +++ b/pkg/filter/filter.go @@ -19,10 +19,17 @@ package filter import "github.com/dubbogo/dubbo-go-pixiu-filter/pkg/context" -// Filter filter interface, used for context.FilterChain. -type Filter interface { - // Do run filter, use c.next() to next filter, before is pre logic, after is post logic. - Do() context.FilterFunc +// Filter filter func, filter +type Filter func(context.Context) + +// Factory filter Factory, for FilterFunc. filter manager will fill the Configuration from local or config center +type Factory interface { + // Config return the pointer of config + Config() interface{} + + // Apply return the filter func, initialize whatever you want before return the func + // use c.next() to next filter, before is pre logic, after is post logic. + Apply() (Filter, error) } // ErrResponse err response.