Instead of keeping a requestPath in app, use request.RequestURI

I was keeping this variable to redirect to the requested URL when the
user has no permission, is not logged in, and is shown the login form to
redirect them back to the original URL.  Since each handler removes a
part of the path each time, i need to keep the original Path for that.

However, i just found out that request.RequestURI already is that
original URI requested by the client.  No need for an extra variable.
This commit is contained in:
jordi fita mas 2023-08-06 04:03:04 +02:00
parent 1456ac5341
commit dd0a4a8ba9
3 changed files with 7 additions and 8 deletions

View File

@ -25,11 +25,11 @@ func newAdminHandler() *adminHandler {
}
}
func (h *adminHandler) Handle(user *auth.User, company *auth.Company, conn *database.Conn, requestPath string) http.Handler {
func (h *adminHandler) Handle(user *auth.User, company *auth.Company, conn *database.Conn) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !user.LoggedIn {
w.WriteHeader(http.StatusUnauthorized)
serveLoginForm(w, r, user, company, requestPath)
serveLoginForm(w, r, user, company, r.RequestURI)
return
}

View File

@ -52,14 +52,13 @@ func New(db *database.DB, avatarsDir string) (http.Handler, error) {
}
func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
requestPath := r.URL.Path
var head string
head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
switch head {
case "static":
h.fileHandler.ServeHTTP(w, r)
case "favicon.ico":
r.URL.Path = requestPath
r.URL.Path = head
h.fileHandler.ServeHTTP(w, r)
default:
conn, err := h.db.Acquire(r.Context())
@ -85,7 +84,7 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case "":
http.Redirect(w, r, "/"+user.Locale.Language.String()+"/", http.StatusFound)
case "admin":
h.admin.Handle(user, company, conn, requestPath).ServeHTTP(w, r)
h.admin.Handle(user, company, conn).ServeHTTP(w, r)
case "login":
switch r.Method {
case http.MethodGet:
@ -100,7 +99,7 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
httplib.MethodNotAllowed(w, r, http.MethodPost, http.MethodGet)
}
case "me":
h.profile.Handler(user, company, conn, requestPath).ServeHTTP(w, r)
h.profile.Handler(user, company, conn).ServeHTTP(w, r)
default:
langTag, err := language.Parse(head)
if err != nil {

View File

@ -80,11 +80,11 @@ func newProfileHandler(static http.Handler, avatarsDir string) (*profileHandler,
return handler, nil
}
func (h *profileHandler) Handler(user *auth.User, company *auth.Company, conn *database.Conn, requestPath string) http.HandlerFunc {
func (h *profileHandler) Handler(user *auth.User, company *auth.Company, conn *database.Conn) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !user.LoggedIn {
w.WriteHeader(http.StatusUnauthorized)
serveLoginForm(w, r, user, company, requestPath)
serveLoginForm(w, r, user, company, r.RequestURI)
return
}