From d8fe32d886256b9d2a1f60ddfaeb9f38718ce02f Mon Sep 17 00:00:00 2001 From: swirl Date: Thu, 12 Aug 2021 14:10:17 -0400 Subject: [PATCH] create func for link creation to shorten code also, fixed spelling error in index.html --- index.html | 2 +- main.go | 89 +++++++++++++++++++++--------------------------------- 2 files changed, 35 insertions(+), 56 deletions(-) diff --git a/index.html b/index.html index 121d9d7..78d0e6c 100644 --- a/index.html +++ b/index.html @@ -36,7 +36,7 @@ A Minimal, SQLite-Backed URL Shortener | $ curl -d https://duckduckgo.com {{.URL}}/ddg | {{.URL}}/ddg | -| 3. Create a short link to https://duckduckgo.com uaing a query string +| 3. Create a short link to https://duckduckgo.com using a query string | $ curl {{.URL}}?https://duckduckgo.com | {{.URL}}/1acd382417199d7e | diff --git a/main.go b/main.go index 2eb164b..5568f3e 100644 --- a/main.go +++ b/main.go @@ -166,6 +166,37 @@ func (c controller) Err(rw http.ResponseWriter, r *http.Request, err error) { fmt.Fprintf(rw, "%s", err) } +func (c controller) CreateShortLink(rw http.ResponseWriter, r *http.Request, rq string) { + u, err := url.Parse(rq) + if err != nil { + c.Err(rw, r, err) + return + } + if u.Scheme != "http" && u.Scheme != "https" { + rw.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(rw, "URL must contain scheme, e.g. `http://` or `https://`.") + return + } + var ( + link Link + h = strings.Trim(r.URL.Path, "/") + ) + if h != "" { + link, err = c.db.NewLinkWithShortLink(u, h) + + } else { + link, err = c.db.NewLink(u) + } + if err != nil { + c.Err(rw, r, err) + return + } + rw.Header().Set("X-Delete-With", link.Del) + rw.WriteHeader(http.StatusFound) + fmt.Fprintf(rw, "%s/%s", c.url, link.Smol) + return +} + func (c controller) ServeHTTP(rw http.ResponseWriter, r *http.Request) { switch r.Method { @@ -177,33 +208,7 @@ func (c controller) ServeHTTP(rw http.ResponseWriter, r *http.Request) { return } if rq != "" { - u, err := url.Parse(rq) - if err != nil { - c.Err(rw, r, err) - return - } - if u.Scheme != "http" && u.Scheme != "https" { - rw.WriteHeader(http.StatusBadRequest) - fmt.Fprintf(rw, "URL must contain scheme, e.g. `http://` or `https://`.") - return - } - var ( - link Link - h = strings.Trim(r.URL.Path, "/") - ) - if h != "" { - link, err = c.db.NewLinkWithShortLink(u, h) - - } else { - link, err = c.db.NewLink(u) - } - if err != nil { - c.Err(rw, r, err) - return - } - rw.Header().Set("X-Delete-With", link.Del) - rw.WriteHeader(http.StatusFound) - fmt.Fprintf(rw, "%s/%s", c.url, link.Smol) + c.CreateShortLink(rw, r, rq) return } else { switch st { @@ -242,34 +247,8 @@ func (c controller) ServeHTTP(rw http.ResponseWriter, r *http.Request) { c.Err(rw, r, err) return } - u, err := url.Parse(string(b)) - if err != nil { - c.Err(rw, r, err) - return - } - if u.Scheme != "http" && u.Scheme != "https" { - rw.WriteHeader(http.StatusBadRequest) - fmt.Fprintf(rw, "URL must contain scheme, e.g. `http://` or `https://`.") - return - } - var ( - link Link - h = strings.Trim(r.URL.Path, "/") - ) - if h != "" { - link, err = c.db.NewLinkWithShortLink(u, h) - - } else { - link, err = c.db.NewLink(u) - } - if err != nil { - c.Err(rw, r, err) - return - } - rw.Header().Set("X-Delete-With", link.Del) - rw.WriteHeader(http.StatusFound) - fmt.Fprintf(rw, "%s/%s", c.url, link.Smol) - return + c.CreateShortLink(rw, r, string(b)) + return case http.MethodDelete: b, err := ioutil.ReadAll(r.Body)