mirror of
https://git.swurl.xyz/swirl/link.git
synced 2023-11-08 10:44:52 +02:00
create func for link creation to shorten code
also, fixed spelling error in index.html
This commit is contained in:
parent
994a47b02e
commit
d8fe32d886
2 changed files with 35 additions and 56 deletions
|
@ -36,7 +36,7 @@ A Minimal, SQLite-Backed URL Shortener
|
||||||
| $ curl -d https://duckduckgo.com {{.URL}}/ddg
|
| $ curl -d https://duckduckgo.com {{.URL}}/ddg
|
||||||
| {{.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
|
| $ curl {{.URL}}?https://duckduckgo.com
|
||||||
| {{.URL}}/1acd382417199d7e
|
| {{.URL}}/1acd382417199d7e
|
||||||
|
|
|
|
||||||
|
|
89
main.go
89
main.go
|
@ -166,6 +166,37 @@ func (c controller) Err(rw http.ResponseWriter, r *http.Request, err error) {
|
||||||
fmt.Fprintf(rw, "%s", err)
|
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) {
|
func (c controller) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
|
|
||||||
|
@ -177,33 +208,7 @@ func (c controller) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if rq != "" {
|
if rq != "" {
|
||||||
u, err := url.Parse(rq)
|
c.CreateShortLink(rw, r, 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
|
return
|
||||||
} else {
|
} else {
|
||||||
switch st {
|
switch st {
|
||||||
|
@ -242,34 +247,8 @@ func (c controller) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
c.Err(rw, r, err)
|
c.Err(rw, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
u, err := url.Parse(string(b))
|
c.CreateShortLink(rw, r, string(b))
|
||||||
if err != nil {
|
return
|
||||||
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
|
|
||||||
|
|
||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
b, err := ioutil.ReadAll(r.Body)
|
b, err := ioutil.ReadAll(r.Body)
|
||||||
|
|
Loading…
Reference in a new issue