Skip to content

Commit

Permalink
Merge pull request #149 from Krishnansh5/panic_alert
Browse files Browse the repository at this point in the history
- sends gin.recovery panic alert to discord bot
  • Loading branch information
Krishnansh5 authored Feb 9, 2024
2 parents b1aee0a + 97564d1 commit 409b381
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
7 changes: 4 additions & 3 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions cmd/panic_alert.go
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion cmd/ras.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion cmd/student.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 409b381

Please sign in to comment.