From 3a4931ca8cf37406c43f963141c76e9e4d1f872b Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Tue, 22 Oct 2019 13:27:32 +0300 Subject: [PATCH] init --- conf/config.yml | 6 +++--- conf/local_config.go | 6 +++--- handler/handler.go | 29 +++++++++++++++++++++++++++++ log_code/code.go | 18 ++++++++++-------- main.go | 41 ++++------------------------------------- proxy/description.go | 24 +++++++++++++----------- server/http.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 105 insertions(+), 62 deletions(-) create mode 100644 handler/handler.go create mode 100644 server/http.go diff --git a/conf/config.yml b/conf/config.yml index e65171f..2fb628f 100644 --- a/conf/config.yml +++ b/conf/config.yml @@ -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: diff --git a/conf/local_config.go b/conf/local_config.go index 5df2771..aec087f 100644 --- a/conf/local_config.go +++ b/conf/local_config.go @@ -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"` } ) diff --git a/handler/handler.go b/handler/handler.go new file mode 100644 index 0000000..218e9dc --- /dev/null +++ b/handler/handler.go @@ -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) + } +} diff --git a/log_code/code.go b/log_code/code.go index dca8361..9497c53 100644 --- a/log_code/code.go +++ b/log_code/code.go @@ -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 diff --git a/main.go b/main.go index c8ce0f6..0f6146a 100644 --- a/main.go +++ b/main.go @@ -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() -} diff --git a/proxy/description.go b/proxy/description.go index f9cab36..b7fd7e2 100644 --- a/proxy/description.go +++ b/proxy/description.go @@ -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() + } } diff --git a/server/http.go b/server/http.go new file mode 100644 index 0000000..bf0f426 --- /dev/null +++ b/server/http.go @@ -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() +}