refactor: refactoring of code

This commit is contained in:
Jonas Kaninda
2024-12-06 14:21:55 +01:00
parent 9016a9ec7a
commit afd4afc83b
19 changed files with 483 additions and 340 deletions

View File

@@ -1,20 +1,5 @@
package utils
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/go-mail/mail"
"html/template"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
/*
MIT License
@@ -39,6 +24,22 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/go-mail/mail"
"github.com/jkaninda/mysql-bkup/pkg/logger"
"html/template"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
func parseTemplate[T any](data T, fileName string) (string, error) {
// Open the file
tmpl, err := template.ParseFiles(filepath.Join(templatePath, fileName))
@@ -55,7 +56,7 @@ func parseTemplate[T any](data T, fileName string) (string, error) {
}
func SendEmail(subject, body string) error {
Info("Start sending email notification....")
logger.Info("Start sending email notification....")
config := loadMailConfig()
emails := strings.Split(config.MailTo, ",")
m := mail.NewMessage()
@@ -67,16 +68,16 @@ func SendEmail(subject, body string) error {
d.TLSConfig = &tls.Config{InsecureSkipVerify: config.SkipTls}
if err := d.DialAndSend(m); err != nil {
Error("Error could not send email : %v", err)
logger.Error("Error could not send email : %v", err)
return err
}
Info("Email notification has been sent")
logger.Info("Email notification has been sent")
return nil
}
func sendMessage(msg string) error {
Info("Sending Telegram notification... ")
logger.Info("Sending Telegram notification... ")
chatId := os.Getenv("TG_CHAT_ID")
body, _ := json.Marshal(map[string]string{
"chat_id": chatId,
@@ -96,11 +97,11 @@ func sendMessage(msg string) error {
}
code := response.StatusCode
if code == 200 {
Info("Telegram notification has been sent")
logger.Info("Telegram notification has been sent")
return nil
} else {
body, _ := ioutil.ReadAll(response.Body)
Error("Error could not send message, error: %s", string(body))
body, _ := io.ReadAll(response.Body)
logger.Error("Error could not send message, error: %s", string(body))
return fmt.Errorf("error could not send message %s", string(body))
}
@@ -120,29 +121,29 @@ func NotifySuccess(notificationData *NotificationData) {
"MAIL_TO",
}
//Email notification
// Email notification
err := CheckEnvVars(mailVars)
if err == nil {
body, err := parseTemplate(*notificationData, "email.tmpl")
if err != nil {
Error("Could not parse email template: %v", err)
logger.Error("Could not parse email template: %v", err)
}
err = SendEmail(fmt.Sprintf("✅ Database Backup Notification %s", notificationData.Database), body)
if err != nil {
Error("Could not send email: %v", err)
logger.Error("Could not send email: %v", err)
}
}
//Telegram notification
// Telegram notification
err = CheckEnvVars(vars)
if err == nil {
message, err := parseTemplate(*notificationData, "telegram.tmpl")
if err != nil {
Error("Could not parse telegram template: %v", err)
logger.Error("Could not parse telegram template: %v", err)
}
err = sendMessage(message)
if err != nil {
Error("Could not send Telegram message: %v", err)
logger.Error("Could not send Telegram message: %v", err)
}
}
}
@@ -160,7 +161,7 @@ func NotifyError(error string) {
"MAIL_TO",
}
//Email notification
// Email notification
err := CheckEnvVars(mailVars)
if err == nil {
body, err := parseTemplate(ErrorMessage{
@@ -169,14 +170,14 @@ func NotifyError(error string) {
BackupReference: os.Getenv("BACKUP_REFERENCE"),
}, "email-error.tmpl")
if err != nil {
Error("Could not parse error template: %v", err)
logger.Error("Could not parse error template: %v", err)
}
err = SendEmail(fmt.Sprintf("🔴 Urgent: Database Backup Failure Notification"), body)
err = SendEmail("🔴 Urgent: Database Backup Failure Notification", body)
if err != nil {
Error("Could not send email: %v", err)
logger.Error("Could not send email: %v", err)
}
}
//Telegram notification
// Telegram notification
err = CheckEnvVars(vars)
if err == nil {
message, err := parseTemplate(ErrorMessage{
@@ -185,13 +186,13 @@ func NotifyError(error string) {
BackupReference: os.Getenv("BACKUP_REFERENCE"),
}, "telegram-error.tmpl")
if err != nil {
Error("Could not parse error template: %v", err)
logger.Error("Could not parse error template: %v", err)
}
err = sendMessage(message)
if err != nil {
Error("Could not send telegram message: %v", err)
logger.Error("Could not send telegram message: %v", err)
}
}
}