generated from Fishwaldo/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
46 lines (41 loc) · 1.58 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package msg
import (
"github.com/Fishwaldo/mouthpiece/internal/db"
"github.com/Fishwaldo/mouthpiece/internal/errors"
. "github.com/Fishwaldo/mouthpiece/internal/log"
"gorm.io/gorm"
"time"
)
func InitializeMessage() {
db.Db.AutoMigrate(&Message{})
db.Db.AutoMigrate(&MessageResult{})
}
type Message struct {
gorm.Model
AppName string `path:"application" doc:"Application Name" Example:"MyApp"`
Body struct {
Message string `json:"message" doc:"Message to be Sent"`
ShortMsg string `json:"shortmessage,omitempty" doc:"Short Message to be Sent"`
Topic string `json:"topic,omitempty" doc:"Topic of Message"`
Severity string `json:"severity,omitempty" doc:"Severity of Message" default:"INFO"`
Timestamp time.Time `json:"timestamp,omitempty" doc:"Timestamp of Message"`
Fields map[string]interface{} `json:"fields,omitempty" doc:"Additional Fields" gorm:"-"`
} `json:"body" doc:"Message Body" gorm:"embedded"`
Result *MessageResult `json:"result,omitempty" doc:"Result of Message"`
}
type MessageResult struct {
MessageID uint `json:"message_id" doc:"Message ID"`
Status string `json:"status" doc:"Status of Message"`
}
func (msg *Message) ProcessMessage() (err error) {
if len(msg.AppName) == 0 {
return mperror.ErrAppNotFound
} else {
Log.V(1).Info("Processing Message", "Message", msg)
if tx := db.Db.Create(&msg); tx.Error != nil {
panic("Can't Save Message to Database: %s" + tx.Error.Error())
}
msg.Result = &MessageResult{MessageID: msg.ID, Status: "Queued"}
return nil
}
}