diff --git a/assignment2/cmd/main.go b/assignment2/cmd/main.go index 8dd4ee727fadf24a70dac1c3f0d865a618d16efb..314a25a32084d341fa98afa628304b448e7d603a 100644 --- a/assignment2/cmd/main.go +++ b/assignment2/cmd/main.go @@ -31,6 +31,7 @@ func main() { http.HandleFunc("/repocheck/v1/languages", assignment2.HandlerLanguages) // runs handelr function http.HandleFunc("/repocheck/v1/status", assignment2.HandlerStatus) // runs handelr function http.HandleFunc("/repocheck/v1/webhooks", assignment2.WebhookHandeler) // runs handelr function + http.HandleFunc("/repocheck/v1/webhooks/", assignment2.WebhookHandeler2) // runs handelr function fmt.Println("Listening on port " + port) log.Fatal(http.ListenAndServe(":"+port, nil)) } diff --git a/assignment2/firebase.go b/assignment2/firebase.go index d67f4a2295824a0d858ea3c1a0b4ca056435e152..d501197076373d17e1e6892ce7d9d61b803152de 100644 --- a/assignment2/firebase.go +++ b/assignment2/firebase.go @@ -57,7 +57,9 @@ func FBSave() error { return nil } -func FBRead() error { +func FBRead() ([]WebhookRegistration, error) { + + var Temp []WebhookRegistration iter := fb.Client.Collection("Webhooks").Documents(fb.Ctx) for { doc, err := iter.Next() @@ -72,19 +74,24 @@ func FBRead() error { if err != nil { fmt.Println("Error when converting retrieved document to webhook struct: ", err) } - tempCount := 0 // controll for duplicate - for i := range webhooks { // lopps true webhooks - if webhooks[i].ID == webhook.ID { // cheek it the webhook allready exist - tempCount++ //adds 1 + + Temp = append(Temp, webhook) + + /* + tempCount := 0 // controll for duplicate + for i := range webhooks { // lopps true webhooks + if webhooks[i].ID == webhook.ID { // cheek it the webhook allready exist + tempCount++ //adds 1 + } } - } - if tempCount == 0 { // if tempCount is 0 it means that there is no duplicats and can add webhook - webhooks = append(webhooks, webhook) - } + if tempCount == 0 { // if tempCount is 0 it means that there is no duplicats and can add webhook + webhooks = append(webhooks, webhook) + } + */ } - return nil + return Temp, nil } func FBDelete(ID string) error { diff --git a/assignment2/go.sum b/assignment2/go.sum index c6f18ae2c5748ae9426d983a6a590b9ad6023643..d87e09c8ec2262513fd4507be955897da223992b 100644 --- a/assignment2/go.sum +++ b/assignment2/go.sum @@ -92,6 +92,7 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271 h1:N66aaryRB3Ax92gH0v3hp1QYZ3zWWCCUR/j8Ifh45Ss= +golang.org/x/net v0.0.0-20191101175033-0deb6923b6d9 h1:DPz9iiH3YoKiKhX/ijjoZvT0VFwK2c6CWYWQ7Zyr8TU= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0= diff --git a/assignment2/test.go b/assignment2/test.go index 484178b734c96afd616dc8defe4289eb81baee2c..dea37390873448860f22ba904568eec6764627a1 100644 --- a/assignment2/test.go +++ b/assignment2/test.go @@ -4,4 +4,23 @@ import "testing" func TestMockDatabase(t *testing.T) { + err := FBInit() + if err != nil { + t.Error(err) + } + + err = FBDelete("") + if err != nil { + t.Error(err) + } + + _, err = FBRead() + if err != nil { + t.Error(err) + } + + err = FBSave() + if err != nil { + t.Error(err) + } } diff --git a/assignment2/webhook.go b/assignment2/webhook.go index c680d1780a9c6dd332312eb2365821531b424cc6..a3eea235efc9826c9d525ab7ca4d4b6edeb376ad 100644 --- a/assignment2/webhook.go +++ b/assignment2/webhook.go @@ -11,7 +11,6 @@ import ( "time" ) -var webhooks []WebhookRegistration //Webhook DB var webhook = WebhookRegistration{} /* @@ -37,33 +36,20 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) { } //webhooks = append(webhooks, webhook) // saves webhook to webhooks ***************** look at this one - fmt.Fprintln(w, len(webhooks)-1) + //fmt.Fprintln(w, len(webhooks)-1) fmt.Println("Webhooks " + webhook.URL + " has been regstrerd") case http.MethodGet: - err := FBRead() + 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 - 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) - } + 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: @@ -84,11 +70,48 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) { } +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: