Skip to content
Snippets Groups Projects
Commit 857667d0 authored by Hans Kristian Hoel's avatar Hans Kristian Hoel
Browse files

traing to make webhooks work

parent 385b0af1
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"sort" "sort"
"strconv" "strconv"
"time"
) )
func HandlerCommits(w http.ResponseWriter, r *http.Request) { func HandlerCommits(w http.ResponseWriter, r *http.Request) {
...@@ -119,8 +120,12 @@ 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 http.Header.Add(w.Header(), "Content-Type", "application/json") // makes the print look good
// Webhooks**********************
Payload := &WebhooksInvocation{} Payload := &WebhooksInvocation{}
Payload.Event = "Commits" Payload.Event = "Commits"
Payload.Time = time.Now()
Payload.Params = "Limit = " + limit + "and Auth = " + auth
json.NewEncoder(w).Encode(C) // encode C json.NewEncoder(w).Encode(C) // encode C
......
module assignment2 module assignment2
go 1.13 go 1.13
// +heroku goVersion go1.13
...@@ -56,15 +56,15 @@ type WebhookRegistration struct { ...@@ -56,15 +56,15 @@ type WebhookRegistration struct {
Time time.Time `json:"time"` Time time.Time `json:"time"`
} }
type Webhooks struct { // type Webhooks struct {
ID int `json:"id"` // ID int `json:"id"`
Event string `json:"Event"` // Event string `json:"Event"`
URL string `json:"url"` // URL string `json:"url"`
Time float64 `json:"time"` // Time float64 `json:"time"`
} // }
type WebhooksInvocation struct { type WebhooksInvocation struct {
Event string `json:"event"` Event string `json:"event"`
Params []string `json:"params"` Params string `json:"params"`
Time float64 `json:"time"` Time time.Time `json:"time"`
} }
...@@ -13,6 +13,11 @@ import ( ...@@ -13,6 +13,11 @@ import (
var webhooks []WebhookRegistration //Webhook DB var webhooks []WebhookRegistration //Webhook DB
var IDNum = 1 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) { func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
...@@ -44,6 +49,10 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) { ...@@ -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) { func ServiceHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
...@@ -57,6 +66,10 @@ func ServiceHandler(w http.ResponseWriter, r *http.Request) { ...@@ -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) { func CallUrl(url string, content string) {
fmt.Println("Attempting invocation of url " + url + "...") fmt.Println("Attempting invocation of url " + url + "...")
res, err := http.Post(url, "string", bytes.NewReader([]byte(content))) res, err := http.Post(url, "string", bytes.NewReader([]byte(content)))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment