67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package proxy
|
||
|
||
import (
|
||
"github.com/integration-system/isp-lib/structure"
|
||
"github.com/pkg/errors"
|
||
"github.com/valyala/fasthttp"
|
||
"isp-gate-service/conf"
|
||
"isp-gate-service/domain"
|
||
"isp-gate-service/proxy/grpc"
|
||
"isp-gate-service/proxy/health_check"
|
||
"isp-gate-service/proxy/http"
|
||
"strings"
|
||
)
|
||
|
||
var store = make(map[string]Proxy)
|
||
|
||
const (
|
||
httpProtocol = "http"
|
||
grpcProtocol = "grpc"
|
||
healthCheckProtocol = "healthсheck"
|
||
)
|
||
|
||
type (
|
||
Proxy interface {
|
||
ProxyRequest(ctx *fasthttp.RequestCtx) domain.ProxyResponse
|
||
Consumer([]structure.AddressConfiguration) bool
|
||
Close()
|
||
}
|
||
)
|
||
|
||
func Init(location conf.Location) (Proxy, error) {
|
||
if location.PathPrefix[0] != '/' {
|
||
return nil, errors.Errorf("path must begin with '/' in path '%s'", location.PathPrefix)
|
||
}
|
||
switch location.Protocol {
|
||
case httpProtocol:
|
||
proxy := http.NewProxy()
|
||
store[location.PathPrefix] = proxy
|
||
return proxy, nil
|
||
case grpcProtocol:
|
||
proxy := grpc.NewProxy()
|
||
store[location.PathPrefix] = proxy
|
||
return proxy, nil
|
||
case healthCheckProtocol:
|
||
proxy := health_check.NewProxy()
|
||
store[location.PathPrefix] = proxy
|
||
return proxy, nil
|
||
default:
|
||
return nil, errors.Errorf("unknown protocol '%s'", location.Protocol)
|
||
}
|
||
}
|
||
|
||
func Find(path string) Proxy {
|
||
for pathPrefix, proxy := range store {
|
||
if strings.HasPrefix(path, pathPrefix) {
|
||
return proxy
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func Close() {
|
||
for _, p := range store {
|
||
p.Close()
|
||
}
|
||
}
|