-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
192 lines (164 loc) · 5.97 KB
/
Copy pathserver.go
File metadata and controls
192 lines (164 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
type NotifyPayload struct {
TaskID string `json:"task_id"`
TaskType string `json:"task_type"`
TaskDisplayName string `json:"task_display_name"`
RepoName string `json:"repo_name"`
Branch string `json:"branch"`
Recipients struct {
To []string `json:"to"`
CC []string `json:"cc"`
} `json:"recipients"`
Subject string `json:"subject"`
Summary string `json:"summary"`
MarkdownContent string `json:"markdown_content"`
MarkdownFilename string `json:"markdown_filename,omitempty"`
SynthesisJSONFilename string `json:"synthesis_json_filename,omitempty"`
SynthesisJSONContent string `json:"synthesis_json_content,omitempty"`
ReportURL string `json:"report_url,omitempty"`
}
type NotifyResponse struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
}
func StartHTTPServer(listener net.Listener) {
defer func() {
if r := recover(); r != nil {
LogMessage(fmt.Sprintf("HTTP Server Panic: %v", r))
}
}()
http.HandleFunc("/api/notify", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(NotifyResponse{Success: true})
})
http.HandleFunc("/api/notify/email", handleEmailNotify)
port := ":8081"
LogMessage("HTTP Server started on 0.0.0.0" + port)
err := http.Serve(listener, nil)
if err != nil {
LogMessage(fmt.Sprintf("HTTP Server failed: %v", err))
}
}
func handleEmailNotify(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
LogMessage(fmt.Sprintf("handleEmailNotify Panic: %v", r))
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(NotifyResponse{Success: false, Error: fmt.Sprintf("Internal Server Error: %v", r)})
}
}()
w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(NotifyResponse{Success: false, Error: "Method not allowed"})
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(NotifyResponse{Success: false, Error: "Failed to read body"})
return
}
defer r.Body.Close()
var payload NotifyPayload
if err := json.Unmarshal(body, &payload); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(NotifyResponse{Success: false, Error: "Invalid JSON payload"})
return
}
if payload.MarkdownContent == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(NotifyResponse{Success: false, Error: "Missing markdown_content format"})
return
}
LogMessage(fmt.Sprintf("Received email request for task: %s", payload.TaskID))
go processEmailPayload(payload)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(NotifyResponse{Success: true, Message: "Payload received. Processing in background."})
}
func processEmailPayload(payload NotifyPayload) {
defer func() {
if r := recover(); r != nil {
LogMessage(fmt.Sprintf("Email Processing Panic (Task: %s): %v", payload.TaskID, r))
}
}()
LogMessage("Generating PDF for task: " + payload.TaskID)
pdfPath, err := GeneratePDF(payload.MarkdownContent, payload.TaskID)
if err != nil {
LogMessage(fmt.Sprintf("Failed to generate PDF: %v", err))
return
}
LogMessage("PDF generated: " + pdfPath)
toEmails := strings.Join(payload.Recipients.To, ";")
ccEmails := strings.Join(payload.Recipients.CC, ";")
summaryText := payload.Summary
if summaryText == "" {
summaryText = ExtractSummary(payload.MarkdownContent)
}
templateContent := LoadTemplate()
finalMarkdownBody := RenderTemplate(templateContent, payload, summaryText)
finalHtmlBody, err := RenderMarkdownToHTML(finalMarkdownBody)
if err != nil {
LogMessage(fmt.Sprintf("Failed to render HTML from template: %v", err))
finalHtmlBody = "<p>" + finalMarkdownBody + "</p>"
}
// Log to GUI View
timeStr := time.Now().Format("01-02 15:04:05")
LogMessage(fmt.Sprintf("Ready to interact with Outlook for task: %s", payload.TaskID))
// Create a temp directory for additional attachments (Markdown and JSON synthesis)
tempDir := filepath.Join(os.TempDir(), "code-shield-notifier")
os.MkdirAll(tempDir, 0755)
var markdownPath string
if payload.MarkdownFilename != "" {
markdownPath = filepath.Join(tempDir, payload.MarkdownFilename)
err = os.WriteFile(markdownPath, []byte(payload.MarkdownContent), 0644)
if err != nil {
LogMessage(fmt.Sprintf("Failed to write Markdown file: %v", err))
markdownPath = ""
}
}
var synthesisPath string
if payload.SynthesisJSONFilename != "" && payload.SynthesisJSONContent != "" {
synthesisPath = filepath.Join(tempDir, payload.SynthesisJSONFilename)
err = os.WriteFile(synthesisPath, []byte(payload.SynthesisJSONContent), 0644)
if err != nil {
LogMessage(fmt.Sprintf("Failed to write Synthesis JSON file: %v", err))
synthesisPath = ""
}
}
attachmentPaths := []string{}
if pdfPath != "" {
attachmentPaths = append(attachmentPaths, pdfPath)
}
if markdownPath != "" {
attachmentPaths = append(attachmentPaths, markdownPath)
}
if synthesisPath != "" {
attachmentPaths = append(attachmentPaths, synthesisPath)
}
err = CreateAndHandleEmail(toEmails, ccEmails, payload.Subject, finalHtmlBody, attachmentPaths, GetAutoSend())
if err != nil {
LogMessage(fmt.Sprintf("Failed to Create/Send email via Outlook: %v", err))
AddDraftLogToView("失败", toEmails, payload.Subject, timeStr)
return
}
if GetAutoSend() {
LogMessage(fmt.Sprintf("Auto-send is ON. Email sent via Outlook! (Task: %s)", payload.TaskID))
AddDraftLogToView("已发送", toEmails, payload.Subject, timeStr)
} else {
LogMessage(fmt.Sprintf("Email saved to Outlook Drafts folder. (Task: %s)", payload.TaskID))
AddDraftLogToView("保存草稿", toEmails, payload.Subject, timeStr)
}
}