Merge branch 'feat/0.7.0/seo' into test

This commit is contained in:
aichy126 2022-12-18 00:33:35 +08:00
commit 703aacf0cf
3 changed files with 39 additions and 5 deletions

View File

@ -204,7 +204,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
roleController := controller_backyard.NewRoleController(roleService)
answerAPIRouter := router.NewAnswerAPIRouter(langController, userController, commentController, reportController, voteController, tagController, followController, collectionController, questionController, answerController, searchController, revisionController, rankController, controller_backyardReportController, userBackyardController, reasonController, themeController, siteInfoController, siteinfoController, notificationController, dashboardController, uploadController, activityController, roleController)
swaggerRouter := router.NewSwaggerRouter(swaggerConf)
uiRouter := router.NewUIRouter(siteinfoController)
uiRouter := router.NewUIRouter(siteinfoController, siteInfoCommonService)
authUserMiddleware := middleware.NewAuthUserMiddleware(authService, siteInfoCommonService)
avatarMiddleware := middleware.NewAvatarMiddleware(serviceConf, uploaderService)
templateRenderController := templaterender.NewTemplateRenderController(questionService, userService, tagService, answerService, commentService, dataData, siteInfoCommonService)

View File

@ -8,6 +8,8 @@ import (
"os"
"github.com/answerdev/answer/internal/controller"
"github.com/answerdev/answer/internal/service/siteinfo_common"
"github.com/answerdev/answer/pkg/htmltext"
"github.com/answerdev/answer/ui"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/log"
@ -20,11 +22,18 @@ const UIStaticPath = "build/static"
// UIRouter is an interface that provides ui static file routers
type UIRouter struct {
siteInfoController *controller.SiteinfoController
siteInfoService *siteinfo_common.SiteInfoCommonService
}
// NewUIRouter creates a new UIRouter instance with the embed resources
func NewUIRouter(siteInfoController *controller.SiteinfoController) *UIRouter {
return &UIRouter{siteInfoController: siteInfoController}
func NewUIRouter(
siteInfoController *controller.SiteinfoController,
siteInfoService *siteinfo_common.SiteInfoCommonService,
) *UIRouter {
return &UIRouter{
siteInfoController: siteInfoController,
siteInfoService: siteInfoService,
}
}
// _resource is an interface that provides static file, it's a private interface
@ -74,8 +83,18 @@ func (a *UIRouter) Register(r *gin.Engine) {
filePath := ""
switch urlPath {
case "/favicon.ico":
c.Header("content-type", "image/vnd.microsoft.icon")
filePath = UIRootFilePath + urlPath
branding, err := a.siteInfoService.GetSiteBranding(c)
if err != nil {
log.Error(err)
}
if branding.Favicon != "" {
c.String(http.StatusOK, htmltext.GetPicByUrl(branding.Favicon))
return
} else {
c.Header("content-type", "image/vnd.microsoft.icon")
filePath = UIRootFilePath + urlPath
}
case "/manifest.json":
// filePath = UIRootFilePath + urlPath
a.siteInfoController.GetManifestJson(c)

View File

@ -1,6 +1,8 @@
package htmltext
import (
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
@ -80,3 +82,16 @@ func FetchExcerpt(html, trimMarker string, limit int) (text string) {
text += trimMarker
return
}
func GetPicByUrl(Url string) string {
res, err := http.Get(Url)
if err != nil {
return ""
}
defer res.Body.Close()
pix, err := ioutil.ReadAll(res.Body)
if err != nil {
return ""
}
return string(pix)
}