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

Organize route handlers #88

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading