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:
parent
1456ac5341
commit
dd0a4a8ba9
|
@ -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) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if !user.LoggedIn {
|
if !user.LoggedIn {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
serveLoginForm(w, r, user, company, requestPath)
|
serveLoginForm(w, r, user, company, r.RequestURI)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,14 +52,13 @@ func New(db *database.DB, avatarsDir string) (http.Handler, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
requestPath := r.URL.Path
|
|
||||||
var head string
|
var head string
|
||||||
head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
|
head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
|
||||||
switch head {
|
switch head {
|
||||||
case "static":
|
case "static":
|
||||||
h.fileHandler.ServeHTTP(w, r)
|
h.fileHandler.ServeHTTP(w, r)
|
||||||
case "favicon.ico":
|
case "favicon.ico":
|
||||||
r.URL.Path = requestPath
|
r.URL.Path = head
|
||||||
h.fileHandler.ServeHTTP(w, r)
|
h.fileHandler.ServeHTTP(w, r)
|
||||||
default:
|
default:
|
||||||
conn, err := h.db.Acquire(r.Context())
|
conn, err := h.db.Acquire(r.Context())
|
||||||
|
@ -85,7 +84,7 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
case "":
|
case "":
|
||||||
http.Redirect(w, r, "/"+user.Locale.Language.String()+"/", http.StatusFound)
|
http.Redirect(w, r, "/"+user.Locale.Language.String()+"/", http.StatusFound)
|
||||||
case "admin":
|
case "admin":
|
||||||
h.admin.Handle(user, company, conn, requestPath).ServeHTTP(w, r)
|
h.admin.Handle(user, company, conn).ServeHTTP(w, r)
|
||||||
case "login":
|
case "login":
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
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)
|
httplib.MethodNotAllowed(w, r, http.MethodPost, http.MethodGet)
|
||||||
}
|
}
|
||||||
case "me":
|
case "me":
|
||||||
h.profile.Handler(user, company, conn, requestPath).ServeHTTP(w, r)
|
h.profile.Handler(user, company, conn).ServeHTTP(w, r)
|
||||||
default:
|
default:
|
||||||
langTag, err := language.Parse(head)
|
langTag, err := language.Parse(head)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -80,11 +80,11 @@ func newProfileHandler(static http.Handler, avatarsDir string) (*profileHandler,
|
||||||
return handler, nil
|
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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if !user.LoggedIn {
|
if !user.LoggedIn {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
serveLoginForm(w, r, user, company, requestPath)
|
serveLoginForm(w, r, user, company, r.RequestURI)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue