add server route

This commit is contained in:
DeniskaRediska committed 2019-10-22 12:40:21 +03:00
1 parent fe6f1f5e5a
commit b29f3c6dcd
7 files changed
+125 -50

No files matched your search

+18 -1
View File
@@ -1,12 +1,15 @@
package proxy
import (
"errors"
"github.com/integration-system/isp-lib/structure"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
"google.golang.org/grpc/codes"
"isp-gate-service/conf"
"isp-gate-service/proxy/grpc"
"isp-gate-service/proxy/grpc/utils"
"isp-gate-service/proxy/http"
"strings"
)
var ProxyStore = make(map[string]Proxy)
@@ -23,6 +26,9 @@ type Proxy interface {
}
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()
@@ -36,3 +42,14 @@ func Init(location conf.Location) (Proxy, error) {
return nil, errors.New("unknown protocol")
}
}
func Handle(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
for pathPrefix, proxy := range ProxyStore {
if strings.HasPrefix(path, pathPrefix) {
proxy.ProxyRequest(ctx)
return
}
}
utils.SendError("unknown path", codes.Internal, []interface{}{map[string]string{"path": path}}, ctx)
}
+5 -5
View File
@@ -17,8 +17,8 @@ type grpcProxy struct {
client *backend.RxGrpcClient
}
func NewProxy() grpcProxy {
return grpcProxy{client: backend.NewRxGrpcClient(
func NewProxy() *grpcProxy {
return &grpcProxy{client: backend.NewRxGrpcClient(
backend.WithDialOptions(
grpc.WithInsecure(), grpc.WithBlock(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(conf.DefaultMaxResponseBodySize))),
@@ -30,7 +30,7 @@ func NewProxy() grpcProxy {
)}
}
func (p grpcProxy) ProxyRequest(ctx *fasthttp.RequestCtx) {
func (p *grpcProxy) ProxyRequest(ctx *fasthttp.RequestCtx) {
currentTime := time.Now()
uri := string(ctx.RequestURI())
@@ -45,10 +45,10 @@ func (p grpcProxy) ProxyRequest(ctx *fasthttp.RequestCtx) {
}
}
func (p grpcProxy) Consumer(addr []structure.AddressConfiguration) bool {
func (p *grpcProxy) Consumer(addr []structure.AddressConfiguration) bool {
return p.client.ReceiveAddressList(addr)
}
func (p grpcProxy) Close() {
func (p *grpcProxy) Close() {
p.client.Close()
}