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
|
||||
| {{.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
|
||||
|
|
||||
|
|
55
main.go
55
main.go
|
@ -166,17 +166,7 @@ func (c controller) Err(rw http.ResponseWriter, r *http.Request, err error) {
|
|||
fmt.Fprintf(rw, "%s", err)
|
||||
}
|
||||
|
||||
func (c controller) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
|
||||
case http.MethodGet:
|
||||
st := strings.TrimRight(r.URL.Path, "/")
|
||||
rq, err := url.QueryUnescape(r.URL.RawQuery)
|
||||
if err != nil {
|
||||
c.Err(rw, r, err)
|
||||
return
|
||||
}
|
||||
if rq != "" {
|
||||
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)
|
||||
|
@ -205,6 +195,21 @@ func (c controller) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|||
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 {
|
||||
|
||||
case http.MethodGet:
|
||||
st := strings.TrimRight(r.URL.Path, "/")
|
||||
rq, err := url.QueryUnescape(r.URL.RawQuery)
|
||||
if err != nil {
|
||||
c.Err(rw, r, err)
|
||||
return
|
||||
}
|
||||
if rq != "" {
|
||||
c.CreateShortLink(rw, r, rq)
|
||||
return
|
||||
} else {
|
||||
switch st {
|
||||
|
||||
|
@ -242,33 +247,7 @@ 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)
|
||||
c.CreateShortLink(rw, r, string(b))
|
||||
return
|
||||
|
||||
case http.MethodDelete:
|
||||
|
|
Loading…
Reference in a new issue