Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: fix donottouch branch #24

Merged
merged 9 commits into from
Sep 14, 2024
17 changes: 14 additions & 3 deletions handlers/shawty.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,27 @@ func Shawty(w http.ResponseWriter, r *http.Request) {
longUrl = "https://" + longUrl
}

errorTempl := template.Must(template.ParseFiles("./partial-html/short-link-error.html"))

// Parse the URL to validate it and check its scheme
parsedUrl, err := url.Parse(longUrl)
if err != nil || parsedUrl.Scheme != "https" {
w.Write([]byte("Invalid URL. Only HTTPS schema is allowed"))
asserts.NoErr(errorTempl.Execute(w, "Invalid URL. Only HTTPS schema is allowed"), "Failed to execute template short-link-error.html")
return
}

// Check if URL is valid by checking if it contains `.`
// Check if URL contains a TLD (Top-Level Domain)
if !strings.Contains(longUrl, ".") {
w.Write([]byte("Enter a valid URL"))
asserts.NoErr(errorTempl.Execute(w, "The URL doesn't contain TLD (Top-Level Domain)"), "Failed to execute template short-link-error.html")
return
} else if split := strings.SplitN(longUrl, ".", 2); split[1] == "" {
asserts.NoErr(errorTempl.Execute(w, "The URL doesn't contain TLD (Top-Level Domain)"), "Failed to execute template short-link-error.html")
return
}

// Check the lenght of the URL
if len(longUrl) > 1000 {
asserts.NoErr(errorTempl.Execute(w, "The URL is too long, Max URL lenght is 1000 characters"), "Failed to execute template short-link-error.html")
return
}

Expand Down
3 changes: 3 additions & 0 deletions partial-html/short-link-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="text-lg font-mono mb-2 mt-10 text-center">
<p class="text-red-400">{{ . }}</p>
</div>
2 changes: 1 addition & 1 deletion static/dist.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.