mirror of
https://github.com/mariocandela/beelzebub.git
synced 2025-07-01 18:47:26 -04:00
Refactoring protocolManager.go and tracer.go, improce dependency injection
This commit is contained in:
13
main.go
13
main.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"beelzebub/parser"
|
"beelzebub/parser"
|
||||||
"beelzebub/protocols"
|
"beelzebub/protocols"
|
||||||
|
"beelzebub/tracer"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@ -31,8 +32,7 @@ func main() {
|
|||||||
hypertextTransferProtocolStrategy := &protocols.HypertextTransferProtocolStrategy{}
|
hypertextTransferProtocolStrategy := &protocols.HypertextTransferProtocolStrategy{}
|
||||||
|
|
||||||
// Protocol manager
|
// Protocol manager
|
||||||
manager := protocols.ProtocolManager{}
|
serviceManager := protocols.InitProtocolManager(logStrategy, hypertextTransferProtocolStrategy)
|
||||||
serviceManager := manager.InitServiceManager()
|
|
||||||
|
|
||||||
for _, beelzebubServiceConfiguration := range beelzebubServicesConfiguration {
|
for _, beelzebubServiceConfiguration := range beelzebubServicesConfiguration {
|
||||||
switch beelzebubServiceConfiguration.Protocol {
|
switch beelzebubServiceConfiguration.Protocol {
|
||||||
@ -54,15 +54,20 @@ func main() {
|
|||||||
}
|
}
|
||||||
<-quit
|
<-quit
|
||||||
}
|
}
|
||||||
|
func logStrategy(event tracer.Event) {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"status": event.Status.String(),
|
||||||
|
"event": event,
|
||||||
|
}).Info("New Event")
|
||||||
|
}
|
||||||
|
|
||||||
func configureLogging(configurations parser.Logging) *os.File {
|
func configureLogging(configurations parser.Logging) *os.File {
|
||||||
file, err := os.OpenFile(configurations.LogsPath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
file, err := os.OpenFile(configurations.LogsPath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error opening file: %v", err)
|
log.Fatalf("error opening file: %v", err)
|
||||||
}
|
}
|
||||||
mw := io.MultiWriter(os.Stdout, file)
|
|
||||||
|
|
||||||
log.SetOutput(mw)
|
log.SetOutput(io.MultiWriter(os.Stdout, file))
|
||||||
|
|
||||||
log.SetFormatter(&log.JSONFormatter{
|
log.SetFormatter(&log.JSONFormatter{
|
||||||
DisableTimestamp: configurations.LogDisableTimestamp,
|
DisableTimestamp: configurations.LogDisableTimestamp,
|
||||||
|
@ -5,14 +5,19 @@ import (
|
|||||||
"beelzebub/tracer"
|
"beelzebub/tracer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ServiceStrategy interface {
|
||||||
|
Init(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration, tracer tracer.Tracer) error
|
||||||
|
}
|
||||||
|
|
||||||
type ProtocolManager struct {
|
type ProtocolManager struct {
|
||||||
strategy ServiceStrategy
|
strategy ServiceStrategy
|
||||||
tracer *tracer.Tracer
|
tracer *tracer.Tracer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pm *ProtocolManager) InitServiceManager() *ProtocolManager {
|
func InitProtocolManager(tracerStrategy tracer.Strategy, strategy ServiceStrategy) *ProtocolManager {
|
||||||
return &ProtocolManager{
|
return &ProtocolManager{
|
||||||
tracer: tracer.Init(),
|
tracer: tracer.Init(tracerStrategy),
|
||||||
|
strategy: strategy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
package protocols
|
|
||||||
|
|
||||||
import (
|
|
||||||
"beelzebub/parser"
|
|
||||||
"beelzebub/tracer"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ServiceStrategy interface {
|
|
||||||
Init(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration, tracer tracer.Tracer) error
|
|
||||||
}
|
|
@ -1,22 +1,23 @@
|
|||||||
package tracer
|
package tracer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Strategy func(event Event)
|
||||||
|
|
||||||
type Tracer struct {
|
type Tracer struct {
|
||||||
|
strategy Strategy
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init() *Tracer {
|
func Init(strategy Strategy) *Tracer {
|
||||||
return &Tracer{}
|
return &Tracer{
|
||||||
|
strategy: strategy,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tracer *Tracer) TraceEvent(event Event) {
|
func (tracer *Tracer) TraceEvent(event Event) {
|
||||||
log.WithFields(log.Fields{
|
tracer.strategy(event)
|
||||||
"status": event.Status.String(),
|
|
||||||
"event": event,
|
|
||||||
}).Info("New Event")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
|
Reference in New Issue
Block a user