Files
isp-gate-service/proxy/description.go
T
2019-11-08 16:36:43 +03:00

67 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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()
}
}