skeleton/internal/web/handlers.go
2019-08-22 00:48:27 +02:00

47 lines
1.1 KiB
Go

package web
import (
"net/http"
"bitmask.me/skeleton/internal/app"
"github.com/alexedwards/scs"
"github.com/gorilla/csrf"
)
type Handlers struct {
*app.App
session *scs.Session
}
func NewHandlers(app *app.App) *Handlers {
h := &Handlers{App: app}
h.session = scs.NewSession()
h.session.Cookie.Persist = false
h.session.Cookie.Secure = false
return h
}
func (h *Handlers) Session() *scs.Session {
return h.session
}
func (h *Handlers) commonRenderContext(r *http.Request) map[string]interface{} {
return map[string]interface{}{
csrf.TemplateTag: csrf.TemplateField(r),
"Username": h.Session().GetString(r.Context(), SessKeyUserName),
"UserID": h.Session().GetString(r.Context(), SessKeyUserID),
}
}
func (h *Handlers) CSRF() func(http.Handler) http.Handler {
return csrf.Protect(
[]byte("12345678901234567890123456789012"),
csrf.FieldName("authenticity_token"),
csrf.Secure(h.session.Cookie.Secure),
)
}
func (h *Handlers) LandingPageHandler(w http.ResponseWriter, r *http.Request) {
h.Templates().Get("landing.tmpl").Execute(w, nil)
}