Skip to content

Commit

Permalink
Organize route handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
fossyy committed Sep 24, 2024
1 parent 757dde5 commit db566fd
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 346 deletions.
28 changes: 0 additions & 28 deletions handler/error/error.go

This file was deleted.

152 changes: 83 additions & 69 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"fmt"
"github.com/fossyy/filekeeper/app"
errorView "github.com/fossyy/filekeeper/view/client/error"
"net/http"
"strings"

errorHandler "github.com/fossyy/filekeeper/handler/error"
"github.com/fossyy/filekeeper/session"
"github.com/fossyy/filekeeper/utils"
)
Expand All @@ -24,14 +24,24 @@ func (w *wrapper) WriteHeader(code int) {
if code == http.StatusNotFound {
w.Header().Set("Content-Type", "text/html")
w.ResponseWriter.WriteHeader(code)
errorHandler.NotFound(w.ResponseWriter, w.request)
component := errorView.NotFound("Not Found")
err := component.Render(w.request.Context(), w)
if err != nil {
app.Server.Logger.Error(err.Error())
return
}
return
}

if code == http.StatusInternalServerError {
w.Header().Set("Content-Type", "text/html")
w.ResponseWriter.WriteHeader(code)
errorHandler.InternalServerError(w.ResponseWriter, w.request)
component := errorView.InternalServerError("Internal Server Error")
err := component.Render(w.request.Context(), w)
if err != nil {
app.Server.Logger.Error(err.Error())
return
}
return
}
w.ResponseWriter.WriteHeader(code)
Expand Down Expand Up @@ -80,78 +90,82 @@ func Handler(next http.Handler) http.Handler {
})
}

func Auth(next http.HandlerFunc, w http.ResponseWriter, r *http.Request) {
status, user, sessionID := session.GetSession(r)
func Auth(next http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status, user, sessionID := session.GetSession(r)

switch status {
case session.Authorized:
ctx := context.WithValue(r.Context(), "user", user)
ctx = context.WithValue(ctx, "sessionID", sessionID)
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
return
case session.Unauthorized:
if r.RequestURI != "/logout" {
switch status {
case session.Authorized:
ctx := context.WithValue(r.Context(), "user", user)
ctx = context.WithValue(ctx, "sessionID", sessionID)
req := r.WithContext(ctx)
next.ServeHTTP(w, req)
return
case session.Unauthorized:
if r.RequestURI != "/logout" {
http.SetCookie(w, &http.Cookie{
Name: "redirect",
Value: r.RequestURI,
Path: "/",
})
}
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
case session.InvalidSession:
http.SetCookie(w, &http.Cookie{
Name: "redirect",
Value: r.RequestURI,
Path: "/",
Name: "Session",
Value: "",
Path: "/",
MaxAge: -1,
})
}
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
case session.InvalidSession:
http.SetCookie(w, &http.Cookie{
Name: "Session",
Value: "",
Path: "/",
MaxAge: -1,
})
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
case session.Suspicious:
userSession := session.Get(sessionID)
err := userSession.Delete()
if err != nil {
app.Server.Logger.Error(err)
}
err = session.RemoveSessionInfo(user.Email, sessionID)
if err != nil {
app.Server.Logger.Error(err)
http.Redirect(w, r, "/signin", http.StatusSeeOther)
return
case session.Suspicious:
userSession := session.Get(sessionID)
err := userSession.Delete()
if err != nil {
app.Server.Logger.Error(err)
}
err = session.RemoveSessionInfo(user.Email, sessionID)
if err != nil {
app.Server.Logger.Error(err)
return
}
http.SetCookie(w, &http.Cookie{
Name: "Session",
Value: "",
Path: "/",
MaxAge: -1,
})
http.Redirect(w, r, "/signin?error=suspicious_session", http.StatusSeeOther)
return
default:
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
http.SetCookie(w, &http.Cookie{
Name: "Session",
Value: "",
Path: "/",
MaxAge: -1,
})
http.Redirect(w, r, "/signin?error=suspicious_session", http.StatusSeeOther)
return
default:
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
})
}

func Guest(next http.HandlerFunc, w http.ResponseWriter, r *http.Request) {
status, _, _ := session.GetSession(r)
func Guest(next http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status, _, _ := session.GetSession(r)

switch status {
case session.Authorized:
http.Redirect(w, r, "/", http.StatusSeeOther)
return
case session.Unauthorized:
next.ServeHTTP(w, r)
return
case session.InvalidSession:
http.SetCookie(w, &http.Cookie{
Name: "Session",
Value: "",
Path: "/",
MaxAge: -1,
})
next.ServeHTTP(w, r)
return
}
switch status {
case session.Authorized:
http.Redirect(w, r, "/", http.StatusSeeOther)
return
case session.Unauthorized:
next.ServeHTTP(w, r)
return
case session.InvalidSession:
http.SetCookie(w, &http.Cookie{
Name: "Session",
Value: "",
Path: "/",
MaxAge: -1,
})
next.ServeHTTP(w, r)
return
}
})
}
Loading

0 comments on commit db566fd

Please sign in to comment.