add router component

This commit is contained in:
ginuerzh
2023-11-19 14:23:21 +08:00
parent 88cc6ff4d5
commit 74639e9c4e
25 changed files with 788 additions and 123 deletions

View File

@ -46,30 +46,36 @@ func NewGRPCPlugin(name string, addr string, opts ...plugin.Option) ingress.Ingr
return p
}
func (p *grpcPlugin) Get(ctx context.Context, host string, opts ...ingress.GetOption) string {
func (p *grpcPlugin) GetRule(ctx context.Context, host string, opts ...ingress.Option) *ingress.Rule {
if p.client == nil {
return ""
return nil
}
r, err := p.client.Get(ctx,
&proto.GetRequest{
r, err := p.client.GetRule(ctx,
&proto.GetRuleRequest{
Host: host,
})
if err != nil {
p.log.Error(err)
return ""
return nil
}
if r.Endpoint == "" {
return nil
}
return &ingress.Rule{
Hostname: host,
Endpoint: r.Endpoint,
}
return r.GetEndpoint()
}
func (p *grpcPlugin) Set(ctx context.Context, host, endpoint string, opts ...ingress.SetOption) bool {
if p.client == nil {
func (p *grpcPlugin) SetRule(ctx context.Context, rule *ingress.Rule, opts ...ingress.Option) bool {
if p.client == nil || rule == nil {
return false
}
r, _ := p.client.Set(ctx, &proto.SetRequest{
Host: host,
Endpoint: endpoint,
r, _ := p.client.SetRule(ctx, &proto.SetRuleRequest{
Host: rule.Hostname,
Endpoint: rule.Endpoint,
})
if r == nil {
return false
@ -85,20 +91,20 @@ func (p *grpcPlugin) Close() error {
return nil
}
type httpPluginGetRequest struct {
type httpPluginGetRuleRequest struct {
Host string `json:"host"`
}
type httpPluginGetResponse struct {
type httpPluginGetRuleResponse struct {
Endpoint string `json:"endpoint"`
}
type httpPluginSetRequest struct {
type httpPluginSetRuleRequest struct {
Host string `json:"host"`
Endpoint string `json:"endpoint"`
}
type httpPluginSetResponse struct {
type httpPluginSetRuleResponse struct {
OK bool `json:"ok"`
}
@ -127,14 +133,14 @@ func NewHTTPPlugin(name string, url string, opts ...plugin.Option) ingress.Ingre
}
}
func (p *httpPlugin) Get(ctx context.Context, host string, opts ...ingress.GetOption) (endpoint string) {
func (p *httpPlugin) GetRule(ctx context.Context, host string, opts ...ingress.Option) *ingress.Rule {
if p.client == nil {
return
return nil
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.url, nil)
if err != nil {
return
return nil
}
if p.header != nil {
req.Header = p.header.Clone()
@ -147,29 +153,35 @@ func (p *httpPlugin) Get(ctx context.Context, host string, opts ...ingress.GetOp
resp, err := p.client.Do(req)
if err != nil {
return
return nil
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return
return nil
}
res := httpPluginGetResponse{}
res := httpPluginGetRuleResponse{}
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return
return nil
}
if res.Endpoint == "" {
return nil
}
return &ingress.Rule{
Hostname: host,
Endpoint: res.Endpoint,
}
return res.Endpoint
}
func (p *httpPlugin) Set(ctx context.Context, host, endpoint string, opts ...ingress.SetOption) bool {
if p.client == nil {
func (p *httpPlugin) SetRule(ctx context.Context, rule *ingress.Rule, opts ...ingress.Option) bool {
if p.client == nil || rule == nil {
return false
}
rb := httpPluginSetRequest{
Host: host,
Endpoint: endpoint,
rb := httpPluginSetRuleRequest{
Host: rule.Hostname,
Endpoint: rule.Endpoint,
}
v, err := json.Marshal(&rb)
if err != nil {
@ -195,7 +207,7 @@ func (p *httpPlugin) Set(ctx context.Context, host, endpoint string, opts ...ing
return false
}
res := httpPluginSetResponse{ }
res := httpPluginSetRuleResponse{}
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return false
}