diff --git a/cmd/main.go b/cmd/main.go index 2c66ced..aa1c38f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -5,6 +5,8 @@ import ( "math/rand" "net/http" "net/url" + "regexp" + "strings" "time" "github.com/4rkal/shortr/models" @@ -50,6 +52,10 @@ func RedirectHandler(c echo.Context) error { return c.String(http.StatusNotFound, "Link not found") } + if !strings.Contains(link.Url, "://") { + link.Url = "http://" + link.Url + } + link.Clicks = link.Clicks + 1 return c.Redirect(http.StatusMovedPermanently, link.Url) @@ -102,8 +108,26 @@ func StatsSubmissionHandler(c echo.Context) error { } func isURL(s string) bool { - _, err := url.ParseRequestURI(s) - return err == nil + if !strings.Contains(s, "://") { + s = "http://" + s + } + + parsedUrl, err := url.ParseRequestURI(s) + if err != nil { + return false + } + + if parsedUrl.Host == "" { + return false + } + + domainRegex := `^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$` + matched, err := regexp.MatchString(domainRegex, parsedUrl.Host) + if err != nil || !matched { + return false + } + + return true } func generateRandomString(length int) string { diff --git a/views/index.templ b/views/index.templ index e569460..96a7500 100644 --- a/views/index.templ +++ b/views/index.templ @@ -7,7 +7,7 @@ templ Index(){

Blazingly fast URL shortener

Shorten URLs and track clicks

- +
diff --git a/views/index_templ.go b/views/index_templ.go index 5079a62..1b28cd6 100644 --- a/views/index_templ.go +++ b/views/index_templ.go @@ -41,7 +41,7 @@ func Index() templ.Component { }() } ctx = templ.InitializeContext(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Blazingly fast URL shortener

Shorten URLs and track clicks

") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Blazingly fast URL shortener

Shorten URLs and track clicks

") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }