update seo config

This commit is contained in:
aichy126 2022-12-05 17:18:56 +08:00
parent e78747c172
commit 6e000bb5f1
11 changed files with 394 additions and 5 deletions

View File

@ -192,7 +192,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
avatarMiddleware := middleware.NewAvatarMiddleware(serviceConf, uploaderService) avatarMiddleware := middleware.NewAvatarMiddleware(serviceConf, uploaderService)
templateRenderController := templaterender.NewTemplateRenderController(questionService, userService, tagService, answerService, commentService) templateRenderController := templaterender.NewTemplateRenderController(questionService, userService, tagService, answerService, commentService)
templateController := controller.NewTemplateController(templateRenderController, siteInfoCommonService) templateController := controller.NewTemplateController(templateRenderController, siteInfoCommonService)
templateRouter := router.NewTemplateRouter(templateController, templateRenderController) templateRouter := router.NewTemplateRouter(templateController, templateRenderController, siteInfoController)
ginEngine := server.NewHTTPServer(debug, staticRouter, answerAPIRouter, swaggerRouter, uiRouter, authUserMiddleware, avatarMiddleware, templateRouter) ginEngine := server.NewHTTPServer(debug, staticRouter, answerAPIRouter, swaggerRouter, uiRouter, authUserMiddleware, avatarMiddleware, templateRouter)
application := newApplication(serverConf, ginEngine) application := newApplication(serverConf, ginEngine)
return application, func() { return application, func() {

View File

@ -787,6 +787,77 @@ const docTemplate = `{
} }
} }
}, },
"/answer/admin/api/siteinfo/seo": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "get site seo information",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "get site seo information",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/handler.RespBody"
},
{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/schema.SiteSeoResp"
}
}
}
]
}
}
}
},
"put": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "update site seo information",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "update site seo information",
"parameters": [
{
"description": "seo",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/schema.SiteSeoReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/handler.RespBody"
}
}
}
}
},
"/answer/admin/api/siteinfo/write": { "/answer/admin/api/siteinfo/write": {
"get": { "get": {
"security": [ "security": [
@ -4337,6 +4408,26 @@ const docTemplate = `{
} }
} }
} }
},
"/robots.txt": {
"get": {
"description": "get site robots information",
"produces": [
"application/json"
],
"tags": [
"site"
],
"summary": "get site robots information",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
} }
}, },
"definitions": { "definitions": {
@ -6037,6 +6128,28 @@ const docTemplate = `{
} }
} }
}, },
"schema.SiteSeoReq": {
"type": "object",
"required": [
"robots"
],
"properties": {
"robots": {
"type": "string"
}
}
},
"schema.SiteSeoResp": {
"type": "object",
"required": [
"robots"
],
"properties": {
"robots": {
"type": "string"
}
}
},
"schema.SiteWriteReq": { "schema.SiteWriteReq": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -775,6 +775,77 @@
} }
} }
}, },
"/answer/admin/api/siteinfo/seo": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "get site seo information",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "get site seo information",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/handler.RespBody"
},
{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/schema.SiteSeoResp"
}
}
}
]
}
}
}
},
"put": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "update site seo information",
"produces": [
"application/json"
],
"tags": [
"admin"
],
"summary": "update site seo information",
"parameters": [
{
"description": "seo",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/schema.SiteSeoReq"
}
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/handler.RespBody"
}
}
}
}
},
"/answer/admin/api/siteinfo/write": { "/answer/admin/api/siteinfo/write": {
"get": { "get": {
"security": [ "security": [
@ -4325,6 +4396,26 @@
} }
} }
} }
},
"/robots.txt": {
"get": {
"description": "get site robots information",
"produces": [
"application/json"
],
"tags": [
"site"
],
"summary": "get site robots information",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
} }
}, },
"definitions": { "definitions": {
@ -6025,6 +6116,28 @@
} }
} }
}, },
"schema.SiteSeoReq": {
"type": "object",
"required": [
"robots"
],
"properties": {
"robots": {
"type": "string"
}
}
},
"schema.SiteSeoResp": {
"type": "object",
"required": [
"robots"
],
"properties": {
"robots": {
"type": "string"
}
}
},
"schema.SiteWriteReq": { "schema.SiteWriteReq": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -1223,6 +1223,20 @@ definitions:
terms_of_service_parsed_text: terms_of_service_parsed_text:
type: string type: string
type: object type: object
schema.SiteSeoReq:
properties:
robots:
type: string
required:
- robots
type: object
schema.SiteSeoResp:
properties:
robots:
type: string
required:
- robots
type: object
schema.SiteWriteReq: schema.SiteWriteReq:
properties: properties:
recommend_tags: recommend_tags:
@ -2071,6 +2085,47 @@ paths:
summary: update site legal info summary: update site legal info
tags: tags:
- admin - admin
/answer/admin/api/siteinfo/seo:
get:
description: get site seo information
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/handler.RespBody'
- properties:
data:
$ref: '#/definitions/schema.SiteSeoResp'
type: object
security:
- ApiKeyAuth: []
summary: get site seo information
tags:
- admin
put:
description: update site seo information
parameters:
- description: seo
in: body
name: data
required: true
schema:
$ref: '#/definitions/schema.SiteSeoReq'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/handler.RespBody'
security:
- ApiKeyAuth: []
summary: update site seo information
tags:
- admin
/answer/admin/api/siteinfo/write: /answer/admin/api/siteinfo/write:
get: get:
description: get site interface description: get site interface
@ -4230,6 +4285,19 @@ paths:
summary: UserList summary: UserList
tags: tags:
- api-question - api-question
/robots.txt:
get:
description: get site robots information
produces:
- application/json
responses:
"200":
description: OK
schema:
type: string
summary: get site robots information
tags:
- site
securityDefinitions: securityDefinitions:
ApiKeyAuth: ApiKeyAuth:
in: header in: header

View File

@ -56,5 +56,5 @@ const (
SiteTypeBranding = "branding" SiteTypeBranding = "branding"
SiteTypeWrite = "write" SiteTypeWrite = "write"
SiteTypeLegal = "legal" SiteTypeLegal = "legal"
SiteTypeSeo = "seo"
) )

View File

@ -1,9 +1,6 @@
package server package server
import ( import (
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/pkg/converter"
"github.com/answerdev/answer/pkg/day"
"html/template" "html/template"
"io/fs" "io/fs"
"math" "math"
@ -12,6 +9,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/pkg/converter"
"github.com/answerdev/answer/pkg/day"
brotli "github.com/anargu/gin-brotli" brotli "github.com/anargu/gin-brotli"
"github.com/answerdev/answer/internal/base/middleware" "github.com/answerdev/answer/internal/base/middleware"
"github.com/answerdev/answer/internal/base/translator" "github.com/answerdev/answer/internal/base/translator"

View File

@ -1,6 +1,8 @@
package controller_backyard package controller_backyard
import ( import (
"net/http"
"github.com/answerdev/answer/internal/base/handler" "github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/middleware" "github.com/answerdev/answer/internal/base/middleware"
"github.com/answerdev/answer/internal/schema" "github.com/answerdev/answer/internal/schema"
@ -85,6 +87,53 @@ func (sc *SiteInfoController) GetSiteLegal(ctx *gin.Context) {
handler.HandleResponse(ctx, err, resp) handler.HandleResponse(ctx, err, resp)
} }
// GetSeo get site seo information
// @Summary get site seo information
// @Description get site seo information
// @Security ApiKeyAuth
// @Tags admin
// @Produce json
// @Success 200 {object} handler.RespBody{data=schema.SiteSeoResp}
// @Router /answer/admin/api/siteinfo/seo [get]
func (sc *SiteInfoController) GetSeo(ctx *gin.Context) {
resp, err := sc.siteInfoService.GetSeo(ctx)
handler.HandleResponse(ctx, err, resp)
}
// GetRobots get site robots information
// @Summary get site robots information
// @Description get site robots information
// @Tags site
// @Produce json
// @Success 200 {string} txt ""
// @Router /robots.txt [get]
func (sc *SiteInfoController) GetRobots(ctx *gin.Context) {
resp, err := sc.siteInfoService.GetSeo(ctx)
if err != nil {
ctx.String(http.StatusOK, "")
return
}
ctx.String(http.StatusOK, resp.Robots)
}
// UpdateSeo update site seo information
// @Summary update site seo information
// @Description update site seo information
// @Security ApiKeyAuth
// @Tags admin
// @Produce json
// @Param data body schema.SiteSeoReq true "seo"
// @Success 200 {object} handler.RespBody{}
// @Router /answer/admin/api/siteinfo/seo [put]
func (sc *SiteInfoController) UpdateSeo(ctx *gin.Context) {
req := schema.SiteSeoReq{}
if handler.BindAndCheck(ctx, &req) {
return
}
err := sc.siteInfoService.SaveSeo(ctx, req)
handler.HandleResponse(ctx, err, nil)
}
// UpdateGeneral update site general information // UpdateGeneral update site general information
// @Summary update site general information // @Summary update site general information
// @Description update site general information // @Description update site general information

View File

@ -231,11 +231,13 @@ func (a *AnswerAPIRouter) RegisterAnswerCmsAPIRouter(r *gin.RouterGroup) {
r.GET("/siteinfo/branding", a.siteInfoController.GetSiteBranding) r.GET("/siteinfo/branding", a.siteInfoController.GetSiteBranding)
r.GET("/siteinfo/write", a.siteInfoController.GetSiteWrite) r.GET("/siteinfo/write", a.siteInfoController.GetSiteWrite)
r.GET("/siteinfo/legal", a.siteInfoController.GetSiteLegal) r.GET("/siteinfo/legal", a.siteInfoController.GetSiteLegal)
r.GET("/siteinfo/seo", a.siteInfoController.GetSeo)
r.PUT("/siteinfo/general", a.siteInfoController.UpdateGeneral) r.PUT("/siteinfo/general", a.siteInfoController.UpdateGeneral)
r.PUT("/siteinfo/interface", a.siteInfoController.UpdateInterface) r.PUT("/siteinfo/interface", a.siteInfoController.UpdateInterface)
r.PUT("/siteinfo/branding", a.siteInfoController.UpdateBranding) r.PUT("/siteinfo/branding", a.siteInfoController.UpdateBranding)
r.PUT("/siteinfo/write", a.siteInfoController.UpdateSiteWrite) r.PUT("/siteinfo/write", a.siteInfoController.UpdateSiteWrite)
r.PUT("/siteinfo/legal", a.siteInfoController.UpdateSiteLegal) r.PUT("/siteinfo/legal", a.siteInfoController.UpdateSiteLegal)
r.PUT("/siteinfo/seo", a.siteInfoController.UpdateSeo)
r.GET("/setting/smtp", a.siteInfoController.GetSMTPConfig) r.GET("/setting/smtp", a.siteInfoController.GetSMTPConfig)
r.PUT("/setting/smtp", a.siteInfoController.UpdateSMTPConfig) r.PUT("/setting/smtp", a.siteInfoController.UpdateSMTPConfig)

View File

@ -3,27 +3,34 @@ package router
import ( import (
"github.com/answerdev/answer/internal/controller" "github.com/answerdev/answer/internal/controller"
templaterender "github.com/answerdev/answer/internal/controller/template_render" templaterender "github.com/answerdev/answer/internal/controller/template_render"
"github.com/answerdev/answer/internal/controller_backyard"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type TemplateRouter struct { type TemplateRouter struct {
templateController *controller.TemplateController templateController *controller.TemplateController
templateRenderController *templaterender.TemplateRenderController templateRenderController *templaterender.TemplateRenderController
siteInfoController *controller_backyard.SiteInfoController
} }
func NewTemplateRouter( func NewTemplateRouter(
templateController *controller.TemplateController, templateController *controller.TemplateController,
templateRenderController *templaterender.TemplateRenderController, templateRenderController *templaterender.TemplateRenderController,
siteInfoController *controller_backyard.SiteInfoController,
) *TemplateRouter { ) *TemplateRouter {
return &TemplateRouter{ return &TemplateRouter{
templateController: templateController, templateController: templateController,
templateRenderController: templateRenderController, templateRenderController: templateRenderController,
siteInfoController: siteInfoController,
} }
} }
// TemplateRouter template router // TemplateRouter template router
func (a *TemplateRouter) RegisterTemplateRouter(r *gin.RouterGroup) { func (a *TemplateRouter) RegisterTemplateRouter(r *gin.RouterGroup) {
r.GET("/robots.txt", a.siteInfoController.GetRobots)
r.GET("/", a.templateController.Index) r.GET("/", a.templateController.Index)
r.GET("/index", a.templateController.Index) r.GET("/index", a.templateController.Index)

View File

@ -18,6 +18,10 @@ type SiteGeneralReq struct {
PermaLink int `validate:"required,lte=3,gte=0" form:"permalink" json:"permalink"` PermaLink int `validate:"required,lte=3,gte=0" form:"permalink" json:"permalink"`
} }
type SiteSeoReq struct {
Robots string `validate:"required" form:"robots" json:"robots"`
}
func (r *SiteGeneralReq) FormatSiteUrl() { func (r *SiteGeneralReq) FormatSiteUrl() {
parsedUrl, err := url.Parse(r.SiteUrl) parsedUrl, err := url.Parse(r.SiteUrl)
if err != nil { if err != nil {
@ -93,6 +97,8 @@ type SiteWriteResp SiteWriteReq
// SiteLegalResp site write response // SiteLegalResp site write response
type SiteLegalResp SiteLegalReq type SiteLegalResp SiteLegalReq
type SiteSeoResp SiteSeoReq
// SiteInfoResp get site info response // SiteInfoResp get site info response
type SiteInfoResp struct { type SiteInfoResp struct {
General *SiteGeneralResp `json:"general"` General *SiteGeneralResp `json:"general"`

View File

@ -241,3 +241,33 @@ func (s *SiteInfoService) UpdateSMTPConfig(ctx context.Context, req *schema.Upda
} }
return return
} }
func (s *SiteInfoService) GetSeo(ctx context.Context) (resp *schema.SiteSeoResp, err error) {
resp = &schema.SiteSeoResp{}
siteInfo, exist, err := s.siteInfoRepo.GetByType(ctx, constant.SiteTypeSeo)
if err != nil {
log.Error(err)
return resp, nil
}
if !exist {
return resp, nil
}
_ = json.Unmarshal([]byte(siteInfo.Content), resp)
return resp, nil
}
func (s *SiteInfoService) SaveSeo(ctx context.Context, req schema.SiteSeoReq) (err error) {
var (
siteType = constant.SiteTypeSeo
content []byte
)
content, _ = json.Marshal(req)
data := entity.SiteInfo{
Type: siteType,
Content: string(content),
}
err = s.siteInfoRepo.SaveByType(ctx, siteType, &data)
return
}