add a debug mode for testing that returns json content and no tempaltes
This commit is contained in:
parent
46eb9862e6
commit
baada0dae5
18
server.go
18
server.go
|
@ -1,6 +1,7 @@
|
|||
package snap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
|
@ -27,6 +28,10 @@ type Server struct {
|
|||
router *mux.Router
|
||||
templates string
|
||||
cachedTmpl *template.Template
|
||||
|
||||
// testModeEnabled if test mode is enabled we will not render the template but jsut return the
|
||||
// json object
|
||||
testModeEnabled bool
|
||||
}
|
||||
|
||||
type SnapBaseContent struct {
|
||||
|
@ -200,6 +205,14 @@ func (s *Server) getTemplates() *template.Template {
|
|||
}
|
||||
|
||||
func (s *Server) render(w http.ResponseWriter, tmpl string, content interface{}) {
|
||||
if s.testModeEnabled {
|
||||
msg, err := json.Marshal(content)
|
||||
if err != nil {
|
||||
s.renderError(w, 400, "Internal Server Error")
|
||||
}
|
||||
s.reply(w, string(msg))
|
||||
return
|
||||
}
|
||||
err := s.getTemplates().ExecuteTemplate(w, tmpl, content)
|
||||
if err != nil {
|
||||
log.Warn(err)
|
||||
|
@ -289,6 +302,11 @@ func (s *Server) WithTheme(themeURL string) *Server {
|
|||
return s
|
||||
}
|
||||
|
||||
func (s *Server) EnableTestMode(enable bool) *Server {
|
||||
s.testModeEnabled = enable
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) WithRootFileSystem(fs http.FileSystem) *Server {
|
||||
s.fs = fs
|
||||
return s
|
||||
|
|
Loading…
Reference in New Issue