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

fixed invocation and some error

parent 48bfd9d9
Branches
No related tags found
No related merge requests found
......@@ -27,7 +27,7 @@ func HandlerCommits(w http.ResponseWriter, r *http.Request) {
C.Auth = true // sets auth to true
}
fmt.Println(URL3) // **************************************************
//fmt.Println(URL3) // **************************************************
resp, err := DoRequest(Client, w, URL3) //request the /prodjects link with 100 repositoris per page
if err != nil {
......@@ -37,9 +37,9 @@ func HandlerCommits(w http.ResponseWriter, r *http.Request) {
PageTot, err := strconv.Atoi(resp.Header.Get("X-Total-Pages")) //Get the total of pages that have repositoris
if err != nil { //check for error
log.Fatal(err)
http.Error(w, "Do not exist"+err.Error(), http.StatusBadRequest)
}
fmt.Println(PageTot)
//fmt.Println(PageTot)
for i := 1; i <= PageTot; i++ { // loop true all pages
URL4 := URL3 + "&page=" + strconv.Itoa(i) // adds page numer to link
......@@ -56,11 +56,11 @@ func HandlerCommits(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Println("str Temp: ", len(TempID)) // *****************************************************************
//fmt.Println("str Temp: ", len(TempID)) // *****************************************************************
I = append(I, TempID...) // adds temp to I
fmt.Println("str i: ", len(I)) // **********************************************
//fmt.Println("str i: ", len(I)) // **********************************************
var TempRep Commits // creates temp for commits
......@@ -78,7 +78,7 @@ func HandlerCommits(w http.ResponseWriter, r *http.Request) {
C.Repos = append(C.Repos, TempRep.Repos...) // adds temp to C.Repos, so that C.Repos gets a length
fmt.Println("str c: ", len(C.Repos)) // ********************************************************
//fmt.Println("str c: ", len(C.Repos)) // ********************************************************
}
......@@ -123,22 +123,6 @@ func HandlerCommits(w http.ResponseWriter, r *http.Request) {
// Webhooks**********************
Payload := &WebhooksInvocation{}
Payload.Event = "Commit"
Payload.Time = time.Now()
Payload.Params = "Limit = " + limit + " and Auth = " + auth
var webhooks []WebhookRegistration
webhooks, err = FBRead()
if err != nil {
fmt.Println("Error: ", err)
}
for i := range webhooks {
if webhooks[i].Event == Payload.Event {
CallUrl(webhooks[i].URL, Payload.Event+","+Payload.Params)
}
}
CallUrl("commits", "Limit = "+limit+" and Auth = "+auth, time.Now())
}
......@@ -69,7 +69,7 @@ func FBRead() ([]WebhookRegistration, error) {
if err != nil {
log.Fatalf("Failed to iterate: %v", err)
}
fmt.Println(doc.Data())
//fmt.Println(doc.Data())
err = doc.DataTo(&webhook) // sets data in to webhook struct
if err != nil {
fmt.Println("Error when converting retrieved document to webhook struct: ", err)
......
......@@ -56,7 +56,7 @@ func HandlerLanguages(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Println(PageTot) // ***************************************************************
//fmt.Println(PageTot) // ***************************************************************
for i := 1; i <= PageTot; i++ { // loops true total pages
URL := URL3 + "&page=" + strconv.Itoa(i) // sets url to one page
......@@ -77,7 +77,7 @@ func HandlerLanguages(w http.ResponseWriter, r *http.Request) {
I = append(I, TempID...) // adds test to I
fmt.Println("len: ", len(I))
//fmt.Println("len: ", len(I))
}
......@@ -111,8 +111,8 @@ func HandlerLanguages(w http.ResponseWriter, r *http.Request) {
var Lang []TestLang // making a temp struckt
CodeTypes := Counter(Code) // make a map[string]int that have codelanguag counted
fmt.Println("test: ", CodeTypes) //*********************************************
CodeTypes := Counter(Code) // make a map[string]int that have codelanguag counted
//fmt.Println("test: ", CodeTypes) //*********************************************
for k, v := range CodeTypes { // loops over map
Lang = append(Lang, TestLang{k, v}) // sets map values in to struct
......@@ -120,7 +120,7 @@ func HandlerLanguages(w http.ResponseWriter, r *http.Request) {
sort.Slice(Lang, func(i, j int) bool { return Lang[i].Count > Lang[j].Count }) //sort struct from highest to lovest int
fmt.Println(Lang) //****************************************************************
//fmt.Println(Lang) //****************************************************************
for i := range Lang { // loops over struct
L.Languages = append(L.Languages, Lang[i].Lang) // sets string from struct inn to L.Languages
......@@ -140,22 +140,6 @@ func HandlerLanguages(w http.ResponseWriter, r *http.Request) {
// Webhooks**********************
Payload := &WebhooksInvocation{}
Payload.Event = "Language"
Payload.Time = time.Now()
Payload.Params = "Limit = " + limit + " and Auth = " + auth
var webhooks []WebhookRegistration
webhooks, err = FBRead()
if err != nil {
fmt.Println("Error: ", err)
}
for i := range webhooks {
if webhooks[i].Event == Payload.Event {
CallUrl(webhooks[i].URL, Payload.Event+","+Payload.Params)
}
}
CallUrl("languages", "Limit = "+limit+" and Auth = "+auth, time.Now())
}
......@@ -17,7 +17,7 @@ func HandlerStatus(w http.ResponseWriter, r *http.Request) {
S.Gitlab = resp.StatusCode
resp, err = http.Get("https://git.gvk.idi.ntnu.no") //må endre link
resp, err = http.Get("https://firebase.google.com") //må endre link
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
......
......@@ -69,9 +69,9 @@ type WebhookRegistration struct {
// }
type WebhooksInvocation struct {
Event string `json:"event"`
Params string `json:"params"`
Time time.Time `json:"time"`
Event string `json:"event"`
Params string `json:"params"`
Time string `json:"time"`
}
//************ Firebase ***********
......
......@@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
......@@ -28,15 +27,14 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
}
webhook.Event = strings.ToLower(webhook.Event)
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:
......@@ -101,41 +99,42 @@ func WebhookHandeler2(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) {
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)))
func CallUrl(event string, params string, timestamp time.Time) {
var webhooks []WebhookRegistration
webhooks, err := FBRead()
if err != nil {
fmt.Println("Error in HTTP request: " + err.Error())
fmt.Println("Error: ", err)
}
response, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println("Something is wrong with invocation response: " + err.Error())
}
for i := range webhooks {
if webhooks[i].Event == event {
var request = WebhooksInvocation{Event: event, Params: params, Time: timestamp.String()}
requestBody, err := json.Marshal(request)
if err != nil {
fmt.Println("Can not encode: " + err.Error())
}
fmt.Println("Attempting invoation of URL " + webhooks[i].URL + "...")
resp, err := http.Post(webhooks[i].URL, "json", bytes.NewReader([]byte(requestBody)))
if err != nil {
fmt.Println("Error in HTTP request: " + err.Error())
}
fmt.Println("Webhook invoked. Received status code " + strconv.Itoa(res.StatusCode) +
" and body: " + string(response))
response, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Something vent wrong: " + err.Error())
}
fmt.Println("Webhook body: " + string(response))
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment