pcmt/handlers/handlers.go
leo 122ea638c9
All checks were successful
continuous-integration/drone/push Build is passing
go: refactor template rendering
* create pkg 'modules/template'
* move template rendering code from 'handlers' to 'modules/template'
* update call sites
* walk the 'templates' dir to discover nested hierarchies
* solidify LiveMode handling (vs embedded assets)
* break out funcMap to it's own file
* general clean-up
2023-05-11 04:32:39 +02:00

57 lines
1.3 KiB
Go

package handlers
import (
"net/http"
modtmpl "git.dotya.ml/mirre-mt/pcmt/modules/template"
"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")
}
tpl := modtmpl.Get("index.tmpl")
csrf := c.Get("csrf").(string)
err := tpl.Execute(c.Response().Writer,
page{
AppName: setting.AppName(),
AppVer: appver,
Title: "Welcome!",
CSRF: csrf,
DevelMode: setting.IsDevel(),
Current: "home",
},
)
if err != nil {
log.Errorf("error when executing tmpl: '%q'", err)
return err
}
return nil
}
}
// 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)
// })