small ref

This commit is contained in:
d1slike committed 2020-04-27 12:00:50 +03:00
1 parent 896dd36025
commit 8c732a15e2
2 files changed
+49 -46

No files matched your search

+6 -32
View File
@@ -2,6 +2,8 @@ package main
import (
"context"
"github.com/integration-system/isp-log/stdcodes"
"log"
"os"
"github.com/integration-system/isp-lib/v2/bootstrap"
@@ -9,8 +11,6 @@ import (
"github.com/integration-system/isp-lib/v2/config/schema"
"github.com/integration-system/isp-lib/v2/metric"
"github.com/integration-system/isp-lib/v2/structure"
log "github.com/integration-system/isp-log"
"github.com/integration-system/isp-log/stdcodes"
"isp-gate-service/accounting"
"isp-gate-service/authenticate"
"isp-gate-service/conf"
@@ -40,7 +40,10 @@ func main() {
RequireRoutes(handleRouteUpdate).
RequireModule("journal", invoker.Journal.ReceiveServiceAddressList, false)
requiredModules := getRequiredModulesByLocations(cfg.Locations)
requiredModules, err := proxy.InitProxies(cfg.Locations)
if err != nil {
log.Fatal(stdcodes.ModuleInvalidLocalConfig, err)
}
for module, consumer := range requiredModules {
bs.RequireModule(module, consumer, false)
}
@@ -108,32 +111,3 @@ func makeDeclaration(localConfig interface{}) bootstrap.ModuleInfo {
Endpoints: []structure.EndpointDescriptor{},
}
}
func getRequiredModulesByLocations(locations []conf.Location) map[string]func([]structure.AddressConfiguration) bool {
locationsByTargetModule := conf.GetLocationsByTargetModule(locations)
requiredModules := make(map[string]func([]structure.AddressConfiguration) bool)
for targetModule, locations := range locationsByTargetModule {
consumerStorage := make([]func([]structure.AddressConfiguration) bool, len(locations))
for i, location := range locations {
p, err := proxy.Init(location.Protocol, location.PathPrefix, location.SkipAuth, location.SkipExistCheck)
if err != nil {
log.Fatal(stdcodes.ModuleInvalidLocalConfig, err)
}
consumerStorage[i] = p.Consumer
}
requiredModules[targetModule] = aggregateConsumers(consumerStorage...)
}
proxy.PostInit()
return requiredModules
}
func aggregateConsumers(consumers ...func([]structure.AddressConfiguration) bool) func([]structure.AddressConfiguration) bool {
return func(list []structure.AddressConfiguration) bool {
for _, consumer := range consumers {
consumer(list)
}
return true
}
}
+43 -14
View File
@@ -1,6 +1,7 @@
package proxy
import (
"isp-gate-service/conf"
"sort"
"strings"
@@ -14,7 +15,9 @@ import (
"isp-gate-service/proxy/websocket"
)
var store = make([]storeItem, 0)
var (
store = make([]storeItem, 0)
)
const (
httpProtocol = "http"
@@ -37,10 +40,37 @@ type (
}
)
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)
func InitProxies(locations []conf.Location) (map[string]func([]structure.AddressConfiguration) bool, error) {
locationsByTargetModule := conf.GetLocationsByTargetModule(locations)
requiredModules := make(map[string]func([]structure.AddressConfiguration) bool)
for targetModule, locations := range locationsByTargetModule {
consumerStorage := make([]func([]structure.AddressConfiguration) bool, len(locations))
for i, location := range locations {
if location.PathPrefix[0] != '/' {
return nil, errors.Errorf("path must begin with '/' in path '%s'", location.PathPrefix)
}
p, err := makeProxy(location.Protocol, location.SkipAuth, location.SkipExistCheck)
if err != nil {
return nil, err
}
store = append(store, storeItem{
pathPrefix: location.PathPrefix,
proxy: p,
})
consumerStorage[i] = p.Consumer
}
requiredModules[targetModule] = aggregateConsumers(consumerStorage...)
}
sort.Slice(store, func(i, j int) bool {
return store[i].pathPrefix > store[j].pathPrefix
})
return requiredModules, nil
}
func makeProxy(protocol string, skipAuth, skipExistCheck bool) (Proxy, error) {
var proxy Proxy
switch protocol {
case httpProtocol:
@@ -55,19 +85,9 @@ func Init(protocol, pathPrefix string, skipAuth, skipExistCheck bool) (Proxy, er
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 _, item := range store {
if strings.HasPrefix(path, item.pathPrefix) {
@@ -90,3 +110,12 @@ func getPathWithoutPrefix(path, prefix string) string {
}
return s
}
func aggregateConsumers(consumers ...func([]structure.AddressConfiguration) bool) func([]structure.AddressConfiguration) bool {
return func(list []structure.AddressConfiguration) bool {
for _, consumer := range consumers {
consumer(list)
}
return true
}
}