Skip to content

Commit

Permalink
move Template.ServeHTTP to it's own type
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed May 24, 2024
1 parent ef18a47 commit 2cb7e52
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
18 changes: 18 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package jaws

import "net/http"

// Handler implements ServeHTTP with a jaws.Template
type Handler struct {
*Jaws
Template
}

func (h Handler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
_ = h.Log(h.NewRequest(r).NewElement(h.Template).JawsRender(wr, nil))
}

// Handler returns a http.Handler using a jaws.Template
func (jw *Jaws) Handler(name string, dot any) http.Handler {
return Handler{Jaws: jw, Template: Template{Template: name, Dot: dot}}
}
24 changes: 24 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package jaws

import (
"bytes"
"net/http/httptest"
"testing"
)

func TestHandler_ServeHTTP(t *testing.T) {
nextJid = 0
rq := newTestRequest()
defer rq.Close()

dot := 123
h := rq.Jaws.Handler("testtemplate", dot)
var buf bytes.Buffer
var rr httptest.ResponseRecorder
rr.Body = &buf
r := httptest.NewRequest("GET", "/", nil)
h.ServeHTTP(&rr, r)
if got := buf.String(); got != `<div id="Jid.1" >123</div>` {
t.Error(got)
}
}
14 changes: 2 additions & 12 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@ import (
"fmt"
"html/template"
"io"
"net/http"
"strings"

"github.com/linkdata/jaws/what"
)

type Template struct {
*Jaws
Template string
Dot any
}

var _ UI = Template{} // statically ensure interface is defined
var _ EventHandler = Template{} // statically ensure interface is defined
var _ http.Handler = Template{} // statically ensure interface is defined

func (t Template) String() string {
return fmt.Sprintf("{%q, %s}", t.Template, TagString(t.Dot))
Expand All @@ -32,7 +29,7 @@ func (t Template) JawsRender(e *Element, wr io.Writer, params []any) error {
e.Tag(tags...)
e.handlers = append(e.handlers, handlers...)
attrstr := template.HTMLAttr(strings.Join(attrs, " ")) // #nosec G203
return t.Jaws.Lookup(t.Template).Execute(wr, With{
return e.Request.Jaws.Lookup(t.Template).Execute(wr, With{
Element: e,
RequestWriter: e.Request.Writer(wr),
Dot: t.Dot,
Expand All @@ -50,13 +47,6 @@ func (t Template) JawsEvent(e *Element, wht what.What, val string) error {
return callEventHandler(t.Dot, e, wht, val)
}

// ServeHTTP implements http.Handler.
//
// http.DefaultServeMux.Handle("/user", myJaws.NewTemplate("user.html", userData))
func (t Template) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
_ = t.Log(t.NewRequest(r).NewElement(t).JawsRender(wr, nil))
}

func (jw *Jaws) NewTemplate(name string, dot any) Template {
return Template{Jaws: jw, Template: name, Dot: dot}
return Template{Template: name, Dot: dot}
}
19 changes: 0 additions & 19 deletions template_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package jaws

import (
"bytes"
"net/http/httptest"
"testing"
)

Expand All @@ -28,20 +26,3 @@ func TestTemplate_Calls_Dot_Updater(t *testing.T) {
t.Error(dot.updateCalled)
}
}

func TestTemplate_As_Handler(t *testing.T) {
nextJid = 0
rq := newTestRequest()
defer rq.Close()

dot := 123
tmpl := rq.Jaws.NewTemplate("testtemplate", dot)
var buf bytes.Buffer
var rr httptest.ResponseRecorder
rr.Body = &buf
r := httptest.NewRequest("GET", "/", nil)
tmpl.ServeHTTP(&rr, r)
if got := buf.String(); got != `<div id="Jid.1" >123</div>` {
t.Error(got)
}
}

0 comments on commit 2cb7e52

Please sign in to comment.