Skip to content

Commit

Permalink
Initial COmmit
Browse files Browse the repository at this point in the history
  • Loading branch information
DrOctavius committed Apr 3, 2022
0 parents commit e4486f8
Show file tree
Hide file tree
Showing 453 changed files with 34,539 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Todo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1. Indicate the Config Path from where the App should read!
2. We should make it possible to change the config structure!?
3. We should be able to add additional configuration options to the MainConfig
4.
Empty file added build/.gitignore
Empty file.
39 changes: 39 additions & 0 deletions core/bootstrap/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package bootstrap

import (
"time"
)

type Bootstrap struct {
// This is the time when has being bootstrapped, usually app start time
startTime time.Time
}

func Start() *Bootstrap {
return &Bootstrap{
startTime: time.Now(),
// These are the broker clients which are auto connecting to the broker servers
//brokerClients: make(map[string]*client.Client),
}
}

// StartForProcess -> This is the initial function which will create the bootstrap instance and register some info!
func StartForProcess() *Bootstrap {
processBootstrap = Start()
return processBootstrap
}

// GetProcessBootstrap -> this returns the bootstrap instance!
func GetProcessBootstrap() *Bootstrap {
return processBootstrap
}

// GetStartTime -> return the time when started!
func (b *Bootstrap) GetStartTime() time.Time {
return b.startTime
}

// GetRunningTime -> get the running time of the current app
func (b *Bootstrap) GetRunningTime() time.Duration {
return time.Since(b.startTime)
}
3 changes: 3 additions & 0 deletions core/bootstrap/globals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package bootstrap

var processBootstrap *Bootstrap
54 changes: 54 additions & 0 deletions core/bootstrap/register_service/register_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package register_service

import (
"github.com/KyaXTeam/go-core/v2/core/logger/appLog"
"github.com/rs/zerolog"
"sync"
)

// Services -> Here we will be storing the registered services
var Services = make(map[string]RegisteredService)
var servicesLock sync.Mutex

type RegisteredService interface {
Run()
Stop()
// TODO: maybe other methods should be added for services
}

func RegisterService(
name string,
service RegisteredService,
) {
servicesLock.Lock()
defer servicesLock.Unlock()
Services[name] = service
}

// RunRegisteredServices -> this will run the registered services like bootstrap client or others...
func RunRegisteredServices() {

// I should run here a Method which will be in the interface...
// This method should be a standard...
// The objects should be stored somewhere!..?!

// Start the broker clients
/*b.brokerClients = broker.GenerateAllClients(_context.GetRootContext())
// TODO: do good logging
for instanceName, brokerClient := range b.brokerClients {
appLog.Info().Msg("starting broker client " + instanceName)
brokerClient.Connect()
}*/
info := func() *zerolog.Event {
return appLog.InfoF("RunRegisteredServices")
}
/*info().Msg("entering...")
defer info().Msg("leaving...")*/

for serviceName, _service := range Services {
info().Str("service_name", serviceName).Msg("running service...")
// Calling the Run Method of that service
_service.Run()
}
}
Empty file.
10 changes: 10 additions & 0 deletions core/clients/broker/client/connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package client

func (c *Client) Connect() {
// We can create the layer for the broker only if we will add additional functionality...
// In that case, we should rename the function to Start
// In that case multiple goroutines will be running....
// But in this case only websocket will be running... so it's not necessary to create additional functionality here

c.WSClient.Connect()
}
128 changes: 128 additions & 0 deletions core/clients/broker/client/constructor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package client

import (
"context"
"github.com/KyaXTeam/go-core/v2/core/clients/broker/config"
"github.com/KyaXTeam/go-core/v2/core/clients/websocket"
websocketConfig "github.com/KyaXTeam/go-core/v2/core/clients/websocket/config"
wsConnection "github.com/KyaXTeam/go-core/v2/core/clients/websocket/connection"
"github.com/KyaXTeam/go-core/v2/core/helpers/_context"
"github.com/KyaXTeam/go-core/v2/core/helpers/conv"
"github.com/KyaXTeam/go-core/v2/core/helpers/err/define"
"github.com/KyaXTeam/go-core/v2/core/helpers/sync/_bool"
"github.com/KyaXTeam/go-core/v2/core/logger"
loggerConfig "github.com/KyaXTeam/go-core/v2/core/logger/config"
loggerPaths "github.com/KyaXTeam/go-core/v2/core/logger/paths"
)

