From 9f13f8e09babd310d8bbc1ce6a12b8929eb5010e Mon Sep 17 00:00:00 2001 From: cdramey Date: Sat, 17 Nov 2018 17:07:33 +0000 Subject: [PATCH] Migrated URL validation out of database saving --- load.go | 17 +++++++++++------ pages/submit.go | 19 ++++++++++++------- storage/bolt/qurl.go | 5 ----- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/load.go b/load.go index f1c7134..3ad1ddf 100644 --- a/load.go +++ b/load.go @@ -42,29 +42,34 @@ func loadjson(stor storage.Storage, filename string) error { max = e.ID } - qurl := &qurl.QURL{ + q := &qurl.QURL{ ID: e.ID, URL: e.URL, } if e.Date.Date > 0 { - qurl.Created = time.Unix((e.Date.Date / 1000), 0) + q.Created = time.Unix((e.Date.Date / 1000), 0) } if e.IP != "" { - qurl.IP = net.ParseIP(e.IP) + q.IP = net.ParseIP(e.IP) } if e.Browser != "" { - qurl.Browser = e.Browser + q.Browser = e.Browser } - err := stor.AddQURL(qurl) + err := q.CheckValid() if err != nil { - fmt.Printf("\nError adding qurl: %s\n", err.Error()) + fmt.Printf("\nValidation failure: %s\n", err.Error()) continue } + err = stor.AddQURL(q) + if err != nil { + return fmt.Errorf("AddQURL() Database error: %s", err.Error()) + } + count++ if (count % 100) == 0 { fmt.Printf("*") diff --git a/pages/submit.go b/pages/submit.go index 3467747..61359e8 100644 --- a/pages/submit.go +++ b/pages/submit.go @@ -54,15 +54,20 @@ func (ctx *SubmitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { Created: time.Now(), } - err := ctx.Storage.AddQURL(q) + err := q.CheckValid() if err != nil { - http.Error(w, fmt.Sprintf("Database error: %s", err.Error()), - http.StatusInternalServerError) - return - } + pg.Message = err.Error() + } else { + err := ctx.Storage.AddQURL(q) + if err != nil { + http.Error(w, fmt.Sprintf("Database error: %s", err.Error()), + http.StatusInternalServerError) + return + } - pg.Message = "URL Added." - pg.URL = fmt.Sprintf("https://qurl.org/%s", qurl.ToString(q.ID)) + pg.Message = "URL Added." + pg.URL = fmt.Sprintf("https://qurl.org/%s", qurl.ToString(q.ID)) + } } } diff --git a/storage/bolt/qurl.go b/storage/bolt/qurl.go index 1134bca..08ed84c 100644 --- a/storage/bolt/qurl.go +++ b/storage/bolt/qurl.go @@ -16,11 +16,6 @@ var ( ) func (stor *BoltStorage) AddQURL(q *qurl.QURL) error { - err := q.CheckValid() - if err != nil { - return err - } - tx, err := stor.DB.Begin(true) if err != nil { return err