This commit is contained in:
DeniskaRediska committed 2019-10-22 13:27:32 +03:00
1 parent 70f5ebeb4b
commit 3a4931ca8c
7 files changed
+105 -62

No files matched your search

+3 -3
View File
@@ -1,12 +1,12 @@
configServiceAddress:
ip: 10.250.9.40
ip: 127.0.0.1
port: 9001
httpOuterAddress:
ip: http://
port: 9028
port: 9003
httpInnerAddress:
ip: 0.0.0.0
port: 9028
port: 9003
moduleName: isp-gate
instanceUuid: ec183598-746a-425f-b5e5-27b7fa8bc6af
locations:
+3 -3
View File
@@ -13,8 +13,8 @@ type (
}
Location struct {
PathPrefix string
Protocol string
TargetModule string
PathPrefix string `valid:"required~Required"`
Protocol string `valid:"required~Required"`
TargetModule string `valid:"required~Required"`
}
)
+29
View File
@@ -0,0 +1,29 @@
package handler
import (
"encoding/json"
"github.com/integration-system/isp-lib/http"
"github.com/integration-system/isp-lib/structure"
"github.com/valyala/fasthttp"
"google.golang.org/grpc/codes"
"isp-gate-service/proxy"
)
func Complete(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
p := proxy.Find(path)
if p != nil {
p.ProxyRequest(ctx)
} else {
statusCode := codes.NotFound
response := structure.GrpcError{
ErrorMessage: "unknown path", ErrorCode: statusCode.String(),
Details: []interface{}{map[string]string{"path": path}},
}
ctx.Response.Header.SetContentType("application/json; charset=utf-8")
ctx.SetStatusCode(http.CodeToHttpStatus(statusCode))
message, _ := json.Marshal(response)
_, _ = ctx.Write(message)
}
}
+10 -8
View File
@@ -1,17 +1,19 @@
package log_code
const (
InfoOnLocalConfigLoad = 601
WarnCreateRestServerHttpSrvShutdown = 602
ErrorCreateRestServerHttpSrvListenAndServe = 603
WarnRequestHandler = 604 //metadata: {"typeData":"", "method":""}
WarnConvertErrorDataMarshalResponse = 605
ErrorGrpcClientDialing = 606
WarnJournalCouldNotWriteToFile = 607
WarnJournalClientDialing = 608
InfoOnLocalConfigLoad = 601
WarnRequestHandler = 604 //metadata: {"typeData":"", "method":""}
WarnConvertErrorDataMarshalResponse = 605
ErrorGrpcClientDialing = 606
WarnJournalCouldNotWriteToFile = 607
WarnJournalClientDialing = 608
ErrorClientJournal = 903
WarnCreateRestServerHttpSrvShutdown = 602
ErrorCreateRestServerHttpSrvListenAndServe = 603
ErrorLocalConfig = 904
ErrorClientGrpc = 905
ErrorClientHttp = 906
+4 -37
View File
@@ -2,21 +2,17 @@ package main
import (
"context"
"github.com/valyala/fasthttp"
"sync"
"time"
"github.com/integration-system/isp-lib/bootstrap"
"github.com/integration-system/isp-lib/config"
"github.com/integration-system/isp-lib/config/schema"
"github.com/integration-system/isp-lib/metric"
"github.com/integration-system/isp-lib/structure"
log "github.com/integration-system/isp-log"
"isp-gate-service/conf"
"isp-gate-service/journal"
"isp-gate-service/log_code"
"isp-gate-service/proxy"
"isp-gate-service/server"
"isp-gate-service/service"
"os"
)
@@ -24,9 +20,6 @@ import (
var (
version = "0.1.0"
date = "undefined"
srvLock = sync.Mutex{}
httpSrv *fasthttp.Server
)
func main() {
@@ -44,7 +37,7 @@ func main() {
if p, err := proxy.Init(location); err != nil {
log.Fatal(log_code.ErrorLocalConfig, err)
} else {
bs.RequireModule(location.TargetModule, p.Consumer, true)
bs.RequireModule(location.TargetModule, p.Consumer, false)
}
}
@@ -64,7 +57,7 @@ func onRemoteConfigReceive(remoteConfig, oldRemoteConfig *conf.RemoteConfig) {
service.JournalMethodsMatcher = service.NewCacheableMethodMatcher(remoteConfig.JournalingMethodsPatterns)
createServer(remoteConfig)
server.Http.Init(remoteConfig.MaxRequestBodySizeBytes)
metric.InitCollectors(remoteConfig.Metrics, oldRemoteConfig.Metrics)
metric.InitHttpServer(remoteConfig.Metrics)
//metric.InitStatusChecker("router-grpc", helper.GetRoutersAndStatus)
@@ -85,9 +78,7 @@ func socketConfiguration(cfg interface{}) structure.SocketConfiguration {
}
func onShutdown(_ context.Context, _ os.Signal) {
for _, p := range proxy.ProxyStore {
p.Close()
}
proxy.Close()
}
func makeDeclaration(localConfig interface{}) bootstrap.ModuleInfo {
@@ -99,27 +90,3 @@ func makeDeclaration(localConfig interface{}) bootstrap.ModuleInfo {
Handlers: []interface{}{},
}
}
func createServer(remoteConfig *conf.RemoteConfig) {
srvLock.Lock()
if httpSrv != nil {
if err := httpSrv.Shutdown(); err != nil {
log.Warn(log_code.WarnCreateRestServerHttpSrvShutdown, err)
}
}
maxRequestBodySize := remoteConfig.GetMaxRequestBodySize()
localConfig := config.Get().(*conf.Configuration)
restAddress := localConfig.HttpInnerAddress.GetAddress()
httpSrv = &fasthttp.Server{
Handler: proxy.Handle,
WriteTimeout: time.Second * 60,
ReadTimeout: time.Second * 60,
MaxRequestBodySize: int(maxRequestBodySize),
}
go func() {
if err := httpSrv.ListenAndServe(restAddress); err != nil {
log.Error(log_code.ErrorCreateRestServerHttpSrvListenAndServe, err)
}
}()
srvLock.Unlock()
}
+13 -11
View File
@@ -4,15 +4,13 @@ import (
"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)
var store = make(map[string]Proxy)
const (
httpProtocol = "http"
@@ -32,24 +30,28 @@ func Init(location conf.Location) (Proxy, error) {
switch location.Protocol {
case httpProtocol:
proxy := http.NewProxy()
ProxyStore[location.PathPrefix] = proxy
store[location.PathPrefix] = proxy
return proxy, nil
case grpcProtocol:
proxy := grpc.NewProxy()
ProxyStore[location.PathPrefix] = proxy
store[location.PathPrefix] = proxy
return proxy, nil
default:
return nil, errors.New("unknown protocol")
}
}
func Handle(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
for pathPrefix, proxy := range ProxyStore {
func Find(path string) Proxy {
for pathPrefix, proxy := range store {
if strings.HasPrefix(path, pathPrefix) {
proxy.ProxyRequest(ctx)
return
return proxy
}
}
utils.SendError("unknown path", codes.Internal, []interface{}{map[string]string{"path": path}}, ctx)
return nil
}
func Close() {
for _, p := range store {
p.Close()
}
}
+43
View File
@@ -0,0 +1,43 @@
package server
import (
"github.com/integration-system/isp-lib/config"
log "github.com/integration-system/isp-log"
"github.com/valyala/fasthttp"
"isp-gate-service/conf"
"isp-gate-service/handler"
"isp-gate-service/log_code"
"sync"
"time"
)
var Http = &httpSrv{mx: sync.Mutex{}}
type httpSrv struct {
srv *fasthttp.Server
mx sync.Mutex
}
func (s *httpSrv) Init(MaxRequestBodySize int64) {
s.mx.Lock()
if s.srv != nil {
if err := s.srv.Shutdown(); err != nil {
log.Warn(log_code.WarnCreateRestServerHttpSrvShutdown, err)
}
}
maxRequestBodySize := MaxRequestBodySize
localConfig := config.Get().(*conf.Configuration)
restAddress := localConfig.HttpInnerAddress.GetAddress()
s.srv = &fasthttp.Server{
Handler: handler.Complete,
WriteTimeout: time.Second * 60,
ReadTimeout: time.Second * 60,
MaxRequestBodySize: int(maxRequestBodySize),
}
go func() {
if err := s.srv.ListenAndServe(restAddress); err != nil {
log.Error(log_code.ErrorCreateRestServerHttpSrvListenAndServe, err)
}
}()
s.mx.Unlock()
}