mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-11 17:52:50 +01:00
60
busservice/busservice.go
Normal file
60
busservice/busservice.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package busservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/SecurityBrewery/catalyst/bus"
|
||||
"github.com/SecurityBrewery/catalyst/database"
|
||||
"github.com/SecurityBrewery/catalyst/database/busdb"
|
||||
"github.com/SecurityBrewery/catalyst/generated/models"
|
||||
"github.com/SecurityBrewery/catalyst/role"
|
||||
"github.com/SecurityBrewery/catalyst/time"
|
||||
)
|
||||
|
||||
type busService struct {
|
||||
db *database.Database
|
||||
apiURL string
|
||||
apiKey string
|
||||
catalystBus *bus.Bus
|
||||
}
|
||||
|
||||
func New(apiurl, apikey string, catalystBus *bus.Bus, db *database.Database) error {
|
||||
|
||||
h := &busService{db: db, apiURL: apiurl, apiKey: apikey, catalystBus: catalystBus}
|
||||
|
||||
if err := catalystBus.SubscribeRequest(h.logRequest); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := catalystBus.SubscribeResult(h.handleResult); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := catalystBus.SubscribeJob(h.handleJob); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func busContext() context.Context {
|
||||
// TODO: change roles?
|
||||
bot := &models.UserResponse{ID: "bot", Roles: []string{role.Admin}}
|
||||
return busdb.UserContext(context.Background(), bot)
|
||||
}
|
||||
|
||||
func (h *busService) logRequest(msg *bus.RequestMsg) {
|
||||
var logEntries []*models.LogEntry
|
||||
for _, i := range msg.IDs {
|
||||
logEntries = append(logEntries, &models.LogEntry{
|
||||
Type: bus.ChannelRequest,
|
||||
Reference: i.String(),
|
||||
Creator: msg.User,
|
||||
Message: msg.Function,
|
||||
Created: time.Now().UTC(),
|
||||
})
|
||||
}
|
||||
|
||||
if err := h.db.LogBatchCreate(busContext(), logEntries); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user