pcmt/handlers/handlers.go

68 lines
1.5 KiB
Go
Raw Normal View History

package handlers
import (
"net/http"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
)
func Admin() echo.HandlerFunc {
return func(c echo.Context) error {
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid credentials")
}
}
func Index() echo.HandlerFunc {
return func(c echo.Context) error {
sess, _ := session.Get(setting.SessionCookieName(), c)
username := sess.Values["username"]
if username != nil {
return c.Redirect(http.StatusFound, "/home")
}
2023-05-11 23:50:40 +02:00
addHeaders(c)
csrf := c.Get("csrf").(string)
err := c.Render(
http.StatusOK,
"index.tmpl",
page{
AppName: setting.AppName(),
AppVer: appver,
Title: "Welcome!",
CSRF: csrf,
DevelMode: setting.IsDevel(),
Current: "home",
},
)
if err != nil {
c.Logger().Errorf("error: %q", err)
return renderErrorPage(
c,
http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError),
err.Error(),
)
}
return err
}
}
2023-05-11 23:50:40 +02:00
func addHeaders(c echo.Context) {
c.Response().Writer.Header().Set("Cross-Origin-Opener-Policy", "same-origin")
}
// experimental global redirect handler?
// http.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
// loc, err := url.QueryUnescape(r.URL.Query().Get("loc"))
// if err != nil {
// http.Error(w, fmt.Sprintf("invalid redirect: %q", r.URL.Query().Get("loc")), http.StatusBadRequest)
// return
// }
// http.Redirect(w, r, loc, http.StatusFound)
// })