Skip to content

Commit

Permalink
optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
xinliangnote committed Nov 5, 2019
1 parent 9972683 commit a8cb381
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 22 deletions.
11 changes: 10 additions & 1 deletion app/route/middleware/exception/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ func SetUp() gin.HandlerFunc {
body = strings.ReplaceAll(body, "{RequestIP}", c.ClientIP())
body = strings.ReplaceAll(body, "{DebugStack}", DebugStack)

_ = mail.SendMail(config.ErrorNotifyUser, subject, body)
options := &mail.Options{
MailHost : config.SystemEmailHost,
MailPort : config.SystemEmailPort,
MailUser : config.SystemEmailUser,
MailPass : config.SystemEmailPass,
MailTo : config.ErrorNotifyUser,
Subject : subject,
Body : body,
}
_ = mail.Send(options)

utilGin := response.Gin{Ctx: c}
utilGin.Response(500, "系统异常,请联系管理员!", nil)
Expand Down
3 changes: 1 addition & 2 deletions app/route/middleware/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ func SetUp() gin.HandlerFunc {

accessLogMap["cost_time"] = fmt.Sprintf("%vms", endTime-startTime)

accessLogJson, _ := jsonUtil.JsonEncode(accessLogMap)

accessLogJson, _ := jsonUtil.Encode(accessLogMap)
accessChannel <- accessLogJson
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/route/middleware/requestid/requestid.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package requestid

import (
"github.com/gin-gonic/gin"
"go-gin-api/app/util"
"go-gin-api/app/util/uuid"
)

func SetUp() gin.HandlerFunc {

return func(c *gin.Context) {
requestId := c.Request.Header.Get("X-Request-Id")
if requestId == "" {
requestId = util.GenUUID()
requestId = uuid.GenUUID()
}
c.Set("X-Request-Id", requestId)
c.Writer.Header().Set("X-Request-Id", requestId)
Expand Down
18 changes: 14 additions & 4 deletions app/util/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"go-gin-api/app/config"
"go-gin-api/app/route/middleware/exception"
"go-gin-api/app/util/json"
time2 "go-gin-api/app/util/time"
"go-gin-api/app/util/mail"
timeUtil "go-gin-api/app/util/time"
"log"
"os"
"runtime/debug"
Expand Down Expand Up @@ -55,14 +56,23 @@ func alarm(level string, str string) {
subject := fmt.Sprintf("【系统告警】%s 项目出错了!", config.AppName)

body := strings.ReplaceAll(exception.MailTemplate, "{ErrorMsg}", fmt.Sprintf("%s", str))
body = strings.ReplaceAll(body, "{RequestTime}", time2.GetCurrentDate())
body = strings.ReplaceAll(body, "{RequestTime}", timeUtil.GetCurrentDate())
body = strings.ReplaceAll(body, "{RequestURL}", "--")
body = strings.ReplaceAll(body, "{RequestUA}", "--")
body = strings.ReplaceAll(body, "{RequestIP}", "--")
body = strings.ReplaceAll(body, "{DebugStack}", DebugStack)

// 执行发邮件
_ = SendMail(config.ErrorNotifyUser, subject, body)
options := &mail.Options{
MailHost : config.SystemEmailHost,
MailPort : config.SystemEmailPort,
MailUser : config.SystemEmailUser,
MailPass : config.SystemEmailPass,
MailTo : config.ErrorNotifyUser,
Subject : subject,
Body : body,
}
_ = mail.Send(options)

} else if level == "SMS" {
// 执行发短信
Expand All @@ -80,7 +90,7 @@ func alarm(level string, str string) {
errorLogMap["time"] = time.Now().Format("2006/01/02 - 15:04:05")
errorLogMap["info"] = str

errorLogJson, _ := json.JsonEncode(errorLogMap)
errorLogJson, _ := json.Encode(errorLogMap)
_, _ = f.WriteString(errorLogJson + "\n")
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/util/grpc_log/grpc_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func ClientInterceptor() grpc.UnaryClientInterceptor {

grpcLogMap["cost_time"] = fmt.Sprintf("%vms", endTime-startTime)

grpcLogJson, _ := json.JsonEncode(grpcLogMap)
grpcLogJson, _ := json.Encode(grpcLogMap)

grpcChannel <- grpcLogJson

Expand Down
2 changes: 1 addition & 1 deletion app/util/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package json

import "encoding/json"

func JsonEncode(v interface{}) (string, error) {
func Encode(v interface{}) (string, error) {
bytes, err := json.Marshal(v)
if err != nil {
return "", err
Expand Down
27 changes: 16 additions & 11 deletions app/util/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,38 @@ package mail

import (
"fmt"
"go-gin-api/app/config"
"gopkg.in/gomail.v2"
"strings"
)

func SendMail(mailTo string, subject string, body string) error {

if config.ErrorNotifyOpen != 1 {
return nil
}
type Options struct {
MailHost string
MailPort int
MailUser string // 发件人
MailPass string // 发件人密码
MailTo string // 收件人 多个用,分割
Subject string // 邮件主题
Body string // 邮件内容
}

func Send(o *Options) error {

m := gomail.NewMessage()

//设置发件人
m.SetHeader("From", config.SystemEmailUser)
m.SetHeader("From", o.MailUser)

//设置发送给多个用户
mailArrTo := strings.Split(mailTo, ",")
mailArrTo := strings.Split(o.MailTo, ",")
m.SetHeader("To", mailArrTo...)

//设置邮件主题
m.SetHeader("Subject", subject)
m.SetHeader("Subject", o.Subject)

//设置邮件正文
m.SetBody("text/html", body)
m.SetBody("text/html", o.Body)

d := gomail.NewDialer(config.SystemEmailHost, config.SystemEmailPort, config.SystemEmailUser, config.SystemEmailPass)
d := gomail.NewDialer(o.MailHost, o.MailPort, o.MailUser, o.MailPass)

err := d.DialAndSend(m)
if err != nil {
Expand Down

0 comments on commit a8cb381

Please sign in to comment.