From 857667d08325092b08748baafaaaa272975090cb Mon Sep 17 00:00:00 2001
From: Hans Kristian Hoel <hanskhoe@stud.ntnu.no>
Date: Thu, 31 Oct 2019 15:57:46 +0100
Subject: [PATCH] traing to make webhooks work

---
 assignment2/commit.go  |  5 +++++
 assignment2/go.mod     |  2 --
 assignment2/structs.go | 18 +++++++++---------
 assignment2/webhook.go | 13 +++++++++++++
 4 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/assignment2/commit.go b/assignment2/commit.go
index 2fc9bd3..82e7b22 100644
--- a/assignment2/commit.go
+++ b/assignment2/commit.go
@@ -7,6 +7,7 @@ import (
 	"net/http"
 	"sort"
 	"strconv"
+	"time"
 )
 
 func HandlerCommits(w http.ResponseWriter, r *http.Request) {
@@ -119,8 +120,12 @@ func HandlerCommits(w http.ResponseWriter, r *http.Request) {
 
 	http.Header.Add(w.Header(), "Content-Type", "application/json") // makes the print look good
 
+	// Webhooks**********************
+
 	Payload := &WebhooksInvocation{}
 	Payload.Event = "Commits"
+	Payload.Time = time.Now()
+	Payload.Params = "Limit = " + limit + "and Auth = " + auth
 
 	json.NewEncoder(w).Encode(C) // encode C
 
diff --git a/assignment2/go.mod b/assignment2/go.mod
index 4f590dc..f87a213 100644
--- a/assignment2/go.mod
+++ b/assignment2/go.mod
@@ -1,5 +1,3 @@
 module assignment2
 
 go 1.13
-
-// +heroku goVersion go1.13
diff --git a/assignment2/structs.go b/assignment2/structs.go
index 290d5a2..a81c5f4 100644
--- a/assignment2/structs.go
+++ b/assignment2/structs.go
@@ -56,15 +56,15 @@ type WebhookRegistration struct {
 	Time  time.Time `json:"time"`
 }
 
-type Webhooks struct {
-	ID    int     `json:"id"`
-	Event string  `json:"Event"`
-	URL   string  `json:"url"`
-	Time  float64 `json:"time"`
-}
+// type Webhooks struct {
+// 	ID    int     `json:"id"`
+// 	Event string  `json:"Event"`
+// 	URL   string  `json:"url"`
+// 	Time  float64 `json:"time"`
+// }
 
 type WebhooksInvocation struct {
-	Event  string   `json:"event"`
-	Params []string `json:"params"`
-	Time   float64  `json:"time"`
+	Event  string    `json:"event"`
+	Params string    `json:"params"`
+	Time   time.Time `json:"time"`
 }
diff --git a/assignment2/webhook.go b/assignment2/webhook.go
index 1ca2327..22f04bd 100644
--- a/assignment2/webhook.go
+++ b/assignment2/webhook.go
@@ -13,6 +13,11 @@ import (
 var webhooks []WebhookRegistration //Webhook DB
 var IDNum = 1
 
+/*
+	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 {
@@ -44,6 +49,10 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
 
 }
 
+/*
+	Invokes the web service to trigger event. Currently only responds to POST requests.
+*/
+
 func ServiceHandler(w http.ResponseWriter, r *http.Request) {
 
 	switch r.Method {
@@ -57,6 +66,10 @@ func ServiceHandler(w http.ResponseWriter, r *http.Request) {
 	}
 }
 
+/*
+	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)))
-- 
GitLab