update sitemap controller

This commit is contained in:
aichy126 2022-12-12 15:39:12 +08:00
parent 0f99388e77
commit 6459f6b7cb
3 changed files with 26 additions and 4 deletions

View File

@ -18,7 +18,6 @@ import (
"github.com/answerdev/answer/pkg/htmltext"
"github.com/answerdev/answer/pkg/obj"
"github.com/answerdev/answer/ui"
"github.com/davecgh/go-spew/spew"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/log"
)
@ -405,6 +404,15 @@ func (tc *TemplateController) html(ctx *gin.Context, code int, tpl string, siteI
}
func (tc *TemplateController) Sitemap(ctx *gin.Context) {
xml, err := tc.templateRenderController.Sitemap(ctx)
if err != nil {
tc.Page404(ctx)
return
}
ctx.String(http.StatusOK, xml)
}
func (tc *TemplateController) SitemapPage(ctx *gin.Context) {
page := 0
pageParam := ctx.Param("page")
pageRegexp := regexp.MustCompile(`question-(.*).xml`)
@ -418,6 +426,10 @@ func (tc *TemplateController) Sitemap(ctx *gin.Context) {
tc.Page404(ctx)
return
}
spew.Dump(page)
ctx.String(http.StatusOK, "sitemap")
xml, err := tc.templateRenderController.SitemapPage(ctx, page)
if err != nil {
tc.Page404(ctx)
return
}
ctx.String(http.StatusOK, xml)
}

View File

@ -1,6 +1,8 @@
package templaterender
import (
"fmt"
"github.com/answerdev/answer/internal/schema"
"github.com/gin-gonic/gin"
)
@ -12,3 +14,11 @@ func (t *TemplateRenderController) Index(ctx *gin.Context, req *schema.QuestionS
func (t *TemplateRenderController) QuestionDetail(ctx *gin.Context, id string) (resp *schema.QuestionInfo, err error) {
return t.questionService.GetQuestion(ctx, id, "", schema.QuestionPermission{})
}
func (t *TemplateRenderController) Sitemap(ctx *gin.Context) (string, error) {
return "Sitemap", nil
}
func (t *TemplateRenderController) SitemapPage(ctx *gin.Context, page int) (string, error) {
return fmt.Sprintf("SitemapPage-%d", page), nil
}

View File

@ -30,7 +30,7 @@ func NewTemplateRouter(
func (a *TemplateRouter) RegisterTemplateRouter(r *gin.RouterGroup) {
r.GET("/sitemap.xml", a.templateController.Sitemap)
r.GET("/sitemap/:page", a.templateController.Sitemap)
r.GET("/sitemap/:page", a.templateController.SitemapPage)
r.GET("/robots.txt", a.siteInfoController.GetRobots)