update libs
This commit is contained in:
1 parent
1b7eef2443
commit
577c1fd1a6
11 files changed
+172
-223
No files matched your search
@@ -1,148 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
rn "runtime"
|
||||
"gitlab8.alx/msp2.0/msp-lib/backend"
|
||||
"gitlab8.alx/msp2.0/msp-lib/config"
|
||||
"gitlab8.alx/msp2.0/msp-lib/socket"
|
||||
"gitlab8.alx/msp2.0/msp-lib/logger"
|
||||
"gitlab8.alx/msp2.0/msp-lib/structure"
|
||||
"gitlab8.alx/msp2.0/msp-lib/utils"
|
||||
"admin-service/conf"
|
||||
"os"
|
||||
"path"
|
||||
"admin-service/helper"
|
||||
"time"
|
||||
"github.com/asaskevich/govalidator"
|
||||
"github.com/graarh/golang-socketio"
|
||||
"encoding/json"
|
||||
"os/signal"
|
||||
"gitlab8.alx/msp2.0/msp-lib/database"
|
||||
"context"
|
||||
"gitlab.alx/msp2.0/msp-lib/backend"
|
||||
"gitlab.alx/msp2.0/msp-lib/bootstrap"
|
||||
"gitlab.alx/msp2.0/msp-lib/database"
|
||||
"gitlab.alx/msp2.0/msp-lib/socket"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
configData *conf.Configuration
|
||||
executableFileDir string
|
||||
configChan = make(chan bool)
|
||||
version = "0.1.0"
|
||||
date = "undefined"
|
||||
version = "0.1.0"
|
||||
date = "undefined"
|
||||
)
|
||||
|
||||
func init() {
|
||||
config.InitConfig(&conf.Configuration{})
|
||||
validLocalConfig()
|
||||
appConfig := config.Get().(*conf.Configuration)
|
||||
createGrpcServer()
|
||||
socket.InitClient(
|
||||
socket.SocketConfiguration{
|
||||
Host: appConfig.ConfigServiceAddress.IP,
|
||||
Port: appConfig.ConfigServiceAddress.Port,
|
||||
Secure: false,
|
||||
UrlParams: map[string]string{"module_name": appConfig.ModuleName, "instance_uuid": appConfig.InstanceUuid},
|
||||
},
|
||||
func(client *gosocketio.Client) {
|
||||
subscribeSocket(client, utils.SendConfigWhenConnected)
|
||||
subscribeSocket(client, utils.SendConfigChanged)
|
||||
subscribeSocket(client, utils.SendConfigOnRequest)
|
||||
},
|
||||
)
|
||||
time.Sleep(time.Second * 3)
|
||||
for config.GetRemote() == nil {
|
||||
logger.Warnf("Remote config isn't received, module: %s, instance_uuid: %s",
|
||||
appConfig.ModuleName, appConfig.InstanceUuid)
|
||||
time.Sleep(time.Second * 5)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
ex, err := os.Executable()
|
||||
executableFileDir = path.Dir(ex)
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
panic(err)
|
||||
}
|
||||
if utils.DEV {
|
||||
_, filename, _, _ := rn.Caller(0)
|
||||
executableFileDir = path.Dir(filename)
|
||||
}
|
||||
listenConfigChange()
|
||||
awaitTerminate()
|
||||
bootstrap.
|
||||
ServiceBootstrap(&conf.Configuration{}, &conf.RemoteConfig{}).
|
||||
OnLocalConfigLoad(onLocalConfigLoad).
|
||||
SocketConfiguration(socketConfiguration).
|
||||
SendRemoteConfigSchemaWithVersion(version).
|
||||
SendRoutes(routesData).
|
||||
OnRemoteConfigReceive(onRemoteConfigReceive).
|
||||
OnShutdown(onShutdown).
|
||||
Run()
|
||||
}
|
||||
|
||||
// Start a GRPC server.
|
||||
func createGrpcServer() {
|
||||
appConfig := config.Get().(*conf.Configuration)
|
||||
addr := structure.AddressConfiguration{IP: appConfig.GrpcInnerAddress.IP, Port: appConfig.GrpcInnerAddress.Port}
|
||||
backend.StartBackendGrpcServer(addr, backend.GetDefaultService(appConfig.ModuleName, helper.GetHandlers()))
|
||||
}
|
||||
|
||||
func validRemoteConfig(remoteConfig *conf.RemoteConfig) {
|
||||
_, err := govalidator.ValidateStruct(remoteConfig)
|
||||
if err != nil {
|
||||
validationErrors := govalidator.ErrorsByField(err)
|
||||
logger.Fatal("Remote config int't valid", validationErrors)
|
||||
panic(err)
|
||||
func socketConfiguration(cfg interface{}) socket.SocketConfiguration {
|
||||
appConfig := cfg.(*conf.Configuration)
|
||||
return socket.SocketConfiguration{
|
||||
Host: appConfig.ConfigServiceAddress.IP,
|
||||
Port: appConfig.ConfigServiceAddress.Port,
|
||||
Secure: false,
|
||||
UrlParams: map[string]string{
|
||||
"module_name": appConfig.ModuleName,
|
||||
"instance_uuid": appConfig.InstanceUuid,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func validLocalConfig() {
|
||||
localConfig := config.Get().(*conf.Configuration)
|
||||
_, err := govalidator.ValidateStruct(localConfig)
|
||||
if err != nil {
|
||||
validationErrors := govalidator.ErrorsByField(err)
|
||||
logger.Fatal("Local config int't valid", validationErrors)
|
||||
panic(err)
|
||||
func onShutdown(_ context.Context, _ os.Signal) {
|
||||
backend.StopGrpcServer()
|
||||
}
|
||||
|
||||
func onRemoteConfigReceive(remoteConfig, _ *conf.RemoteConfig) {
|
||||
database.InitDb(remoteConfig.Database)
|
||||
}
|
||||
|
||||
func onLocalConfigLoad(cfg *conf.Configuration) {
|
||||
handlers := helper.GetHandlers()
|
||||
service := backend.GetDefaultService(cfg.ModuleName, handlers...)
|
||||
backend.StartBackendGrpcServer(cfg.GrpcInnerAddress, service)
|
||||
}
|
||||
|
||||
func routesData(localConfig interface{}) bootstrap.SendRoutesData {
|
||||
cfg := localConfig.(*conf.Configuration)
|
||||
return bootstrap.SendRoutesData{
|
||||
ModuleName: cfg.ModuleName,
|
||||
Version: version,
|
||||
GrpcOuterAddress: cfg.GrpcOuterAddress,
|
||||
Handlers: helper.GetHandlers(),
|
||||
}
|
||||
}
|
||||
|
||||
func subscribeSocket(client *gosocketio.Client, eventName string) {
|
||||
client.On(eventName, func(h *gosocketio.Channel, args string) error {
|
||||
logger.Infof("--- Got event: %s message: %s", eventName, args)
|
||||
remoteConfig := &conf.RemoteConfig{}
|
||||
config.InitRemoteConfig(remoteConfig, args)
|
||||
validRemoteConfig(remoteConfig)
|
||||
configChan <- true
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func listenConfigChange() {
|
||||
go func() {
|
||||
for {
|
||||
<-configChan
|
||||
|
||||
localConfig := config.Get().(*conf.Configuration)
|
||||
remoteConfig := config.GetRemote().(*conf.RemoteConfig)
|
||||
|
||||
database.InitDb(remoteConfig.Database)
|
||||
|
||||
addrOuter := structure.AddressConfiguration{
|
||||
IP: localConfig.GrpcOuterAddress.IP,
|
||||
Port: localConfig.GrpcOuterAddress.Port,
|
||||
}
|
||||
|
||||
methods := backend.CreateBackendConfig(
|
||||
structure.ModuleInfo{ModuleName: localConfig.ModuleName, Version: version},
|
||||
addrOuter,
|
||||
localConfig.ModuleName, 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() {
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, os.Interrupt)
|
||||
<-c
|
||||
logger.Info("Shutting down")
|
||||
os.Exit(0)
|
||||
}
|
||||
Reference in new issue
Block a user