-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from Krishnansh5/panic_alert
- sends gin.recovery panic alert to discord bot
- Loading branch information
Showing
6 changed files
with
63 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters