diff --git a/handlers/routes.go b/handlers/routes.go index c4f8933d..f50c11d2 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -74,6 +74,7 @@ func Router() *mux.Router { r.Handle("/settings/build", http.HandlerFunc(saveAssetsHandler)).Methods("GET") r.Handle("/settings/delete_assets", http.HandlerFunc(deleteAssetsHandler)).Methods("GET") r.Handle("/settings/notifier/{method}", http.HandlerFunc(saveNotificationHandler)).Methods("POST") + r.Handle("/settings/notifier/{method}/test", http.HandlerFunc(testNotificationHandler)).Methods("POST") r.Handle("/settings/export", http.HandlerFunc(exportHandler)).Methods("GET") r.Handle("/plugins/download/{name}", http.HandlerFunc(pluginsDownloadHandler)) r.Handle("/plugins/{name}/save", http.HandlerFunc(pluginSavedHandler)).Methods("POST") diff --git a/handlers/settings.go b/handlers/settings.go index 1ef9d34f..bb993e76 100644 --- a/handlers/settings.go +++ b/handlers/settings.go @@ -196,3 +196,68 @@ func saveNotificationHandler(w http.ResponseWriter, r *http.Request) { notifier.OnSave(notifer.Method) executeResponse(w, r, "settings.html", core.CoreApp, "/settings") } + +func testNotificationHandler(w http.ResponseWriter, r *http.Request) { + var err error + if !IsAuthenticated(r) { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + form := parseForm(r) + vars := mux.Vars(r) + method := vars["method"] + enabled := form.Get("enable") + host := form.Get("host") + port := int(utils.StringInt(form.Get("port"))) + username := form.Get("username") + password := form.Get("password") + var1 := form.Get("var1") + var2 := form.Get("var2") + apiKey := form.Get("api_key") + apiSecret := form.Get("api_secret") + limits := int(utils.StringInt(form.Get("limits"))) + + notifer, notif, err := notifier.SelectNotifier(method) + if err != nil { + utils.Log(3, fmt.Sprintf("issue saving notifier %v: %v", method, err)) + executeResponse(w, r, "settings.html", core.CoreApp, "/settings") + return + } + + if host != "" { + notifer.Host = host + } + if port != 0 { + notifer.Port = port + } + if username != "" { + notifer.Username = username + } + if password != "" && password != "##########" { + notifer.Password = password + } + if var1 != "" { + notifer.Var1 = var1 + } + if var2 != "" { + notifer.Var2 = var2 + } + if apiKey != "" { + notifer.ApiKey = apiKey + } + if apiSecret != "" { + notifer.ApiSecret = apiSecret + } + if limits != 0 { + notifer.Limits = limits + } + notifer.Enabled = enabled == "on" + + ok, err := notif.(notifier.Tester).OnTest(*notifer) + + if ok { + w.Write([]byte("ok")) + } else { + w.Write([]byte(err.Error())) + } +} diff --git a/source/js/main.js b/source/js/main.js index 55d1a627..08a0133d 100644 --- a/source/js/main.js +++ b/source/js/main.js @@ -23,6 +23,23 @@ $('.service_li').on('click', function() { return false; }); +$('.test_notifier').on('click', function(e) { + var form = $(this).parents('form:first'); + var values = form.serialize(); + + console.log(form); + + $.ajax({ + url: form.attr("action")+"/test", + type: 'POST', + data: values, + success: function(data) { + alert(data); + } + }); + + e.preventDefault(); +}); $('form').submit(function() { console.log(this); diff --git a/source/tmpl/settings.html b/source/tmpl/settings.html index a76a3c60..af50fa2f 100644 --- a/source/tmpl/settings.html +++ b/source/tmpl/settings.html @@ -181,9 +181,8 @@ {{ range .Notifications }} {{$n := .Select}}