-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathalert_controller.go
More file actions
102 lines (83 loc) · 2.56 KB
/
alert_controller.go
File metadata and controls
102 lines (83 loc) · 2.56 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"encoding/json"
"github.com/astaxie/beego"
"log"
"os"
"strconv"
)
// AlertController is a controller
type AlertController struct {
beego.Controller
}
type AlertErrorMessage struct {
Message string `json:"message"`
}
func (alertReceiver *AlertController) ServeErrorWithStatus(httpStatus int, err error) {
alertReceiver.ServeErrorWithStatusAndMessage(httpStatus, err, err.Error())
}
func (alertReceiver *AlertController) ServeErrorWithStatusAndMessage(httpStatus int, err error, message string) {
if err != nil {
alertReceiver.Data["json"] = AlertErrorMessage{Message: message}
alertReceiver.ServeJSONWithStatus(httpStatus)
}
}
// Get is a receiver method
func (alertReceiver *AlertController) GetAlertsOfOwner() {
requestedOwnerID, _ := strconv.Atoi(alertReceiver.Ctx.Input.Param(":id"))
alerts, err := ListAlerts(requestedOwnerID)
if err != nil {
alertReceiver.ServeErrorWithStatus(404, err)
} else {
alertReceiver.Data["json"] = alerts
alertReceiver.ServeJSONWithStatus(200)
}
}
// Post is a receiver method
func (alertReceiver *AlertController) CreateAlert() {
var alert Alert
json.Unmarshal(alertReceiver.Ctx.Input.RequestBody, &alert)
validationErr := alert.validate()
if validationErr != nil {
alertReceiver.ServeErrorWithStatus(400, validationErr)
return
}
alert.ID = GenerateAlertId()
err := triggerIngestion(alert)
if err != nil {
log.Printf("ERROR DURING INGESTION TRIGGER: %#v\n", err)
log.Println("Could not trigger ingestion service at (" + os.Getenv("DATA_INGESTION_URL") + ")")
alertReceiver.ServeErrorWithStatus(500, err)
return
}
createdAlert, err := save(alert)
if err != nil {
if err.Error() == ALERT_NAME_MUST_BE_UNIQUE_PER_OWNER {
alertReceiver.ServeErrorWithStatus(409, err)
} else {
alertReceiver.ServeErrorWithStatus(500, err)
}
return
}
alertReceiver.Data["json"] = createdAlert
alertReceiver.ServeJSONWithStatus(200)
}
func (alertReceiver *AlertController) GetAlertById() {
alertId := alertReceiver.Ctx.Input.Param(":id")
alert, err := FindAlert(alertId)
if err != nil {
alertReceiver.Ctx.WriteString(err.Error())
alertReceiver.ServeJSONWithStatus(500)
} else if (Alert{}) == alert {
alertReceiver.ServeJSONWithStatus(404)
} else {
alertReceiver.Data["json"] = alert
alertReceiver.ServeJSONWithStatus(200)
}
}
// ServeJSONWithStatus decorates responses
func (alertReceiver *AlertController) ServeJSONWithStatus(code int) {
alertReceiver.Ctx.Output.Header("Content-Type", "application/json;charset=UTF-8")
alertReceiver.Ctx.Output.SetStatus(code)
alertReceiver.ServeJSON()
}