From baada0dae53e0111540ce3db0d3a9579188f4275 Mon Sep 17 00:00:00 2001 From: spsobole Date: Sat, 13 Feb 2021 15:49:08 -0700 Subject: [PATCH] add a debug mode for testing that returns json content and no tempaltes --- server.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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