序
本文主要研究一下dubbo-go-proxy的apiFilter
apiFilter
dubbo-go-proxy/pkg/filter/api/api.go
func Init() {
extension.SetFilterFunc(constant.HTTPApiFilter, apiFilterFunc())
}
Init方法往extension设置了名为dgp.filters.http.api
的apiFilterFunc
apiFilterFunc
dubbo-go-proxy/pkg/filter/api/api.go
// apiFilterFunc url match api
func apiFilterFunc() context.FilterFunc {
return func(c context.Context) {
url := c.GetUrl()
method := c.GetMethod()
// [williamfeng323]TO-DO: get the API details from router which saved in constant.LocalMemoryApiDiscoveryService
if api, ok := api.EmptyApi.FindApi(url); ok {
if !api.MatchMethod(method) {
c.WriteWithStatus(http.StatusMethodNotAllowed, constant.Default405Body)
c.AddHeader(constant.HeaderKeyContextType, constant.HeaderValueTextPlain)
c.Abort()
return
}
if !api.IsOk(api.Name) {
c.WriteWithStatus(http.StatusNotAcceptable, constant.Default406Body)
c.AddHeader(constant.HeaderKeyContextType, constant.HeaderValueTextPlain)
c.Abort()
return
}
// [williamfeng323]TO-DO: the c.Api method need to be updated to use the newest API definition
c.Api(api)
c.Next()
} else {
c.WriteWithStatus(http.StatusNotFound, constant.Default404Body)
c.AddHeader(constant.HeaderKeyContextType, constant.HeaderValueTextPlain)
c.Abort()
}
}
}
apiFilterFunc首先通过api.EmptyApi.FindApi(url)来查找对应的api,找不到则返回404;找到的话则先通过api.MatchMethod判断method是否匹配,不匹配的话返回405;之后通过api.IsOk判断该api的status是否up,不是的话返回406;最后执行c.Api(api)及c.Next()。
dubbo-go-proxy-filter
dubbo-go-proxy-filter@v0.1.0-rc1.0.20210120132524-c63f4eb13725/pkg/api/api.go
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
}
api.go提供了FindApi、MatchMethod、IsOk方法
小结
apiFilterFunc首先通过api.EmptyApi.FindApi(url)来查找对应的api,找不到则返回404;找到的话则先通过api.MatchMethod判断method是否匹配,不匹配的话返回405;之后通过api.IsOk判断该api的status是否up,不是的话返回406;最后执行c.Api(api)及c.Next()。
doc
有疑问加站长微信联系(非本文作者)