update template

This commit is contained in:
aichy126 2022-11-29 17:20:28 +08:00
parent bf41dec058
commit 9839272d95
5 changed files with 103 additions and 7 deletions

View File

@ -189,7 +189,9 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
uiRouter := router.NewUIRouter()
authUserMiddleware := middleware.NewAuthUserMiddleware(authService)
avatarMiddleware := middleware.NewAvatarMiddleware(serviceConf, uploaderService)
ginEngine := server.NewHTTPServer(debug, staticRouter, answerAPIRouter, swaggerRouter, uiRouter, authUserMiddleware, avatarMiddleware)
templateController := controller.NewTemplateController()
templateRouter := router.NewTemplateRouter(templateController)
ginEngine := server.NewHTTPServer(debug, staticRouter, answerAPIRouter, swaggerRouter, uiRouter, authUserMiddleware, avatarMiddleware, templateRouter)
application := newApplication(serverConf, ginEngine)
return application, func() {
cleanup2()

View File

@ -15,6 +15,7 @@ func NewHTTPServer(debug bool,
viewRouter *router.UIRouter,
authUserMiddleware *middleware.AuthUserMiddleware,
avatarMiddleware *middleware.AvatarMiddleware,
templateRouter *router.TemplateRouter,
) *gin.Engine {
if debug {
@ -30,6 +31,7 @@ func NewHTTPServer(debug bool,
rootGroup := r.Group("")
swaggerRouter.Register(rootGroup)
templateRouter.RegisterTemplateRouter(rootGroup)
static := r.Group("")
static.Use(avatarMiddleware.AvatarThumb())
staticRouter.RegisterStaticRouter(static)

View File

@ -1,11 +1,88 @@
package controller
import (
"net/http"
"regexp"
"github.com/answerdev/answer/ui"
"github.com/gin-gonic/gin"
)
type TemplateController struct {
scriptPath string
cssPath string
}
// NewTemplateController new controller
func NewTemplateController() *TemplateController {
return &TemplateController{}
script, css := GetStyle()
return &TemplateController{
scriptPath: script,
cssPath: css,
}
}
func GetStyle() (script, css string) {
file, err := ui.Build.ReadFile("build/index.html")
if err != nil {
return
}
scriptRegexp := regexp.MustCompile(`<script defer="defer" src="(.*)"></script>`)
scriptData := scriptRegexp.FindStringSubmatch(string(file))
cssRegexp := regexp.MustCompile(`<link href="(.*)" rel="stylesheet">`)
cssListData := cssRegexp.FindStringSubmatch(string(file))
if len(scriptData) == 2 {
script = scriptData[1]
}
if len(cssListData) == 2 {
css = cssListData[1]
}
return
}
// func (tc *TemplateController) SearchTagLike(ctx *gin.Context) {
// Index question list
func (tc *TemplateController) Index(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"scriptPath": tc.scriptPath,
"cssPath": tc.cssPath,
})
}
// QuestionInfo question and answers info
func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
id := ctx.Param("id")
answerid := ctx.Param("answerid")
ctx.JSON(http.StatusOK, gin.H{
"id": id,
"answerid": answerid,
"scriptPath": tc.scriptPath,
"cssPath": tc.cssPath,
})
}
// TagList tags list
func (tc *TemplateController) TagList(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"scriptPath": tc.scriptPath,
"cssPath": tc.cssPath,
})
}
// TagInfo taginfo
func (tc *TemplateController) TagInfo(ctx *gin.Context) {
tag := ctx.Param("tag")
ctx.JSON(http.StatusOK, gin.H{
"tag": tag,
"scriptPath": tc.scriptPath,
"cssPath": tc.cssPath,
})
}
// UserInfo user info
func (tc *TemplateController) UserInfo(ctx *gin.Context) {
username := ctx.Param("username")
ctx.JSON(http.StatusOK, gin.H{
"username": username,
"scriptPath": tc.scriptPath,
"cssPath": tc.cssPath,
})
}

View File

@ -1,16 +1,31 @@
package router
import (
"github.com/answerdev/answer/internal/controller"
"github.com/gin-gonic/gin"
)
type TemplateRouter struct {
templateController *controller.TemplateController
}
func NewTemplateRouter() *TemplateRouter {
return &TemplateRouter{}
func NewTemplateRouter(
templateController *controller.TemplateController,
) *TemplateRouter {
return &TemplateRouter{
templateController: templateController,
}
}
// TemplateRouter template router
func (a *TemplateRouter) TemplateRouter(r *gin.RouterGroup) {
func (a *TemplateRouter) RegisterTemplateRouter(r *gin.RouterGroup) {
r.GET("/", a.templateController.Index)
r.GET("/index", a.templateController.Index)
r.GET("/questions", a.templateController.Index)
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)
}

View File

@ -1 +1 @@
<!doctype html><html><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><link rel="manifest" href="/manifest.json"/><title>Answer</title><script defer="defer" src="/static/js/main.1bed8401.js"></script><link href="/static/css/main.6975b122.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
<!doctype html><html><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><link rel="manifest" href="/manifest.json"/><title>Answer</title><script defer="defer" src="/static/js/main.1bed8401.js"></script><link href="/static/css/main.6975b122.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>