Browse Source

Migrated URL validation out of database saving

master
Christopher Ramey 5 years ago
committed by cdramey
parent
commit
9f13f8e09b
  1. 17
      load.go
  2. 19
      pages/submit.go
  3. 5
      storage/bolt/qurl.go

17
load.go

@ -42,29 +42,34 @@ func loadjson(stor storage.Storage, filename string) error {
max = e.ID max = e.ID
} }
qurl := &qurl.QURL{
q := &qurl.QURL{
ID: e.ID, ID: e.ID,
URL: e.URL, URL: e.URL,
} }
if e.Date.Date > 0 { 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 != "" { if e.IP != "" {
qurl.IP = net.ParseIP(e.IP)
q.IP = net.ParseIP(e.IP)
} }
if e.Browser != "" { if e.Browser != "" {
qurl.Browser = e.Browser
q.Browser = e.Browser
} }
err := stor.AddQURL(qurl)
err := q.CheckValid()
if err != nil { if err != nil {
fmt.Printf("\nError adding qurl: %s\n", err.Error())
fmt.Printf("\nValidation failure: %s\n", err.Error())
continue continue
} }
err = stor.AddQURL(q)
if err != nil {
return fmt.Errorf("AddQURL() Database error: %s", err.Error())
}
count++ count++
if (count % 100) == 0 { if (count % 100) == 0 {
fmt.Printf("*") fmt.Printf("*")

19
pages/submit.go

@ -54,15 +54,20 @@ func (ctx *SubmitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Created: time.Now(), Created: time.Now(),
} }
err := ctx.Storage.AddQURL(q)
err := q.CheckValid()
if err != nil { 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))
}
} }
} }

5
storage/bolt/qurl.go

@ -16,11 +16,6 @@ var (
) )
func (stor *BoltStorage) AddQURL(q *qurl.QURL) error { func (stor *BoltStorage) AddQURL(q *qurl.QURL) error {
err := q.CheckValid()
if err != nil {
return err
}
tx, err := stor.DB.Begin(true) tx, err := stor.DB.Begin(true)
if err != nil { if err != nil {
return err return err

Loading…
Cancel
Save