Select Git revision
-
Hans Kristian Hoel authoredHans Kristian Hoel authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
webhook.go 3.04 KiB
package assignment2
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
var webhooks []WebhookRegistration //Webhook DB
var webhook = WebhookRegistration{}
/*
Handles webhook registration (POST) and lookup (GET) requests.
Expects WebhookRegistration struct body in request.
*/
func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
err := json.NewDecoder(r.Body).Decode(&webhook) //decode to webhook
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
webhook.Time = time.Now() // sets time stamp
FBSave() // saves webhook to firebase
//webhooks = append(webhooks, webhook) // saves webhook to webhooks ***************** look at this one
fmt.Fprintln(w, len(webhooks)-1)
fmt.Println("Webhooks " + webhook.URL + " has been regstrerd")
case http.MethodGet:
FBRead()
http.Header.Add(w.Header(), "Content-Type", "application/json") // makes the print look good
parts := strings.Split(r.URL.Path, "/") //finds url parts
fmt.Println(parts)
if len(parts) == 5 { // this need to be fixed
tempInt, err := strconv.Atoi(parts[4]) // gets the numid of choosen webhook
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
err = json.NewEncoder(w).Encode(webhooks[tempInt]) // encode choosen webhook
if err != nil {
http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
}
} else { // if not choosen any webhooks
err := json.NewEncoder(w).Encode(webhooks) // encode all webhooks
if err != nil {
http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
}
}
case http.MethodDelete:
err := json.NewDecoder(r.Body).Decode(&webhook) //decode to webhook
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
err = FBDelete(webhook.ID)
if err != nil {
fmt.Println("Error: ", err)
}
default:
http.Error(w, "Method is invalid "+r.Method, http.StatusBadRequest)
}
}
/*
Invokes the web service to trigger event. Currently only responds to POST requests.
*/
func ServiceHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
fmt.Println("Recive POST request")
for _, v := range webhooks {
go CallUrl(v.URL, "Resonse on registered event in webhook demo: "+v.Event)
}
default:
http.Error(w, "Method is invalid "+r.Method, http.StatusBadRequest)
}
}
/*
Calls given URL with given content and awaits response (status and body).
*/
func CallUrl(url string, content string) {
fmt.Println("Attempting invocation of url " + url + "...")
res, err := http.Post(url, "string", bytes.NewReader([]byte(content)))
if err != nil {
fmt.Println("Error in HTTP request: " + err.Error())
}
response, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println("Something is wrong with invocation response: " + err.Error())
}
fmt.Println("Webhook invoked. Received status code " + strconv.Itoa(res.StatusCode) +
" and body: " + string(response))
}