Merge branch 'feat/1.1.1/ai' into test

This commit is contained in:
aichy126 2023-07-13 12:00:51 +08:00
commit f2590a4272
3 changed files with 41 additions and 11 deletions

View File

@ -223,7 +223,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
shortIDMiddleware := middleware.NewShortIDMiddleware(siteInfoCommonService)
templateRenderController := templaterender.NewTemplateRenderController(questionService, userService, tagService, answerService, commentService, siteInfoCommonService, questionRepo)
templateController := controller.NewTemplateController(templateRenderController, siteInfoCommonService)
templateRouter := router.NewTemplateRouter(templateController, templateRenderController, siteInfoController)
templateRouter := router.NewTemplateRouter(templateController, templateRenderController, siteInfoController, authUserMiddleware)
connectorController := controller.NewConnectorController(siteInfoCommonService, emailService, userExternalLoginService)
userCenterLoginService := user_external_login2.NewUserCenterLoginService(userRepo, userCommon, userExternalLoginRepo, userActiveActivityRepo, siteInfoCommonService)
userCenterController := controller.NewUserCenterController(userCenterLoginService, siteInfoCommonService)

View File

@ -1,6 +1,7 @@
package middleware
import (
"net/http"
"strings"
"github.com/answerdev/answer/internal/schema"
@ -140,6 +141,28 @@ func (am *AuthUserMiddleware) AdminAuth() gin.HandlerFunc {
}
}
func (am *AuthUserMiddleware) CheckPrivateMode() gin.HandlerFunc {
return func(ctx *gin.Context) {
userLoginUrl := "/users/login"
if ctx.Request.URL.Path == userLoginUrl {
ctx.Next()
return
}
resp, err := am.siteInfoCommonService.GetSiteLogin(ctx)
if err != nil {
ctx.Redirect(http.StatusTemporaryRedirect, userLoginUrl)
ctx.Abort()
return
}
if resp.LoginRequired {
ctx.Redirect(http.StatusTemporaryRedirect, userLoginUrl)
ctx.Abort()
return
}
ctx.Next()
}
}
// GetLoginUserIDFromContext get user id from context
func GetLoginUserIDFromContext(ctx *gin.Context) (userID string) {
userInfo := GetUserInfoFromContext(ctx)

View File

@ -1,6 +1,7 @@
package router
import (
"github.com/answerdev/answer/internal/base/middleware"
"github.com/answerdev/answer/internal/controller"
templaterender "github.com/answerdev/answer/internal/controller/template_render"
"github.com/answerdev/answer/internal/controller_admin"
@ -11,17 +12,21 @@ type TemplateRouter struct {
templateController *controller.TemplateController
templateRenderController *templaterender.TemplateRenderController
siteInfoController *controller_admin.SiteInfoController
authUserMiddleware *middleware.AuthUserMiddleware
}
func NewTemplateRouter(
templateController *controller.TemplateController,
templateRenderController *templaterender.TemplateRenderController,
siteInfoController *controller_admin.SiteInfoController,
authUserMiddleware *middleware.AuthUserMiddleware,
) *TemplateRouter {
return &TemplateRouter{
templateController: templateController,
templateRenderController: templateRenderController,
siteInfoController: siteInfoController,
authUserMiddleware: authUserMiddleware,
}
}
@ -33,15 +38,17 @@ func (a *TemplateRouter) RegisterTemplateRouter(r *gin.RouterGroup) {
r.GET("/robots.txt", a.siteInfoController.GetRobots)
r.GET("/custom.css", a.siteInfoController.GetCss)
r.GET("/", a.templateController.Index)
r.GET("/questions", a.templateController.QuestionList)
r.GET("/questions/:id", a.templateController.QuestionInfo)
r.GET("/questions/:id/:title", a.templateController.QuestionInfo)
r.GET("/questions/:id/:title/:answerid", a.templateController.QuestionInfo)
r.GET("/tags", a.templateController.TagList)
r.GET("/tags/:tag", a.templateController.TagInfo)
r.GET("/users/:username", a.templateController.UserInfo)
r.GET("/404", a.templateController.Page404)
//todo add middleware
seo := r.Group("")
seo.Use(a.authUserMiddleware.CheckPrivateMode())
seo.GET("/", a.templateController.Index)
seo.GET("/questions", a.templateController.QuestionList)
seo.GET("/questions/:id", a.templateController.QuestionInfo)
seo.GET("/questions/:id/:title", a.templateController.QuestionInfo)
seo.GET("/questions/:id/:title/:answerid", a.templateController.QuestionInfo)
seo.GET("/tags", a.templateController.TagList)
seo.GET("/tags/:tag", a.templateController.TagInfo)
seo.GET("/users/:username", a.templateController.UserInfo)
}