diff --git a/cmd/admin.go b/cmd/admin.go index a8d2018..639c46c 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -19,7 +19,7 @@ func adminRCServer(mail_channel chan mail.Mail) *http.Server { engine.Use(middleware.CORS()) engine.Use(middleware.Authenticator()) engine.Use(middleware.EnsurePsuedoAdmin()) - engine.Use(gin.Recovery()) + engine.Use(gin.CustomRecovery(recoveryHandler)) engine.Use(gin.Logger()) rc.AdminRouter(mail_channel, engine) @@ -40,7 +40,7 @@ func adminApplicationServer(mail_channel chan mail.Mail) *http.Server { engine.Use(middleware.CORS()) engine.Use(middleware.Authenticator()) engine.Use(middleware.EnsurePsuedoAdmin()) - engine.Use(gin.Recovery()) + engine.Use(gin.CustomRecovery(recoveryHandler)) engine.Use(gin.Logger()) application.AdminRouter(mail_channel, engine) @@ -61,7 +61,7 @@ func adminCompanyServer() *http.Server { engine.Use(middleware.CORS()) engine.Use(middleware.Authenticator()) engine.Use(middleware.EnsureAdmin()) - engine.Use(gin.Recovery()) + engine.Use(gin.CustomRecovery(recoveryHandler)) engine.Use(gin.Logger()) company.AdminRouter(engine) @@ -82,6 +82,7 @@ func adminStudentServer() *http.Server { engine.Use(middleware.CORS()) engine.Use(middleware.Authenticator()) engine.Use(middleware.EnsurePsuedoAdmin()) + engine.Use(gin.CustomRecovery(recoveryHandler)) student.AdminRouter(engine) diff --git a/cmd/auth.go b/cmd/auth.go index b08a651..fe0b553 100644 --- a/cmd/auth.go +++ b/cmd/auth.go @@ -14,7 +14,7 @@ func authServer(mail_channel chan mail.Mail) *http.Server { PORT := viper.GetString("PORT.AUTH") r := gin.New() r.Use(middleware.CORS()) - r.Use(gin.Recovery()) + r.Use(gin.CustomRecovery(recoveryHandler)) r.Use(gin.Logger()) auth.Router(mail_channel, r) diff --git a/cmd/company.go b/cmd/company.go index 57e4c31..dcacac6 100644 --- a/cmd/company.go +++ b/cmd/company.go @@ -16,7 +16,7 @@ func companyServer() *http.Server { engine := gin.New() engine.Use(middleware.CORS()) engine.Use(middleware.Authenticator()) - engine.Use(gin.Recovery()) + engine.Use(gin.CustomRecovery(recoveryHandler)) engine.Use(gin.Logger()) rc.CompanyRouter(engine) diff --git a/cmd/panic_alert.go b/cmd/panic_alert.go new file mode 100644 index 0000000..fe4a38f --- /dev/null +++ b/cmd/panic_alert.go @@ -0,0 +1,55 @@ +package main + +import ( + "encoding/json" + "log" + "net" + "net/http" + "time" + + "github.com/sirupsen/logrus" + + "github.com/gin-gonic/gin" +) + +type alertMsg struct { + Endpoint string `json:"endpoint"` + Err interface{} `json:"error"` +} + +var alertChannel chan alertMsg + +var unix_socket = "/tmp/ras-backend.sock" + +func sendAlertToDiscord() { + conn, err := net.Dial("unix", unix_socket) + for err != nil { + // logrus.Error("Error in connecting to socket: ", err) + conn, err = net.Dial("unix", unix_socket) + time.Sleep(5 * time.Second) + } + defer conn.Close() + log.Println("Ready to send panic alerts") + for { + alert := <-alertChannel + jsonData, err := json.Marshal(alert) + if err != nil { + logrus.Error("Error in alerting panic: ", err) + continue + } + _, err = conn.Write(jsonData) + if err != nil { + logrus.Error("Error in writing data to socket: ", err) + } + } +} + +func recoveryHandler(c *gin.Context, err interface{}) { + alertChannel <- alertMsg{c.Request.URL.Path, err} + c.AbortWithStatus(http.StatusInternalServerError) +} + +func init() { + alertChannel = make(chan alertMsg) + go sendAlertToDiscord() +} diff --git a/cmd/ras.go b/cmd/ras.go index 49713da..8f55444 100644 --- a/cmd/ras.go +++ b/cmd/ras.go @@ -16,7 +16,7 @@ func rasServer(mail_channel chan mail.Mail) *http.Server { engine.Use(middleware.CORS()) // engine.Use(middleware.Authenticator()) ras.RASRouter(mail_channel, engine) - engine.Use(gin.Recovery()) + engine.Use(gin.CustomRecovery(recoveryHandler)) engine.Use(gin.Logger()) server := &http.Server{ diff --git a/cmd/student.go b/cmd/student.go index e124c28..56c58a5 100644 --- a/cmd/student.go +++ b/cmd/student.go @@ -17,7 +17,7 @@ func studentServer(mail_channel chan mail.Mail) *http.Server { engine := gin.New() engine.Use(middleware.CORS()) engine.Use(middleware.Authenticator()) - engine.Use(gin.Recovery()) + engine.Use(gin.CustomRecovery(recoveryHandler)) engine.Use(gin.Logger()) student.StudentRouter(engine)