fix search in locations, fix proxying URI in http
This commit is contained in:
1 parent
f0b8407479
commit
896dd36025
7 files changed
+126
-82
No files matched your search
+35
-30
@@ -1,6 +1,9 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/integration-system/isp-lib/v2/structure"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/valyala/fasthttp"
|
||||
@@ -9,10 +12,9 @@ import (
|
||||
"isp-gate-service/proxy/health_check"
|
||||
"isp-gate-service/proxy/http"
|
||||
"isp-gate-service/proxy/websocket"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var store = make(map[string]Proxy)
|
||||
var store = make([]storeItem, 0)
|
||||
|
||||
const (
|
||||
httpProtocol = "http"
|
||||
@@ -29,59 +31,62 @@ type (
|
||||
SkipExistCheck() bool
|
||||
Close()
|
||||
}
|
||||
storeItem struct {
|
||||
pathPrefix string
|
||||
proxy Proxy
|
||||
}
|
||||
)
|
||||
|
||||
func Init(protocol, pathPrefix string, skipAuth, skipExistCheck bool) (Proxy, error) {
|
||||
if pathPrefix[0] != '/' {
|
||||
return nil, errors.Errorf("path must begin with '/' in path '%s'", pathPrefix)
|
||||
}
|
||||
var proxy Proxy
|
||||
switch protocol {
|
||||
case httpProtocol:
|
||||
proxy := http.NewProxy(skipAuth, skipExistCheck)
|
||||
store[pathPrefix] = proxy
|
||||
return proxy, nil
|
||||
proxy = http.NewProxy(skipAuth, skipExistCheck)
|
||||
case grpcProtocol:
|
||||
proxy := grpc.NewProxy(skipAuth, skipExistCheck)
|
||||
store[pathPrefix] = proxy
|
||||
return proxy, nil
|
||||
proxy = grpc.NewProxy(skipAuth, skipExistCheck)
|
||||
case healthCheckProtocol:
|
||||
proxy := health_check.NewProxy(skipAuth, skipExistCheck)
|
||||
store[pathPrefix] = proxy
|
||||
return proxy, nil
|
||||
proxy = health_check.NewProxy(skipAuth, skipExistCheck)
|
||||
case websocketProtocol:
|
||||
proxy := websocket.NewProxy(skipAuth, skipExistCheck)
|
||||
store[pathPrefix] = proxy
|
||||
return proxy, nil
|
||||
proxy = websocket.NewProxy(skipAuth, skipExistCheck)
|
||||
default:
|
||||
return nil, errors.Errorf("unknown protocol '%s'", protocol)
|
||||
}
|
||||
|
||||
store = append(store, storeItem{
|
||||
pathPrefix: pathPrefix,
|
||||
proxy: proxy,
|
||||
})
|
||||
return proxy, nil
|
||||
}
|
||||
|
||||
func PostInit() {
|
||||
sort.Slice(store, func(i, j int) bool {
|
||||
return store[i].pathPrefix > store[j].pathPrefix
|
||||
})
|
||||
}
|
||||
|
||||
func Find(path string) (Proxy, string) {
|
||||
for pathPrefix, proxy := range store {
|
||||
if strings.HasPrefix(path, pathPrefix) {
|
||||
return proxy, getPathWithoutPrefix(path)
|
||||
for _, item := range store {
|
||||
if strings.HasPrefix(path, item.pathPrefix) {
|
||||
return item.proxy, getPathWithoutPrefix(path, item.pathPrefix)
|
||||
}
|
||||
}
|
||||
return nil, getPathWithoutPrefix(path)
|
||||
return nil, ""
|
||||
}
|
||||
|
||||
func Close() {
|
||||
for _, p := range store {
|
||||
p.Close()
|
||||
p.proxy.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func getPathWithoutPrefix(path string) string {
|
||||
firstFound := false
|
||||
for i, value := range path {
|
||||
if value == '/' {
|
||||
if firstFound {
|
||||
return path[i+1:]
|
||||
} else {
|
||||
firstFound = true
|
||||
}
|
||||
}
|
||||
func getPathWithoutPrefix(path, prefix string) string {
|
||||
s := strings.TrimPrefix(path, prefix)
|
||||
if len(s) > 0 && s[0] == '/' {
|
||||
return s[1:]
|
||||
}
|
||||
return ""
|
||||
return s
|
||||
}
|
||||
Reference in new issue
Block a user