2019-05-14 14:11:03 +02:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"bitmask.me/skeleton/internal/app"
|
2019-08-22 00:48:27 +02:00
|
|
|
"github.com/alexedwards/scs"
|
|
|
|
"github.com/gorilla/csrf"
|
2019-05-14 14:11:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-22 00:48:27 +02:00
|
|
|
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),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-05-14 14:11:03 +02:00
|
|
|
func (h *Handlers) LandingPageHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
h.Templates().Get("landing.tmpl").Execute(w, nil)
|
|
|
|
}
|