2019-11-15 04:58:55 +00:00
|
|
|
package responses
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.kirsle.net/apps/gophertype/pkg/constants"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TemplateFuncs available to all templates.
|
|
|
|
func TemplateFuncs(r *http.Request) template.FuncMap {
|
|
|
|
return template.FuncMap{
|
2019-11-26 03:55:28 +00:00
|
|
|
"CSRF": CSRF(r),
|
|
|
|
"FormValue": FormValue(r),
|
|
|
|
"TestFunction": TestFunction(r),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CSRF returns the current CSRF token as an HTML hidden form field.
|
|
|
|
func CSRF(r *http.Request) func() template.HTML {
|
|
|
|
return func() template.HTML {
|
|
|
|
token, _ := r.Cookie(constants.CSRFCookieName)
|
|
|
|
return template.HTML(fmt.Sprintf(
|
|
|
|
`<input type="hidden" name="%s" value="%s">`,
|
|
|
|
constants.CSRFFormName,
|
|
|
|
token.Value,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FormValue returns a form value (1st item only).
|
|
|
|
func FormValue(r *http.Request) func(string) string {
|
|
|
|
return func(key string) string {
|
|
|
|
return r.FormValue(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFunction is a "hello world" template function.
|
|
|
|
func TestFunction(r *http.Request) func() template.HTML {
|
|
|
|
return func() template.HTML {
|
|
|
|
return template.HTML("TestFunction() called")
|
2019-11-15 04:58:55 +00:00
|
|
|
}
|
|
|
|
}
|