Add Audit cababilities. First try on deleteOne
This commit is contained in:
44
main.go
44
main.go
@@ -17,6 +17,7 @@ import (
|
||||
|
||||
"git.gsuntres.com/general/commons"
|
||||
"git.gsuntres.com/general/sys"
|
||||
"git.gsuntres.com/general/events"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -87,6 +88,14 @@ type MongoClient struct {
|
||||
Registry map[string]*CollectionDefinition
|
||||
// Relaxed if set to true will not enforce schema
|
||||
Relaxed bool
|
||||
// Context fields to serialize when needed
|
||||
ContextFields []string
|
||||
// WithAudit enable auditing functionality.
|
||||
WithAudit bool
|
||||
// OnAudit callback called when audit is enabled.
|
||||
OnAudit *OnAudit
|
||||
// IgnoreAudit a list of entities to ignore
|
||||
IgnoreAudit []string
|
||||
}
|
||||
|
||||
func (c *MongoClient) SetRelaxed() {
|
||||
@@ -250,6 +259,7 @@ var client *MongoClient = &MongoClient{
|
||||
Limit: 10,
|
||||
Registry: make(map[string]*CollectionDefinition, 0),
|
||||
Relaxed: false,
|
||||
IgnoreAudit: []string{"event"},
|
||||
}
|
||||
|
||||
func GetMongoClient() *MongoClient {
|
||||
@@ -280,6 +290,9 @@ type MongoStartProps struct {
|
||||
MongoPass string
|
||||
MongoDebugQuery bool
|
||||
MongoDBPrefix string
|
||||
ContextFields []string
|
||||
WithAudit bool
|
||||
OnAudit *OnAudit
|
||||
AllowTruncatingDoubles bool
|
||||
}
|
||||
|
||||
@@ -314,18 +327,30 @@ func Start(props *MongoStartProps) error {
|
||||
}).
|
||||
SetRegistry(GetCustomRegistry())
|
||||
|
||||
colors := []string{
|
||||
commons.EscapeGreen,
|
||||
commons.EscapeRed,
|
||||
commons.EscapeYellow,
|
||||
commons.EscapeBlue,
|
||||
commons.EscapeMagenta,
|
||||
commons.EscapeCyan,
|
||||
}
|
||||
|
||||
client.DebugQuery = props.MongoDebugQuery
|
||||
if client.DebugQuery {
|
||||
// Debug queries
|
||||
monitor := &event.CommandMonitor{
|
||||
Started: func(_ context.Context, e *event.CommandStartedEvent) {
|
||||
log.Printf("%d@Start %s#%s %s", e.RequestID, e.DatabaseName, e.CommandName, e.Command)
|
||||
ecode := colors[e.RequestID % 6]
|
||||
log.Printf("%s%d@Start%s %s#%s %s", ecode, e.RequestID, commons.EscapeReset, e.DatabaseName, e.CommandName, e.Command)
|
||||
},
|
||||
Succeeded: func(_ context.Context, e *event.CommandSucceededEvent) {
|
||||
log.Printf("%d@OK in %s", e.RequestID, e.Reply)
|
||||
ecode := colors[e.RequestID % 6]
|
||||
log.Printf("%s%d@OK%s in %s", ecode, e.RequestID, commons.EscapeReset, e.Reply)
|
||||
},
|
||||
Failed: func(_ context.Context, e *event.CommandFailedEvent) {
|
||||
log.Printf("%d@Fail in %s", e.RequestID, e.Failure)
|
||||
ecode := colors[e.RequestID % 6]
|
||||
log.Printf("%s%d@Fail%s in %s", ecode, e.RequestID, commons.EscapeReset, e.Failure)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -335,6 +360,19 @@ func Start(props *MongoStartProps) error {
|
||||
// set DBPrefix
|
||||
client.DBPrefix = props.MongoDBPrefix
|
||||
|
||||
client.ContextFields = props.ContextFields
|
||||
|
||||
client.WithAudit = props.WithAudit
|
||||
|
||||
client.OnAudit = props.OnAudit
|
||||
|
||||
if client.WithAudit {
|
||||
var onAudit OnAudit = func(audit *AuditResult) error {
|
||||
return events.Publish(audit)
|
||||
}
|
||||
client.OnAudit = &onAudit
|
||||
}
|
||||
|
||||
if err := cOptions.Validate(); err != nil {
|
||||
log.Fatalf("Failed to validate mongo options: %+v", err.Error())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user