func New(
ctx context.Context,
config config.Config,
) (*Client, error) {

if !conv.ParseBool(config.IsEnabled) {
return nil, define.Err(0, "broker client is disabled", config.PipeName)
}

if ctx == nil {
ctx = _context.GetDefaultContext()
}

// Generate the logger config
// If Name has not being set for the logger in the config,
// It will automatically take the name from the instance!
if config.Logger.Name == "" {
config.Logger.Name = config.PipeName
}
// If DirLogPath is not defined, it will set the default folder!
if config.Logger.DirLogPath == "" {
config.Logger.DirLogPath = loggerPaths.GetLogsPathForClients("broker/" + config.Logger.Name)
}

// Set Module Name
if config.Logger.ModuleName == "" {
config.Logger.ModuleName = "Broker Client=" + config.PipeName
}

// Set the default values for the config... that's in case something is missed
loggerDefaultConfig, _err := loggerConfig.DefaultConfig(&config.Logger)

if _err != nil {
return nil, _err
}

brokerConnections := config.Connections
var wsConnections []*wsConnection.Connection
for _, brokerConn := range brokerConnections {
wsConn, _err := wsConnection.DefaultConfig(nil)
if _err != nil {
// TODO:
}
wsConn.IsSecure = brokerConn.IsSecure
wsConn.Host = brokerConn.Host
wsConn.Port = brokerConn.Port
wsConn.UriPath = "/api_ws/broker/pipes/" + config.PipeName
wsConn.AcceptCertificate = brokerConn.AcceptCertificate
wsConn.MaxRetries = brokerConn.MaxRetries
wsConn.RetryTimeout = brokerConn.RetryTimeout
// Enable always authentication
wsConn.EnableAuth = "yes"
wsConn.AuthOptions = wsConnection.AuthOptions{
// Set authentication method to 1
AuthType: wsConnection.AuthByToken,
Token: config.AuthToken,
}
// Add the connection
wsConnections = append(wsConnections, &wsConn)
}

// Generating the final config
wsConfig := websocketConfig.Config{
Name: config.PipeName,
// ---------RELATED TO CONNECTION----------\\
AutoReconnect: config.AutoReconnect,
Reconnect: websocketConfig.ReconnectOptions{
TimeoutSeconds: config.Reconnect.TimeoutSeconds,
MaxRetries: config.Reconnect.MaxRetries,
},
UseMultipleConnections: config.UseMultipleConnections,
// Set the connections
Connections: wsConnections,
// ---------RELATED TO CONNECTION----------\\
}

// Create the logger instance for broker
brokerLoggerInstance := logger.New(loggerDefaultConfig)
// Set same logger config for websocket, but make some minor changes
wsConfig.Logger = loggerDefaultConfig
// Setting the writer to websocket instance config, so it will write the logs to the brokers logs
wsConfig.Logger.ParentWriter = brokerLoggerInstance.MainWriter
// Set writing to parent as mandatory!
wsConfig.Logger.WriteToParent = "yes"
// Disable file writing, we don't need websocket to save separately the logs
wsConfig.Logger.FileIsEnabled = "no"

// We create the websocket client here... if additional functionality will be added to broker, we can move it to
// Start/Connect function
wsClient, _err := websocket.New(ctx, wsConfig)
if _err != nil {

return nil, define.Err(0, "failed to create websocket client for broker", _err.Error())
}

c := &Client{
// Broker config
config: config,
// Websocket config
wsConfig: wsConfig,
// The client will be created on connect!
WSClient: wsClient,
// Set the parent Context
parentCtx: ctx,
// Create the bool
ctxDone: _bool.New(),
// Logger
Logger: brokerLoggerInstance,
}
return c, nil
}
10 changes: 10 additions & 0 deletions core/clients/broker/client/disconnect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package client

