add server route
This commit is contained in:
1 parent
fe6f1f5e5a
commit
b29f3c6dcd
7 files changed
+125
-50
No files matched your search
+18
-1
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user