Skip to content

Commit

Permalink
chore(core): match = match host + match path
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant committed Sep 26, 2023
1 parent a88c0f0 commit cb93551
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 37 deletions.
6 changes: 6 additions & 0 deletions core/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package core

import "errors"

var ErrHostNotFound = errors.New("not found by host")
var ErrPathNotFound = errors.New("not found by path")
96 changes: 59 additions & 37 deletions core/match.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"errors"
"fmt"
"regexp"

Expand All @@ -25,49 +26,70 @@ func (c *core) match(host string, path string) (s *service.Service, err error) {
}

func Match(rules []rule.Rule, host, path string) (s *service.Service, err error) {
hostService, rule, err := matchHost(rules, host)
if err != nil {
if !errors.Is(err, ErrHostNotFound) {
return nil, err
}

return nil, nil
}

pathService, err := matchPath(rule.Paths, path)
if err != nil {
if !errors.Is(err, ErrPathNotFound) {
return nil, err
}

return hostService, nil
}

return pathService, nil
}

func matchHost(rules []rule.Rule, host string) (b *service.Service, r *rule.Rule, err error) {
for _, rule := range rules {
hostRegExp := fmt.Sprintf("^%s$", rule.Host)
if isMatched, _ := regexp.MatchString(hostRegExp, host); isMatched {
// paths
if rule.Paths != nil {
for _, rpath := range rule.Paths {
rpathRe := fmt.Sprintf("^%s", rpath.Path)
//
isMatched, err := regexp.MatchString(rpathRe, path)
if err != nil {
return nil, fmt.Errorf("failed to match path: %s", err)
}

if isMatched {
s = &service.Service{
Protocol: rpath.Backend.Service.Protocol,
Name: rpath.Backend.Service.Name,
Port: rpath.Backend.Service.Port,
Request: rpath.Backend.Service.Request,
Response: rpath.Backend.Service.Response,
}
break
}
}
hostRewriter := rewriter.Rewriter{
From: hostRegExp,
To: rule.Backend.Service.Name,
}

// main
if s == nil {
hostRewriter := rewriter.Rewriter{
From: hostRegExp,
To: rule.Backend.Service.Name,
}
serviceNameNew := hostRewriter.Rewrite(host)
s = &service.Service{
Protocol: rule.Backend.Service.Protocol,
Name: serviceNameNew,
Port: rule.Backend.Service.Port,
Request: rule.Backend.Service.Request,
Response: rule.Backend.Service.Response,
}
s := &service.Service{
Protocol: rule.Backend.Service.Protocol,
Name: hostRewriter.Rewrite(host),
Port: rule.Backend.Service.Port,
Request: rule.Backend.Service.Request,
Response: rule.Backend.Service.Response,
}
return s, &rule, nil
}
}

return s, nil
// return nil, nil, fmt.Errorf("no rule found for host %s", host)
return nil, nil, ErrHostNotFound
}

func matchPath(paths []rule.Path, path string) (r *service.Service, err error) {
for _, rpath := range paths {
rpathRe := fmt.Sprintf("^%s", rpath.Path)
//
isMatched, err := regexp.MatchString(rpathRe, path)
if err != nil {
return nil, fmt.Errorf("failed to match path: %s", err)
}

if isMatched {
return &service.Service{
Protocol: rpath.Backend.Service.Protocol,
Name: rpath.Backend.Service.Name,
Port: rpath.Backend.Service.Port,
Request: rpath.Backend.Service.Request,
Response: rpath.Backend.Service.Response,
}, nil
}
}

// return nil, fmt.Errorf("no rule found for path %s", path)
return nil, ErrPathNotFound
}

0 comments on commit cb93551

Please sign in to comment.