func (c *Client) Disconnect() {
// We can create the layer for the broker only if we will add additional functionality...
// In that case, we should rename the function to Start
// In that case multiple goroutines will be running....
// But in this case only websocket will be running... so it's not necessary to create additional functionality here

c.WSClient.Disconnect()
}
86 changes: 86 additions & 0 deletions core/clients/broker/client/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package client

import "github.com/rs/zerolog"

/*
We should enable logging throughout the entire client, that will help us do a better debug
1. Add Enable/Disable logging
2. Add different log levels
3. Add a name of the log instance... usually it should take from the config or somehow?!... or we can setup also manually a name
4.
*/

// LDebug -> 0
func (c *Client) LDebug() *zerolog.Event {
return c.Logger.Debug()
}

// LInfo -> 1
func (c *Client) LInfo() *zerolog.Event {
return c.Logger.Info()
}

// LWarn -> 2
func (c *Client) LWarn() *zerolog.Event {
return c.Logger.Warn()
}

// LError -> 3
func (c *Client) LError() *zerolog.Event {
return c.Logger.Error()
}

// LFatal -> 4
func (c *Client) LFatal() *zerolog.Event {
return c.Logger.Fatal()
}

// LPanic -> 5
func (c *Client) LPanic() *zerolog.Event {
return c.Logger.Panic()
}

//

//-------------------------------------\\

//

func (c *Client) LEvent(eventType string, eventName string, beforeMsg func(event *zerolog.Event)) {
c.Logger.InfoEvent(eventType, eventName, beforeMsg)
}

func (c *Client) LEventCustom(eventType string, eventName string) *zerolog.Event {
return c.Logger.InfoEventCustom(eventType, eventName)
}

func (c *Client) LEventF(eventType string, eventName string, functionName string) *zerolog.Event {
return c.Logger.InfoEventF(eventType, eventName, functionName)
}

//

//-------------------------------------\\

//

// LWarnF -> when you need specifically to indicate in what function the logging is happening
func (c *Client) LWarnF(functionName string) *zerolog.Event {
return c.Logger.WarnF(functionName)
}

// LInfoF -> when you need specifically to indicate in what function the logging is happening
func (c *Client) LInfoF(functionName string) *zerolog.Event {
return c.Logger.InfoF(functionName)
}

// LDebugF -> when you need specifically to indicate in what function the logging is happening
func (c *Client) LDebugF(functionName string) *zerolog.Event {
return c.Logger.DebugF(functionName)
}

// LErrorF -> when you need specifically to indicate in what function the logging is happening
func (c *Client) LErrorF(functionName string) *zerolog.Event {
return c.Logger.ErrorF(functionName)
}
34 changes: 34 additions & 0 deletions core/clients/broker/client/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package client

import (
"context"
"github.com/KyaXTeam/go-core/v2/core/clients/broker/config"
wsClient "github.com/KyaXTeam/go-core/v2/core/clients/websocket/client"
wsConfig "github.com/KyaXTeam/go-core/v2/core/clients/websocket/config"
"github.com/KyaXTeam/go-core/v2/core/helpers/_context"
"github.com/KyaXTeam/go-core/v2/core/helpers/sync/_bool"
"github.com/KyaXTeam/go-core/v2/core/logger/model"
)

type Client struct {
// Broker Configuration
config config.Config
// WebSocket Configuration
wsConfig wsConfig.Config

// WebSocket Client which will be created on connect
WSClient *wsClient.Client

// Logger
Logger *model.Logger

//-----------CONTEXT-----------\\
// This is for stopping entirely the client... meaning to disconnect, stop the connection process etc...!
parentCtx context.Context
ctx *_context.CancelCtx
// THis is for canceling the connection process
ctxConnect *_context.CancelCtx
// Here we just set if it's done or not!
ctxDone *_bool.Bool
//-----------CONTEXT-----------\\
}
Loading

0 comments on commit e4486f8

Please sign in to comment.