Skip to content

Commit

Permalink
add 404 and 403 pages
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-streltsov committed Oct 4, 2024
1 parent 8f804c7 commit 336f935
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
53 changes: 40 additions & 13 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,44 @@ func (h *Handler) Routes() http.Handler {
mux.HandleFunc("/delete/", h.deleteURLHandler)
mux.HandleFunc("/details/", h.urlDetailsHandler)

mux.HandleFunc("/404", h.notFoundHandler)
mux.HandleFunc("/403", h.forbiddenHandler)

rl := middleware.NewRateLimiter(100, time.Minute)
return middleware.LoggingMiddleware(middleware.RateLimitingMiddleware(rl)(mux))
}

func (h *Handler) notFoundHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
tmpl, err := template.ParseFiles("internal/templates/base.html", "internal/templates/404.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.ExecuteTemplate(w, "base.html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

func (h *Handler) forbiddenHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
tmpl, err := template.ParseFiles("internal/templates/base.html", "internal/templates/403.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = tmpl.ExecuteTemplate(w, "base.html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

func (h *Handler) indexHandler(w http.ResponseWriter, r *http.Request) {
// TODO: create a 404 page, etc
if r.URL.Path != "/" {
http.NotFound(w, r)
h.notFoundHandler(w, r)
return
}

Expand Down Expand Up @@ -206,16 +236,15 @@ func (h *Handler) newURLHandler(w http.ResponseWriter, r *http.Request) {
}

func (h *Handler) redirectHandler(w http.ResponseWriter, r *http.Request) {
// TODO: add flashes
key := strings.TrimPrefix(r.URL.Path, "/r/")
if key == "" {
http.Error(w, "Key is required", http.StatusBadRequest)
h.notFoundHandler(w, r)
return
}

url, err := h.db.GetURL(key)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
h.notFoundHandler(w, r)
return
}

Expand Down Expand Up @@ -253,7 +282,7 @@ func (h *Handler) redirectHandler(w http.ResponseWriter, r *http.Request) {
}

if !isSafe {
http.Error(w, "The requested URL is not safe", http.StatusForbidden)
h.forbiddenHandler(w, r)
return
}

Expand Down Expand Up @@ -478,9 +507,7 @@ func (h *Handler) editURLHandler(w http.ResponseWriter, r *http.Request) {
}

if url.UserID != user.ID {
session.AddFlash("Unauthorized access", "error")
session.Save(r, w)
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
h.forbiddenHandler(w, r)
return
}

Expand Down Expand Up @@ -591,12 +618,12 @@ func (h *Handler) deleteURLHandler(w http.ResponseWriter, r *http.Request) {

url, err := h.db.GetURLByID(urlID)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
h.notFoundHandler(w, r)
return
}

if url.UserID != user.ID {
http.Error(w, "Unauthorized", http.StatusForbidden)
h.forbiddenHandler(w, r)
return
}

Expand Down Expand Up @@ -625,12 +652,12 @@ func (h *Handler) urlDetailsHandler(w http.ResponseWriter, r *http.Request) {

url, err := h.db.GetURLByID(urlID)
if err != nil {
http.Error(w, "URL not found", http.StatusNotFound)
h.notFoundHandler(w, r)
return
}

if url.UserID != user.ID {
http.Error(w, "Unauthorized", http.StatusForbidden)
h.forbiddenHandler(w, r)
return
}

Expand Down
20 changes: 20 additions & 0 deletions internal/templates/403.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{ template "base.html" . }}

{{ define "title" }}403 Forbidden{{ end }}

{{ define "content" }}
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-body text-center">
<h1 class="card-title display-1">403</h1>
<h2 class="card-subtitle mb-4">Forbidden</h2>
<p class="card-text">You don't have permission to access this resource.</p>
<a href="/" class="btn btn-primary">Go Home</a>
</div>
</div>
</div>
</div>
</div>
{{ end }}
20 changes: 20 additions & 0 deletions internal/templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{ template "base.html" . }}

{{ define "title" }}404 Not Found{{ end }}

{{ define "content" }}
<div class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-body text-center">
<h1 class="card-title display-1">404</h1>
<h2 class="card-subtitle mb-4">Page Not Found</h2>
<p class="card-text">The page you are looking for doesn't exist or has been moved.</p>
<a href="/" class="btn btn-primary">Go Home</a>
</div>
</div>
</div>
</div>
</div>
{{ end }}

0 comments on commit 336f935

Please sign in to comment.