framework.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package framework
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. log "github.com/sirupsen/logrus"
  7. "io"
  8. "net"
  9. "net/http"
  10. "os"
  11. "path/filepath"
  12. "pmail/dto/parsemail"
  13. "pmail/utils/context"
  14. "time"
  15. )
  16. type EmailHook interface {
  17. // SendBefore 邮件发送前的数据 同步执行
  18. SendBefore(ctx *context.Context, email *parsemail.Email)
  19. // SendAfter 邮件发送后的数据,err是每个收信服务器的错误信息 异步执行
  20. SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error)
  21. // ReceiveParseBefore 接收到邮件,解析之前的原始数据 同步执行
  22. ReceiveParseBefore(ctx *context.Context, email *[]byte)
  23. // ReceiveParseAfter 接收到邮件,解析之后的结构化数据 (收信规则前,写数据库前执行) 同步执行
  24. ReceiveParseAfter(ctx *context.Context, email *parsemail.Email)
  25. // ReceiveSaveAfter 邮件落库以后执行(收信规则后执行) 异步执行
  26. ReceiveSaveAfter(ctx *context.Context, email *parsemail.Email)
  27. }
  28. // HookDTO PMail 主程序和插件通信的结构体
  29. type HookDTO struct {
  30. ServerVersion string // 服务端程序版本
  31. Ctx *context.Context // 上下文
  32. Email *parsemail.Email // 邮件内容
  33. EmailByte *[]byte // 未解析前的文件内容
  34. ErrMap map[string]error // 错误信息
  35. }
  36. type Plugin struct {
  37. name string
  38. hook EmailHook
  39. }
  40. func CreatePlugin(name string, hook EmailHook) *Plugin {
  41. return &Plugin{
  42. name: name,
  43. hook: hook,
  44. }
  45. }
  46. type logFormatter struct {
  47. }
  48. // Format 定义日志输出格式
  49. func (l *logFormatter) Format(entry *log.Entry) ([]byte, error) {
  50. b := bytes.Buffer{}
  51. b.WriteString(fmt.Sprintf("[%s]", entry.Level.String()))
  52. b.WriteString(fmt.Sprintf("[%s]", entry.Time.Format("2006-01-02 15:04:05")))
  53. if entry.Context != nil {
  54. ctx := entry.Context.(*context.Context)
  55. if ctx != nil {
  56. b.WriteString(fmt.Sprintf("[%s]", ctx.GetValue(context.LogID)))
  57. }
  58. }
  59. b.WriteString(fmt.Sprintf("[%s:%d]", entry.Caller.File, entry.Caller.Line))
  60. b.WriteString(entry.Message)
  61. b.WriteString("\n")
  62. return b.Bytes(), nil
  63. }
  64. func (p *Plugin) Run() {
  65. // 设置日志格式为json格式
  66. log.SetFormatter(&logFormatter{})
  67. log.SetReportCaller(true)
  68. // 设置将日志输出到标准输出(默认的输出为stderr,标准错误)
  69. // 日志消息输出可以是任意的io.writer类型
  70. log.SetOutput(os.Stdout)
  71. if len(os.Args) < 2 {
  72. panic("Command Params Error!")
  73. }
  74. mux := http.NewServeMux()
  75. mux.HandleFunc("/SendBefore", func(writer http.ResponseWriter, request *http.Request) {
  76. log.Debugf("[%s] SendBefore Start", p.name)
  77. var hookDTO HookDTO
  78. body, _ := io.ReadAll(request.Body)
  79. err := json.Unmarshal(body, &hookDTO)
  80. if err != nil {
  81. log.Errorf("params error %+v", err)
  82. return
  83. }
  84. p.hook.SendBefore(hookDTO.Ctx, hookDTO.Email)
  85. body, _ = json.Marshal(hookDTO)
  86. writer.Write(body)
  87. log.Debugf("[%s] SendBefore End", p.name)
  88. })
  89. mux.HandleFunc("/SendAfter", func(writer http.ResponseWriter, request *http.Request) {
  90. log.Debugf("[%s] SendAfter Start", p.name)
  91. var hookDTO HookDTO
  92. body, _ := io.ReadAll(request.Body)
  93. err := json.Unmarshal(body, &hookDTO)
  94. if err != nil {
  95. log.Errorf("params error %+v", err)
  96. return
  97. }
  98. p.hook.SendAfter(hookDTO.Ctx, hookDTO.Email, hookDTO.ErrMap)
  99. body, _ = json.Marshal(hookDTO)
  100. writer.Write(body)
  101. log.Debugf("[%s] SendAfter End", p.name)
  102. })
  103. mux.HandleFunc("/ReceiveParseBefore", func(writer http.ResponseWriter, request *http.Request) {
  104. log.Debugf("[%s] ReceiveParseBefore Start", p.name)
  105. var hookDTO HookDTO
  106. body, _ := io.ReadAll(request.Body)
  107. err := json.Unmarshal(body, &hookDTO)
  108. if err != nil {
  109. log.Errorf("params error %+v", err)
  110. return
  111. }
  112. p.hook.ReceiveParseBefore(hookDTO.Ctx, hookDTO.EmailByte)
  113. body, _ = json.Marshal(hookDTO)
  114. writer.Write(body)
  115. log.Debugf("[%s] ReceiveParseBefore End", p.name)
  116. })
  117. mux.HandleFunc("/ReceiveParseAfter", func(writer http.ResponseWriter, request *http.Request) {
  118. log.Debugf("[%s] ReceiveParseAfter Start", p.name)
  119. var hookDTO HookDTO
  120. body, _ := io.ReadAll(request.Body)
  121. err := json.Unmarshal(body, &hookDTO)
  122. if err != nil {
  123. log.Errorf("params error %+v", err)
  124. return
  125. }
  126. p.hook.ReceiveParseAfter(hookDTO.Ctx, hookDTO.Email)
  127. body, _ = json.Marshal(hookDTO)
  128. writer.Write(body)
  129. log.Debugf("[%s] ReceiveParseAfter End", p.name)
  130. })
  131. mux.HandleFunc("/ReceiveSaveAfter", func(writer http.ResponseWriter, request *http.Request) {
  132. log.Debugf("[%s] ReceiveSaveAfter Start", p.name)
  133. var hookDTO HookDTO
  134. body, _ := io.ReadAll(request.Body)
  135. err := json.Unmarshal(body, &hookDTO)
  136. if err != nil {
  137. log.Errorf("params error %+v", err)
  138. return
  139. }
  140. p.hook.ReceiveSaveAfter(hookDTO.Ctx, hookDTO.Email)
  141. body, _ = json.Marshal(hookDTO)
  142. writer.Write(body)
  143. log.Debugf("[%s] ReceiveSaveAfter End", p.name)
  144. })
  145. server := http.Server{
  146. ReadTimeout: 5 * time.Second,
  147. WriteTimeout: 5 * time.Second,
  148. Handler: mux,
  149. }
  150. unixListener, err := net.Listen("unix", getExePath()+"/"+os.Args[1])
  151. if err != nil {
  152. panic(err)
  153. }
  154. err = server.Serve(unixListener)
  155. if err != nil {
  156. panic(err)
  157. }
  158. }
  159. func getExePath() string {
  160. ex, err := os.Executable()
  161. if err != nil {
  162. panic(err)
  163. }
  164. exePath := filepath.Dir(ex)
  165. return exePath
  166. }