Migrated URL validation out of database saving

This commit is contained in:
Christopher Ramey 2018-11-17 17:07:33 +00:00 committed by cdramey
parent 5c669c8b93
commit 9f13f8e09b
3 changed files with 23 additions and 18 deletions

17
load.go
View File

@ -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("*")

View File

@ -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))
}
}
}

View File

@ -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