Skip to content
Snippets Groups Projects
Select Git revision
  • e3a8f0463978a3186c5e7ba01a966b5b17397b40
  • master default protected
2 results

webhook.go

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    webhook.go 3.56 KiB
    package assignment2
    
    import (
    	"bytes"
    	"encoding/json"
    	"fmt"
    	"io/ioutil"
    	"net/http"
    	"strconv"
    	"strings"
    	"time"
    )
    
    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
    
    		err = FBSave() // saves webhook to firebase
    		if err != nil {
    			fmt.Println("Error: ", err)
    		}
    		//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:
    		var webhooks []WebhookRegistration //Webhook DB
    		webhooks, err := FBRead()
    		if err != nil {
    			fmt.Println("Error: ", err)
    		}
    		http.Header.Add(w.Header(), "Content-Type", "application/json") // makes the print look good
    
    		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)
    	}
    
    }
    
    func WebhookHandeler2(w http.ResponseWriter, r *http.Request) {
    	switch r.Method {
    
    	case http.MethodGet:
    		var webhooks []WebhookRegistration //Webhook DB
    		webhooks, err := FBRead()
    		if err != nil {
    			fmt.Println("Error: ", err)
    		}
    		http.Header.Add(w.Header(), "Content-Type", "application/json")
    
    		parts := strings.Split(r.URL.Path, "/") //finds url parts
    		fmt.Println(parts)
    
    		// tempInt, err := strconv.Atoi(parts[4]) // gets the numid of choosen webhook
    		// if err != nil {
    		// 	http.Error(w, err.Error(), http.StatusBadRequest)
    		// }
    
    		for i := range webhooks {
    			if webhooks[i].ID == parts[4] {
    				err = json.NewEncoder(w).Encode(webhooks[i]) // encode choosen webhook
    				if err != nil {
    					http.Error(w, "Some thing went wrong"+err.Error(), http.StatusInternalServerError)
    				}
    
    			}
    		}
    
    	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) {
    	var webhooks []WebhookRegistration //Webhook DB
    
    	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))
    
    }