2020-01-03 06:10:25 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2020-02-01 03:53:00 +00:00
|
|
|
"encoding/json"
|
2020-01-03 06:10:25 +00:00
|
|
|
"github.com/gorilla/mux"
|
2020-01-03 07:57:42 +00:00
|
|
|
"github.com/hunterlong/statping/core/integrations"
|
2020-02-01 03:53:00 +00:00
|
|
|
"github.com/hunterlong/statping/types"
|
2020-01-03 06:10:25 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func apiAllIntegrationsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
integrations := integrations.Integrations
|
|
|
|
returnJson(integrations, w, r)
|
|
|
|
}
|
|
|
|
|
2020-02-01 03:53:00 +00:00
|
|
|
func apiIntegrationViewHandler(w http.ResponseWriter, r *http.Request) {
|
2020-01-03 06:10:25 +00:00
|
|
|
vars := mux.Vars(r)
|
2020-02-01 03:53:00 +00:00
|
|
|
intgr, err := integrations.Find(vars["name"])
|
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
2020-01-03 06:10:25 +00:00
|
|
|
}
|
2020-02-01 03:53:00 +00:00
|
|
|
returnJson(intgr.Get(), w, r)
|
|
|
|
}
|
2020-01-03 06:10:25 +00:00
|
|
|
|
2020-02-01 03:53:00 +00:00
|
|
|
func apiIntegrationHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
intgr, err := integrations.Find(vars["name"])
|
2020-01-03 06:10:25 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
2020-02-01 03:53:00 +00:00
|
|
|
|
|
|
|
var intJson *types.Integration
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
if err := decoder.Decode(&intJson); err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
integration := intgr.Get()
|
|
|
|
integration.Enabled = intJson.Enabled
|
|
|
|
integration.Fields = intJson.Fields
|
|
|
|
|
|
|
|
if err := integrations.Update(integration); err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := intgr.List()
|
2020-01-03 06:10:25 +00:00
|
|
|
if err != nil {
|
|
|
|
sendErrorJson(err, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returnJson(list, w, r)
|
|
|
|
}
|