diff --git a/go.mod b/go.mod index 0b2ca928..6b779048 100644 --- a/go.mod +++ b/go.mod @@ -73,6 +73,7 @@ require ( github.com/inconshreveable/mousetrap v1.0.1 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect + github.com/jxskiss/ginregex v0.2.0 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect github.com/lestrrat-go/strftime v1.0.6 // indirect diff --git a/go.sum b/go.sum index 01b47bd3..00b8f5f8 100644 --- a/go.sum +++ b/go.sum @@ -166,6 +166,7 @@ github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjo github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.7.0/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= +github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8= github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -409,6 +410,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1 github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/jxskiss/ginregex v0.2.0 h1:ufz3EWGEF4oUJr5PEmS1Z7AzmzRsaIGux2M0Jogfwds= +github.com/jxskiss/ginregex v0.2.0/go.mod h1:3Ioyw1ilM5ZQVsOkCfjbBgcABgbmGErEIQH5gRYU3Wk= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go index 96ec4fdc..2768344e 100644 --- a/internal/controller/template_controller.go +++ b/internal/controller/template_controller.go @@ -14,9 +14,11 @@ import ( templaterender "github.com/answerdev/answer/internal/controller/template_render" "github.com/answerdev/answer/internal/schema" "github.com/answerdev/answer/internal/service/siteinfo_common" + "github.com/answerdev/answer/pkg/converter" "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" ) @@ -401,3 +403,21 @@ func (tc *TemplateController) html(ctx *gin.Context, code int, tpl string, siteI ctx.HTML(code, tpl, data) } + +func (tc *TemplateController) Sitemap(ctx *gin.Context) { + page := 0 + pageParam := ctx.Param("page") + pageRegexp := regexp.MustCompile(`question-(.*).xml`) + pageStr := pageRegexp.FindStringSubmatch(pageParam) + if len(pageStr) != 2 { + tc.Page404(ctx) + return + } + page = converter.StringToInt(pageStr[1]) + if page == 0 { + tc.Page404(ctx) + return + } + spew.Dump(page) + ctx.String(http.StatusOK, "sitemap") +} diff --git a/internal/router/template_router.go b/internal/router/template_router.go index 7b5c61dd..1fba0807 100644 --- a/internal/router/template_router.go +++ b/internal/router/template_router.go @@ -29,6 +29,9 @@ func NewTemplateRouter( // TemplateRouter template router func (a *TemplateRouter) RegisterTemplateRouter(r *gin.RouterGroup) { + r.GET("/sitemap.xml", a.templateController.Sitemap) + r.GET("/sitemap/:page", a.templateController.Sitemap) + r.GET("/robots.txt", a.siteInfoController.GetRobots) r.GET("/", a.templateController.Index)