Open platform to collect and prioritize feedback
feature: add sitemap.xml (#558)
Guilherme Oenning9353e755e320
diff --git a/app/cmd/routes.go b/app/cmd/routes.goindex 3857c1d3f..180c06c81 100644--- a/app/cmd/routes.go+++ b/app/cmd/routes.go@@ -53,6 +53,8 @@ func routes(r *web.Engine) *web.Engine {//From this step, a Tenant is required (regardless of status)r.Use(middlewares.RequireTenant())+ r.Get("/sitemap.xml", handlers.Sitemap())+tenantAssets := r.Group(){tenantAssets.Use(middlewares.ClientCache(72 * time.Hour))diff --git a/app/handlers/common.go b/app/handlers/common.goindex a2b457303..de968acff 100644--- a/app/handlers/common.go+++ b/app/handlers/common.go@@ -1,8 +1,10 @@package handlersimport (+ "fmt""io/ioutil""net/http"+ "strings""time""github.com/getfider/fider/app"@@ -36,6 +38,33 @@ func LegalPage(title, file string) web.HandlerFunc {}}+//Sitemap returns the sitemap.xml of current site+func Sitemap() web.HandlerFunc {+ return func(c web.Context) error {+ if c.Tenant().IsPrivate {+ return c.NotFound()+ }++ posts, err := c.Services().Posts.GetAll()+ if err != nil {+ return c.Failure(err)+ }++ baseURL := c.BaseURL()+ text := strings.Builder{}+ text.WriteString(`<?xml version="1.0" encoding="UTF-8"?>`)+ text.WriteString(`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`)+ text.WriteString(fmt.Sprintf("<url> <loc>%s</loc> </url>", baseURL))+ for _, post := range posts {+ text.WriteString(fmt.Sprintf("<url> <loc>%s/posts/%d/%s</loc> </url>", baseURL, post.Number, post.Slug))+ }+ text.WriteString(`</urlset>`)++ c.Response.Header().Del("Content-Security-Policy")+ return c.XML(http.StatusOK, text.String())+ }+}+//RobotsTXT return content of robots.txt filefunc RobotsTXT() web.HandlerFunc {return func(c web.Context) error {diff --git a/app/handlers/common_test.go b/app/handlers/common_test.goindex b269067b2..9d89391a9 100644--- a/app/handlers/common_test.go+++ b/app/handlers/common_test.go@@ -1,6 +1,7 @@package handlers_testimport (+ "io/ioutil""net/http""testing"@@ -56,3 +57,55 @@ func TestRobotsTXT(t *testing.T) {code, _ := server.Execute(handlers.RobotsTXT())Expect(code).Equals(http.StatusOK)}++func TestSitemap(t *testing.T) {+ RegisterT(t)++ server, _ := mock.NewServer()+ code, response := server.+ OnTenant(mock.DemoTenant).+ WithURL("http://demo.test.fider.io:3000/sitemap.xml").+ Execute(handlers.Sitemap())++ bytes, _ := ioutil.ReadAll(response.Body)+ Expect(code).Equals(http.StatusOK)+ Expect(string(bytes)).Equals(`<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url> <loc>http://demo.test.fider.io:3000</loc> </url></urlset>`)+}++func TestSitemap_WithPosts(t *testing.T) {+ RegisterT(t)++ server, services := mock.NewServer()+ services.Posts.SetCurrentUser(mock.AryaStark)+ services.Posts.Add("My new idea 1", "")+ services.Posts.SetCurrentUser(mock.AryaStark)+ services.Posts.Add("The other idea", "")++ code, response := server.+ OnTenant(mock.DemoTenant).+ WithURL("http://demo.test.fider.io:3000/sitemap.xml").+ Execute(handlers.Sitemap())++ bytes, _ := ioutil.ReadAll(response.Body)+ Expect(code).Equals(http.StatusOK)+ Expect(string(bytes)).Equals(`<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url> <loc>http://demo.test.fider.io:3000</loc> </url><url> <loc>http://demo.test.fider.io:3000/posts/1/my-new-idea-1</loc> </url><url> <loc>http://demo.test.fider.io:3000/posts/2/the-other-idea</loc> </url></urlset>`)+}++func TestSitemap_PrivateTenant_WithPosts(t *testing.T) {+ RegisterT(t)++ server, services := mock.NewServer()+ services.Posts.SetCurrentUser(mock.AryaStark)+ services.Posts.Add("My new idea 1", "")+ services.Posts.SetCurrentUser(mock.AryaStark)+ services.Posts.Add("The other idea", "")++ mock.DemoTenant.IsPrivate = true++ code, _ := server.+ OnTenant(mock.DemoTenant).+ WithURL("http://demo.test.fider.io:3000/sitemap.xml").+ Execute(handlers.Sitemap())++ Expect(code).Equals(http.StatusNotFound)+}diff --git a/app/pkg/web/context.go b/app/pkg/web/context.goindex 95ddba3d4..4f932510a 100644--- a/app/pkg/web/context.go+++ b/app/pkg/web/context.go@@ -40,8 +40,10 @@ var (PlainContentType = "text/plain"HTMLContentType = "text/html"JSONContentType = "application/json"+ XMLContentType = "application/xml"UTF8PlainContentType = PlainContentType + "; charset=utf-8"UTF8HTMLContentType = HTMLContentType + "; charset=utf-8"+ UTF8XMLContentType = XMLContentType + "; charset=utf-8"UTF8JSONContentType = JSONContentType + "; charset=utf-8")@@ -450,6 +452,11 @@ func (ctx *Context) String(code int, text string) error {return ctx.Blob(code, UTF8PlainContentType, []byte(text))}+// XML returns a XML response with status code.+func (ctx *Context) XML(code int, text string) error {+ return ctx.Blob(code, UTF8XMLContentType, []byte(text))+}+// JSON returns a JSON response with status code.func (ctx *Context) JSON(code int, i interface{}) error {b, err := json.Marshal(i)