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
|
package snap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
|
@ -27,6 +28,10 @@ type Server struct {
|
||||||
router *mux.Router
|
router *mux.Router
|
||||||
templates string
|
templates string
|
||||||
cachedTmpl *template.Template
|
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 {
|
type SnapBaseContent struct {
|
||||||
|
@ -200,6 +205,14 @@ func (s *Server) getTemplates() *template.Template {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) render(w http.ResponseWriter, tmpl string, content interface{}) {
|
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)
|
err := s.getTemplates().ExecuteTemplate(w, tmpl, content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn(err)
|
log.Warn(err)
|
||||||
|
@ -289,6 +302,11 @@ func (s *Server) WithTheme(themeURL string) *Server {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) EnableTestMode(enable bool) *Server {
|
||||||
|
s.testModeEnabled = enable
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) WithRootFileSystem(fs http.FileSystem) *Server {
|
func (s *Server) WithRootFileSystem(fs http.FileSystem) *Server {
|
||||||
s.fs = fs
|
s.fs = fs
|
||||||
return s
|
return s
|
||||||
|
|
Loading…
Reference in New Issue