Documentation
¶
Overview ¶
templatehandler implements an http.Handler that can be used to create a basic read only api based on go templates.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RenderJsonError ¶
func RenderJsonError(w http.ResponseWriter, status int, err error)
RenderJsonError writes the provided err to the ResponseWriter in JSON format.
Types ¶
type DefaultRenderManager ¶
type DefaultRenderManager struct {
Data interface{}
}
DefaultRenderManager is an implementation of the RenderManager interface that selects the first template available and populates it with whatever data is assigned to the Data element of the DefaultRenderManager.
func (*DefaultRenderManager) GetData ¶
func (m *DefaultRenderManager) GetData(r *http.Request) (interface{}, error)
GetData returns the Data element of the DefaultRenderManager instance.
func (*DefaultRenderManager) TemplateSelector ¶
func (m *DefaultRenderManager) TemplateSelector(r *http.Request, t *template.Template) (string, error)
Returns the name of the first template from all those attached to t.
type ErrNotFound ¶
type ErrNotFound struct {
Message string
}
ErrNotFound should be returned when a template could not be found to serve the request.
func (ErrNotFound) Error ¶
func (e ErrNotFound) Error() string
type RenderManager ¶
type RenderManager interface { GetData(*http.Request) (interface{}, error) TemplateSelector(*http.Request, *template.Template) (string, error) TemplateFuncs() template.FuncMap }
RenderManagers are responsible for choosing the correct template to render and what data to populate it with. Embed the DefaultRenderManager for basic functionality.
type TemplateHandler ¶
type TemplateHandler struct { Template *template.Template Headers map[string]string RenderManager // contains filtered or unexported fields }
TemplateHandler implements the http.Handler interface. Typically this would be used to implement a very simple api using go templates and a custom RenderManager.
func NewTemplateHandler ¶
func NewTemplateHandler(path string, headers map[string]string, rm RenderManager) (*TemplateHandler, error)
NewTemplateHandler returns a TemplateHandler with all templates found under the provided path loaded into Template. The supplied RenderManager and Headers are also populated.
func (*TemplateHandler) ReloadTemplates ¶
func (th *TemplateHandler) ReloadTemplates() error
ReloadTemplates walks the supplied template path loading all templates into the handler
func (*TemplateHandler) ServeHTTP ¶
func (t *TemplateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles requests from the user. Any error raised during the process other than ErrTemplateNotFound results in a 500 status being returned to the user and a more detailed log being written.