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

fixing error handling

parent bba0697a
No related branches found
No related tags found
No related merge requests found
...@@ -14,10 +14,11 @@ var StartTime = time.Now() // sets StartTime ...@@ -14,10 +14,11 @@ var StartTime = time.Now() // sets StartTime
func main() { func main() {
assignment2.StartTime = StartTime // sends StartTime assignment2.StartTime = StartTime // sends StartTime
assignment2.FBInit() err := assignment2.FBInit()
//assignment2.FBSave() if err != nil {
//assignment2.FBRead() fmt.Println("Error: ", err)
//assignment2.FBDelete() }
defer assignment2.FBClose() defer assignment2.FBClose()
port := os.Getenv("PORT") port := os.Getenv("PORT")
......
...@@ -15,7 +15,7 @@ import ( ...@@ -15,7 +15,7 @@ import (
var fb = FireBase{} var fb = FireBase{}
func FBInit() { func FBInit() error {
// Firebase initialisation // Firebase initialisation
fb.Ctx = context.Background() fb.Ctx = context.Background()
// We use a service account, load credentials file that you downloaded from your project's settings menu. // We use a service account, load credentials file that you downloaded from your project's settings menu.
...@@ -23,7 +23,9 @@ func FBInit() { ...@@ -23,7 +23,9 @@ func FBInit() {
sa := option.WithCredentialsFile("./cloud-assignment2-fc85f-firebase-adminsdk-14mld-d42cb80420.json") sa := option.WithCredentialsFile("./cloud-assignment2-fc85f-firebase-adminsdk-14mld-d42cb80420.json")
app, err := firebase.NewApp(fb.Ctx, nil, sa) app, err := firebase.NewApp(fb.Ctx, nil, sa)
if err != nil { if err != nil {
log.Fatalln(err) fmt.Printf("Error in FirebaseDatabase.Init() function: %v\n", err)
return errors.Wrap(err, "Error in FirebaseDatabase.Init()")
} }
fb.Client, err = app.Firestore(fb.Ctx) fb.Client, err = app.Firestore(fb.Ctx)
...@@ -35,25 +37,27 @@ func FBInit() { ...@@ -35,25 +37,27 @@ func FBInit() {
log.Fatalln(err) log.Fatalln(err)
} }
return nil
} }
func FBClose() { func FBClose() {
fb.Client.Close() fb.Client.Close()
} }
func FBSave() { func FBSave() error {
ref := fb.Client.Collection("Webhooks").NewDoc() ref := fb.Client.Collection("Webhooks").NewDoc()
webhook.ID = ref.ID webhook.ID = ref.ID
_, err := ref.Set(fb.Ctx, webhook) _, err := ref.Set(fb.Ctx, webhook)
if err != nil { if err != nil {
fmt.Println("ERROR saving webhook to Firestore DB: ", err) fmt.Println("ERROR saving webhook to Firestore DB: ", err)
//return errors.Wrap(err, "Error in FirebaseDatabase.Save()") return errors.Wrap(err, "Error in FirebaseDatabase.Save()")
} }
// return nil return nil
} }
func FBRead() { func FBRead() error {
iter := fb.Client.Collection("Webhooks").Documents(fb.Ctx) iter := fb.Client.Collection("Webhooks").Documents(fb.Ctx)
for { for {
doc, err := iter.Next() doc, err := iter.Next()
...@@ -64,7 +68,10 @@ func FBRead() { ...@@ -64,7 +68,10 @@ func FBRead() {
log.Fatalf("Failed to iterate: %v", err) log.Fatalf("Failed to iterate: %v", err)
} }
fmt.Println(doc.Data()) fmt.Println(doc.Data())
doc.DataTo(&webhook) // sets data in to webhook struct err = doc.DataTo(&webhook) // sets data in to webhook struct
if err != nil {
fmt.Println("Error when converting retrieved document to webhook struct: ", err)
}
tempCount := 0 // controll for duplicate tempCount := 0 // controll for duplicate
for i := range webhooks { // lopps true webhooks for i := range webhooks { // lopps true webhooks
if webhooks[i].ID == webhook.ID { // cheek it the webhook allready exist if webhooks[i].ID == webhook.ID { // cheek it the webhook allready exist
...@@ -77,25 +84,15 @@ func FBRead() { ...@@ -77,25 +84,15 @@ func FBRead() {
} }
} }
return nil
} }
func FBDelete(ID string) error { func FBDelete(ID string) error {
docRef := fb.Client.Collection("Webhooks").Doc(ID) docRef := fb.Client.Collection("Webhooks").Doc(ID)
_, err := docRef.Delete(fb.Ctx) _, err := docRef.Delete(fb.Ctx)
if err != nil { if err != nil {
fmt.Printf("ERROR deleting student (%v) from Firestore DB: %v\n", err) fmt.Printf("ERROR deleting webhook (%v) from Firestore DB: %v\n", err)
return errors.Wrap(err, "Error in FirebaseDatabase.Delete()") return errors.Wrap(err, "Error in FirebaseDatabase.Delete()")
} }
return nil return nil
} }
// Delete deletes a students from the DB.
// func (db *FirestoreDatabase) Delete(s *Student) error {
// docRef := db.Client.Collection(db.CollectionName).Doc(s.ID)
// _, err := docRef.Delete(db.Ctx)
// if err != nil {
// fmt.Printf("ERROR deleting student (%v) from Firestore DB: %v\n", s, err)
// return errors.Wrap(err, "Error in FirebaseDatabase.Delete()")
// }
// return nil
// }
package assignment2 package assignment2
import "testing"
func TestMockDatabase(t *testing.T) {
}
...@@ -31,14 +31,20 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) { ...@@ -31,14 +31,20 @@ func WebhookHandeler(w http.ResponseWriter, r *http.Request) {
webhook.Time = time.Now() // sets time stamp webhook.Time = time.Now() // sets time stamp
FBSave() // saves webhook to firebase 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 //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") fmt.Println("Webhooks " + webhook.URL + " has been regstrerd")
case http.MethodGet: case http.MethodGet:
FBRead() err := FBRead()
if err != nil {
fmt.Println("Error: ", err)
}
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
parts := strings.Split(r.URL.Path, "/") //finds url parts parts := strings.Split(r.URL.Path, "/") //finds url parts
fmt.Println(parts) fmt.Println(parts)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment