examples for the news/ section
This commit is contained in:
parent
fdf11f9a23
commit
98ca4f8408
3
go.sum
3
go.sum
|
@ -5,6 +5,7 @@ github.com/AndreasBriese/bbloom v0.0.0-20180913140656-343706a395b7/go.mod h1:bOv
|
|||
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
|
||||
github.com/PuerkitoBio/goquery v1.5.0 h1:uGvmFXOA73IKluu/F84Xd1tt/z07GYm8X49XKHP7EJk=
|
||||
github.com/PuerkitoBio/goquery v1.5.0/go.mod h1:qD2PgZ9lccMbQlc7eEOjaeRlFQON7xY8kdmcsrnKqMg=
|
||||
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
|
||||
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
|
||||
|
@ -14,6 +15,7 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
|
|||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
|
||||
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
|
||||
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
|
||||
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
|
||||
|
@ -367,6 +369,7 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR
|
|||
golang.org/x/net v0.0.0-20190607181551-461777fb6f67/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 h1:fHDIZ2oxGnUZRN6WgWFCbYBjH9uqVPRCUVUDhs0wnbA=
|
||||
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
|
|
|
@ -2,7 +2,6 @@ package handlers
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
@ -24,9 +23,7 @@ func New(m *mux.Router) (http.Handler, error) {
|
|||
"/landing/index.tmpl",
|
||||
"/landing/about.tmpl",
|
||||
"/error.tmpl")...),
|
||||
render.FuncMap(template.FuncMap{
|
||||
"urlTo": web.NewURLTo(m),
|
||||
}),
|
||||
render.FuncMap(web.TemplateFuncs(m)),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("web Handler: failed to create renderer: %w", err)
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package news
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.mindeco.de/http/render"
|
||||
"go.mindeco.de/http/tester"
|
||||
"go.mindeco.de/logging/logtest"
|
||||
|
||||
"go.mindeco.de/ssb-rooms/web"
|
||||
"go.mindeco.de/ssb-rooms/web/router"
|
||||
)
|
||||
|
||||
var (
|
||||
testMux *http.ServeMux
|
||||
testClient *tester.Tester
|
||||
testRouter = router.News(nil)
|
||||
|
||||
testAssets = http.Dir("../../templates")
|
||||
)
|
||||
|
||||
func setup(t *testing.T) {
|
||||
log, _ := logtest.KitLogger("feed", t)
|
||||
r, err := render.New(testAssets, //TODO: embedd web.Assets,
|
||||
render.SetLogger(log),
|
||||
render.BaseTemplates("/testing/base.tmpl"),
|
||||
render.AddTemplates(append(HTMLTemplates, "/error.tmpl")...),
|
||||
render.FuncMap(web.TemplateFuncs(testRouter)),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(errors.Wrap(err, "setup: render init failed"))
|
||||
}
|
||||
testMux = http.NewServeMux()
|
||||
testMux.Handle("/", Handler(testRouter, r))
|
||||
testClient = tester.New(testMux, t)
|
||||
}
|
||||
|
||||
func teardown() {
|
||||
testMux = nil
|
||||
testClient = nil
|
||||
}
|
|
@ -10,13 +10,26 @@ import (
|
|||
)
|
||||
|
||||
type Post struct {
|
||||
ID int
|
||||
Name, Text string
|
||||
}
|
||||
|
||||
var db = []Post{
|
||||
Post{"Hello", "lot's of stuff"},
|
||||
Post{"Testing", "yeeeeaaaahhhh..."},
|
||||
Post{"WAT", "i have only a partial idea of what i'm doing"},
|
||||
Post{
|
||||
ID: 0,
|
||||
Name: "Hello",
|
||||
Text: "lot's of stuff",
|
||||
},
|
||||
Post{
|
||||
ID: 1,
|
||||
Name: "Testing",
|
||||
Text: "yeeeeaaaahhhh...",
|
||||
},
|
||||
Post{
|
||||
ID: 2,
|
||||
Name: "WAT",
|
||||
Text: "i have only a partial idea of what i'm doing",
|
||||
},
|
||||
}
|
||||
|
||||
func showOverview(w http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
package news
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"go.mindeco.de/ssb-rooms/web/router"
|
||||
)
|
||||
|
||||
func TestOverview(t *testing.T) {
|
||||
setup(t)
|
||||
defer teardown()
|
||||
a := assert.New(t)
|
||||
url, err := router.News(nil).Get(router.NewsOverview).URL()
|
||||
a.Nil(err)
|
||||
html, resp := testClient.GetHTML(url.String(), nil)
|
||||
a.Equal(http.StatusOK, resp.Code, "wrong HTTP status code")
|
||||
a.Equal(html.Find("#welcome").Text(), "Welcome!")
|
||||
}
|
||||
|
||||
func TestPost(t *testing.T) {
|
||||
setup(t)
|
||||
defer teardown()
|
||||
a := assert.New(t)
|
||||
url, err := router.News(nil).Get(router.NewsPost).URL("PostID", "1")
|
||||
a.Nil(err)
|
||||
html, resp := testClient.GetHTML(url.String(), nil)
|
||||
a.Equal(http.StatusOK, resp.Code, "wrong HTTP status code")
|
||||
a.Equal(html.Find("h1").Text(), db[1].Name)
|
||||
}
|
||||
|
||||
func TestURLTo(t *testing.T) {
|
||||
setup(t)
|
||||
defer teardown()
|
||||
a := assert.New(t)
|
||||
url, err := router.News(nil).Get(router.NewsPost).URL("PostID", "1")
|
||||
a.Nil(err)
|
||||
html, resp := testClient.GetHTML(url.String(), nil)
|
||||
a.Equal(http.StatusOK, resp.Code, "wrong HTTP status code")
|
||||
a.Equal(html.Find("h1").Text(), db[1].Name)
|
||||
lnk, ok := html.Find("#overview").Attr("href")
|
||||
a.True(ok)
|
||||
a.Equal("/", lnk)
|
||||
lnk, ok = html.Find("#next").Attr("href")
|
||||
a.True(ok, "did not find href attribute")
|
||||
a.Equal("/post/2", lnk)
|
||||
}
|
|
@ -9,4 +9,5 @@
|
|||
</div>
|
||||
</div> <!-- /row -->
|
||||
<a href="{{urlTo "news:overview"}}" id="overview">Overview</a>
|
||||
<a href="{{urlTo "news:post" "PostID" (inc .ID)}}" id="next">Next...</a>
|
||||
{{end}}
|
|
@ -0,0 +1,13 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>{{block "title" .}}Default Title{{end}}</title>
|
||||
</head>
|
||||
<body>
|
||||
{{block "extra" .}}{{end}}
|
||||
<div class="container">
|
||||
{{ block "content" . }}{{end}}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -2,6 +2,7 @@ package web
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"html/template"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
|
@ -10,6 +11,13 @@ import (
|
|||
"go.mindeco.de/logging"
|
||||
)
|
||||
|
||||
func TemplateFuncs(m *mux.Router) template.FuncMap {
|
||||
return template.FuncMap{
|
||||
"urlTo": NewURLTo(m),
|
||||
"inc": func(i int) int { return i + 1 },
|
||||
}
|
||||
}
|
||||
|
||||
func NewURLTo(appRouter *mux.Router) func(string, ...interface{}) *url.URL {
|
||||
l := logging.Logger("helper.URLTo") // TOOD: inject in a scoped way
|
||||
return func(routeName string, ps ...interface{}) *url.URL {
|
||||
|
|
Loading…
Reference in New Issue