to channel
This commit is contained in:
1 parent
0bdedc65ad
commit
1109b9f12e
1 file changed
+40
-36
@@ -24,9 +24,8 @@ import (
|
|||||||
var (
|
var (
|
||||||
configData *conf.Configuration
|
configData *conf.Configuration
|
||||||
executableFileDir string
|
executableFileDir string
|
||||||
methodBytes []byte
|
configChan = make(chan bool)
|
||||||
)
|
)
|
||||||
var socketConnected = false
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
config.InitConfig(&conf.Configuration{})
|
config.InitConfig(&conf.Configuration{})
|
||||||
@@ -42,12 +41,6 @@ func init() {
|
|||||||
subscribeSocket(client, utils.SendConfigWhenConnected)
|
subscribeSocket(client, utils.SendConfigWhenConnected)
|
||||||
subscribeSocket(client, utils.SendConfigChanged)
|
subscribeSocket(client, utils.SendConfigChanged)
|
||||||
subscribeSocket(client, utils.SendConfigOnRequest)
|
subscribeSocket(client, utils.SendConfigOnRequest)
|
||||||
socket.GetClient().On("connection", func(so *gosocketio.Channel) {
|
|
||||||
socketConnected = true
|
|
||||||
if methodBytes != nil {
|
|
||||||
so.Emit(utils.SendRoutesWhenConnected, string(methodBytes))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
time.Sleep(time.Second * 3)
|
time.Sleep(time.Second * 3)
|
||||||
@@ -68,33 +61,15 @@ func main() {
|
|||||||
_, filename, _, _ := rn.Caller(0)
|
_, filename, _, _ := rn.Caller(0)
|
||||||
executableFileDir = path.Dir(filename)
|
executableFileDir = path.Dir(filename)
|
||||||
}
|
}
|
||||||
//createGrpcServer()
|
listenConfigChange()
|
||||||
awaitTerminate()
|
awaitTerminate()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start a GRPC server.
|
// Start a GRPC server.
|
||||||
func createGrpcServer() {
|
func createGrpcServer() {
|
||||||
// Run our server in a goroutine so that it doesn't block.
|
|
||||||
handlers := helper.GetHandlers()
|
|
||||||
remoteConfig := config.GetRemote().(*conf.RemoteConfig)
|
remoteConfig := config.GetRemote().(*conf.RemoteConfig)
|
||||||
appConfig := config.Get().(*conf.Configuration)
|
|
||||||
addr := structure.AddressConfiguration{IP: remoteConfig.GrpcAddress.IP, Port: remoteConfig.GrpcAddress.Port}
|
addr := structure.AddressConfiguration{IP: remoteConfig.GrpcAddress.IP, Port: remoteConfig.GrpcAddress.Port}
|
||||||
addrOuter := structure.AddressConfiguration{
|
backend.StartBackendGrpcServer(addr, backend.GetDefaultService(remoteConfig.GrpcPrefix, helper.GetHandlers()))
|
||||||
IP: appConfig.GrpcOuterAddress.IP,
|
|
||||||
Port: appConfig.GrpcOuterAddress.Port,
|
|
||||||
}
|
|
||||||
backend.StartBackendGrpcServer(addr, backend.GetDefaultService(remoteConfig.GrpcPrefix, handlers))
|
|
||||||
methods := backend.GetBackendConfig(addrOuter, remoteConfig.GrpcPrefix, handlers)
|
|
||||||
bytes, err := json.Marshal(methods)
|
|
||||||
if err != nil {
|
|
||||||
logger.Warn("Error when serializing Backend Routes", err)
|
|
||||||
} else {
|
|
||||||
methodBytes = bytes
|
|
||||||
logger.Infof("EXPORTED MODULE METHODS: %s", methods)
|
|
||||||
if socketConnected {
|
|
||||||
socket.GetClient().Emit(utils.SendRoutesWhenConnected, string(bytes))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func validRemoteConfig(remoteConfig *conf.RemoteConfig) {
|
func validRemoteConfig(remoteConfig *conf.RemoteConfig) {
|
||||||
@@ -112,19 +87,13 @@ func subscribeSocket(client *gosocketio.Client, eventName string) {
|
|||||||
remoteConfig := &conf.RemoteConfig{}
|
remoteConfig := &conf.RemoteConfig{}
|
||||||
config.InitRemoteConfig(remoteConfig, args)
|
config.InitRemoteConfig(remoteConfig, args)
|
||||||
validRemoteConfig(remoteConfig)
|
validRemoteConfig(remoteConfig)
|
||||||
database.InitDb(remoteConfig.Database)
|
configChan <- true
|
||||||
backend.StopGrpcServer()
|
|
||||||
for !checkPortIsFree(remoteConfig.GrpcAddress.Port) {
|
|
||||||
time.Sleep(time.Second * 3)
|
|
||||||
logger.Info("Wait for free port for new grpc connection")
|
|
||||||
}
|
|
||||||
createGrpcServer()
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkPortIsFree(port string) bool {
|
func checkPortIsFree(port string) bool {
|
||||||
ln, err := net.Listen("tcp", ":" + port)
|
ln, err := net.Listen("tcp", ":"+port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
@@ -133,6 +102,41 @@ func checkPortIsFree(port string) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func listenConfigChange() {
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
<-configChan
|
||||||
|
|
||||||
|
localConfig := config.Get().(*conf.Configuration)
|
||||||
|
remoteConfig := config.GetRemote().(*conf.RemoteConfig)
|
||||||
|
|
||||||
|
backend.StopGrpcServer()
|
||||||
|
for !checkPortIsFree(remoteConfig.GrpcAddress.Port) {
|
||||||
|
time.Sleep(time.Second * 3)
|
||||||
|
logger.Info("Wait for free port for new grpc connection")
|
||||||
|
}
|
||||||
|
createGrpcServer()
|
||||||
|
database.InitDb(remoteConfig.Database)
|
||||||
|
|
||||||
|
addrOuter := structure.AddressConfiguration{
|
||||||
|
IP: localConfig.GrpcOuterAddress.IP,
|
||||||
|
Port: localConfig.GrpcOuterAddress.Port,
|
||||||
|
}
|
||||||
|
|
||||||
|
methods := backend.GetBackendConfig(addrOuter, remoteConfig.GrpcPrefix, helper.GetHandlers())
|
||||||
|
bytes, err := json.Marshal(methods)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warn("Error when serializing Backend Routes", err)
|
||||||
|
} else {
|
||||||
|
logger.Infof("EXPORTED MODULE METHODS: %s", methods)
|
||||||
|
socketClient := socket.GetClient()
|
||||||
|
socketClient.Emit(utils.SendRoutesWhenConnected, string(bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
func awaitTerminate() {
|
func awaitTerminate() {
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, os.Interrupt)
|
signal.Notify(c, os.Interrupt)
|
||||||
|
|||||||
Reference in new issue
Block a user