This commit is contained in:
teddy
2025-04-19 01:05:10 +07:00
parent 2a67bb260f
commit 04de3f84a7
5 changed files with 207 additions and 0 deletions

View File

@@ -0,0 +1 @@
.env-example

15
.env-example Normal file
View File

@@ -0,0 +1,15 @@
PORT=3000
DB_PORT=
DB_NAME=
DB_USERNAME=
DB_PASSWORD=
DB_HOST=
SMTP_PORT=
SMTP_HOST=
SMTP_SENDER=
AUTH_EMAIL=
AUTH_PASSWORD=
ALERT_EMAIL=xxx@gmail.com,yyy@gmail.com

View File

@@ -8,6 +8,8 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"os"
"strings"
"time" "time"
"gorm.io/gorm" "gorm.io/gorm"
@@ -119,6 +121,78 @@ func Route(db *gorm.DB) {
w.Write([]byte("success")) w.Write([]byte("success"))
})) }))
http.HandleFunc("/server-down-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
sessionID := helper.GenerateSessionID()
module := "SERVER DOWN NOTIFICATION"
log.Printf("%+v %+v request %+v", sessionID, module, nil)
subject := "Server Down 🚨"
sendTo := strings.Split(os.Getenv("ALERT_EMAIL"), ",")
type Data struct {
TimeAt string
Host string
}
data := Data{TimeAt: time.Now().Format("2006-01-02 15:04:05"), Host: "36.66.3.44"}
htmlString := helper.ParseHTML("template/server-down.html", data)
go func() {
log.Printf("%+v %+v SENDING EMAIL TO %+v", sessionID, module, sendTo)
err := helper.SendEmail(sendTo, sendTo, "", subject, "", htmlString)
if err != nil {
log.Printf("%+v %+v response %+v", sessionID, module, err.Error())
} else {
log.Printf("%+v %+v EMAIL SENT TO %+v", sessionID, module, sendTo)
}
}()
log.Printf("%+v %+v response %+v", sessionID, module, "success")
w.Write([]byte("success"))
}))
http.HandleFunc("/high-memory-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
sessionID := helper.GenerateSessionID()
module := "HIGH MEMORY USAGE NOTIFICATION"
log.Printf("%+v %+v request %+v", sessionID, module, nil)
subject := "High Memory Usage 🚨"
sendTo := strings.Split(os.Getenv("ALERT_EMAIL"), ",")
type Data struct {
TimeAt string
Host string
}
data := Data{TimeAt: time.Now().Format("2006-01-02 15:04:05"), Host: "36.66.3.44"}
htmlString := helper.ParseHTML("template/high-memory-usage.html", data)
go func() {
log.Printf("%+v %+v SENDING EMAIL TO %+v", sessionID, module, sendTo)
err := helper.SendEmail(sendTo, sendTo, "", subject, "", htmlString)
if err != nil {
log.Printf("%+v %+v response %+v", sessionID, module, err.Error())
} else {
log.Printf("%+v %+v EMAIL SENT TO %+v", sessionID, module, sendTo)
}
}()
log.Printf("%+v %+v response %+v", sessionID, module, "success")
w.Write([]byte("success"))
}))
http.HandleFunc("*", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("*", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404) w.WriteHeader(404)
w.Write([]byte("what are you looking for!?")) w.Write([]byte("what are you looking for!?"))

View File

@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8" />
<title>🚨 Penggunaan Memori Tinggi</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f4f6;
padding: 20px;
}
.container {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
max-width: 100%;
margin: auto;
border: 1px solid #e0e0e0;
}
.header {
color: #d62828;
font-size: 20px;
font-weight: bold;
margin-bottom: 15px;
}
.info {
font-size: 14px;
color: #333333;
margin-bottom: 10px;
}
.footer {
font-size: 12px;
color: #888;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">🚨 Peringatan: Penggunaan Memori Tinggi</div>
<div class="info">
<strong>🖥 Host:</strong> {{.Host}}<br />
<strong>📊 Penggunaan RAM:</strong> Melebihi 85%<br />
<strong>🕒 Waktu Deteksi:</strong> {{.TimeAt}}<br />
</div>
<p>
Penggunaan memori pada server telah melebihi batas yang diizinkan. Mohon
segera lakukan pemeriksaan untuk menghindari potensi gangguan layanan.
</p>
<div class="footer">
Email ini dikirim secara otomatis oleh sistem monitoring. Terima kasih.
</div>
</div>
</body>
</html>

59
template/server-down.html Normal file
View File

@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8" />
<title>🚨 Server Down</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f6f8fa;
color: #333;
padding: 20px;
}
.container {
background-color: #ffffff;
border-radius: 8px;
padding: 20px;
border: 1px solid #e1e4e8;
max-width: 100%;
margin: auto;
}
.header {
font-size: 20px;
font-weight: bold;
color: #e63946;
margin-bottom: 10px;
}
.info {
margin: 10px 0;
}
.footer {
font-size: 12px;
color: #888;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
🚨 Peringatan: Server Down
</div>
<div class="info">
<strong>🕒 Waktu Deteksi:</strong> {{.TimeAt}}<br />
<strong>🌐 Host / Endpoint:</strong> {{.Host}}<br />
</div>
<p>
Mohon segera lakukan pengecekan pada sistem terkait agar layanan dapat
kembali normal.
</p>
<div class="footer">
Email ini dikirim secara otomatis oleh sistem monitoring.<br />
Terima kasih atas perhatian dan tindak lanjutnya.
</div>
</div>
</body>
</html>