github.com/bluestoneag/bluephish@v0.1.0/controllers/api/webhook.go (about) 1 package api 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "strconv" 7 8 log "github.com/bluestoneag/bluephish/logger" 9 "github.com/bluestoneag/bluephish/models" 10 "github.com/bluestoneag/bluephish/webhook" 11 "github.com/gorilla/mux" 12 ) 13 14 // Webhooks returns a list of webhooks, both active and disabled 15 func (as *Server) Webhooks(w http.ResponseWriter, r *http.Request) { 16 switch { 17 case r.Method == "GET": 18 whs, err := models.GetWebhooks() 19 if err != nil { 20 log.Error(err) 21 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError) 22 return 23 } 24 JSONResponse(w, whs, http.StatusOK) 25 26 case r.Method == "POST": 27 wh := models.Webhook{} 28 err := json.NewDecoder(r.Body).Decode(&wh) 29 if err != nil { 30 JSONResponse(w, models.Response{Success: false, Message: "Invalid JSON structure"}, http.StatusBadRequest) 31 return 32 } 33 err = models.PostWebhook(&wh) 34 if err != nil { 35 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) 36 return 37 } 38 JSONResponse(w, wh, http.StatusCreated) 39 } 40 } 41 42 // Webhook returns details of a single webhook specified by "id" parameter 43 func (as *Server) Webhook(w http.ResponseWriter, r *http.Request) { 44 vars := mux.Vars(r) 45 id, _ := strconv.ParseInt(vars["id"], 0, 64) 46 wh, err := models.GetWebhook(id) 47 if err != nil { 48 JSONResponse(w, models.Response{Success: false, Message: "Webhook not found"}, http.StatusNotFound) 49 return 50 } 51 switch { 52 case r.Method == "GET": 53 JSONResponse(w, wh, http.StatusOK) 54 55 case r.Method == "DELETE": 56 err = models.DeleteWebhook(id) 57 if err != nil { 58 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError) 59 return 60 } 61 log.Infof("Deleted webhook with id: %d", id) 62 JSONResponse(w, models.Response{Success: true, Message: "Webhook deleted Successfully!"}, http.StatusOK) 63 64 case r.Method == "PUT": 65 wh = models.Webhook{} 66 err = json.NewDecoder(r.Body).Decode(&wh) 67 if err != nil { 68 log.Errorf("error decoding webhook: %v", err) 69 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) 70 return 71 } 72 wh.Id = id 73 err = models.PutWebhook(&wh) 74 if err != nil { 75 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) 76 return 77 } 78 JSONResponse(w, wh, http.StatusOK) 79 } 80 } 81 82 // ValidateWebhook makes an HTTP request to a specified remote url to ensure that it's valid. 83 func (as *Server) ValidateWebhook(w http.ResponseWriter, r *http.Request) { 84 type validationEvent struct { 85 Success bool `json:"success"` 86 } 87 switch { 88 case r.Method == "POST": 89 vars := mux.Vars(r) 90 id, _ := strconv.ParseInt(vars["id"], 0, 64) 91 wh, err := models.GetWebhook(id) 92 if err != nil { 93 log.Error(err) 94 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError) 95 return 96 } 97 payload := validationEvent{Success: true} 98 err = webhook.Send(webhook.EndPoint{URL: wh.URL, Secret: wh.Secret}, payload) 99 if err != nil { 100 JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest) 101 return 102 } 103 JSONResponse(w, wh, http.StatusOK) 104 } 105 }