Fix notify path , Add notify test btn
This commit is contained in:
29
main.go
29
main.go
@@ -57,6 +57,7 @@ func main() {
|
|||||||
http.HandleFunc("/api/tasks", auth(handleTasks))
|
http.HandleFunc("/api/tasks", auth(handleTasks))
|
||||||
http.HandleFunc("/api/task", auth(handleTask))
|
http.HandleFunc("/api/task", auth(handleTask))
|
||||||
http.HandleFunc("/api/config", auth(handleConfig))
|
http.HandleFunc("/api/config", auth(handleConfig))
|
||||||
|
http.HandleFunc("/api/test-notification", auth(handleTestNotification))
|
||||||
|
|
||||||
port := os.Getenv("PORT")
|
port := os.Getenv("PORT")
|
||||||
if port == "" {
|
if port == "" {
|
||||||
@@ -226,6 +227,7 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
|
|||||||
<input id="timeout" type="number" min="5" required>
|
<input id="timeout" type="number" min="5" required>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-actions">
|
<div class="form-actions">
|
||||||
|
<button type="button" class="btn" onclick="testNotification()">测试通知</button>
|
||||||
<button type="button" class="btn" onclick="hideConfigModal()">取消</button>
|
<button type="button" class="btn" onclick="hideConfigModal()">取消</button>
|
||||||
<button type="submit" class="btn">保存</button>
|
<button type="submit" class="btn">保存</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -321,6 +323,11 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
|
|||||||
document.getElementById('configModal').classList.remove('show');
|
document.getElementById('configModal').classList.remove('show');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testNotification() {
|
||||||
|
fetch('/api/test-notification', {headers})
|
||||||
|
.then(r => r.ok ? alert('测试通知已发送,请检查 Gotify') : alert('发送失败,查看后台日志'));
|
||||||
|
}
|
||||||
|
|
||||||
function editTask(id) {
|
function editTask(id) {
|
||||||
fetch('/api/tasks', {headers})
|
fetch('/api/tasks', {headers})
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
@@ -515,11 +522,31 @@ func notify(title, msg string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
endpoint := strings.TrimRight(url, "/") + "/message"
|
||||||
payload := fmt.Sprintf(`{"title":"%s","message":"%s","priority":10}`, title, msg)
|
payload := fmt.Sprintf(`{"title":"%s","message":"%s","priority":10}`, title, msg)
|
||||||
http.Post(url+"/message?token="+token, "application/json", strings.NewReader(payload))
|
req, _ := http.NewRequest("POST", endpoint, strings.NewReader(payload))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("X-Gotify-Key", token)
|
||||||
|
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("发送通知失败: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
log.Printf("通知返回错误 %d: %s", resp.StatusCode, body)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleTestNotification(w http.ResponseWriter, r *http.Request) {
|
||||||
|
notify("测试通知", "Gotify 配置正常")
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
func updateClient() {
|
func updateClient() {
|
||||||
configMu.RLock()
|
configMu.RLock()
|
||||||
timeout := config.Timeout
|
timeout := config.Timeout
|
||||||
|
|||||||
Reference in New Issue
Block a user