diff --git a/server.go b/server.go index 012a6f9..8202735 100644 --- a/server.go +++ b/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