diff --git a/internal/base/server/http.go b/internal/base/server/http.go index 82eeb293..aeb11c5c 100644 --- a/internal/base/server/http.go +++ b/internal/base/server/http.go @@ -1,9 +1,14 @@ package server import ( + "html/template" + "io/fs" + "os" + brotli "github.com/anargu/gin-brotli" "github.com/answerdev/answer/internal/base/middleware" "github.com/answerdev/answer/internal/router" + "github.com/answerdev/answer/ui" "github.com/gin-gonic/gin" ) @@ -31,7 +36,6 @@ func NewHTTPServer(debug bool, rootGroup := r.Group("") swaggerRouter.Register(rootGroup) - templateRouter.RegisterTemplateRouter(rootGroup) static := r.Group("") static.Use(avatarMiddleware.AvatarThumb()) staticRouter.RegisterStaticRouter(static) @@ -50,5 +54,15 @@ func NewHTTPServer(debug bool, cmsauthV1.Use(authUserMiddleware.CmsAuth()) answerRouter.RegisterAnswerCmsAPIRouter(cmsauthV1) + dev := os.Getenv("DEVCODE") + if dev != "" { + r.LoadHTMLGlob("../../ui/template/*") + } else { + html, _ := fs.Sub(ui.Template, "template") + htmlTemplate := template.Must(template.New("").ParseFS(html, "*.html")) + r.SetHTMLTemplate(htmlTemplate) + } + + templateRouter.RegisterTemplateRouter(rootGroup) return r } diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go index 84810397..7361f0d2 100644 --- a/internal/controller/template_controller.go +++ b/internal/controller/template_controller.go @@ -41,7 +41,7 @@ func GetStyle() (script, css string) { // Index question list func (tc *TemplateController) Index(ctx *gin.Context) { - ctx.JSON(http.StatusOK, gin.H{ + ctx.HTML(http.StatusOK, "question.html", gin.H{ "scriptPath": tc.scriptPath, "cssPath": tc.cssPath, }) @@ -51,7 +51,7 @@ func (tc *TemplateController) Index(ctx *gin.Context) { func (tc *TemplateController) QuestionInfo(ctx *gin.Context) { id := ctx.Param("id") answerid := ctx.Param("answerid") - ctx.JSON(http.StatusOK, gin.H{ + ctx.HTML(http.StatusOK, "question-detail.html", gin.H{ "id": id, "answerid": answerid, "scriptPath": tc.scriptPath, @@ -61,7 +61,7 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) { // TagList tags list func (tc *TemplateController) TagList(ctx *gin.Context) { - ctx.JSON(http.StatusOK, gin.H{ + ctx.HTML(http.StatusOK, "tags.html", gin.H{ "scriptPath": tc.scriptPath, "cssPath": tc.cssPath, }) @@ -70,7 +70,7 @@ func (tc *TemplateController) TagList(ctx *gin.Context) { // TagInfo taginfo func (tc *TemplateController) TagInfo(ctx *gin.Context) { tag := ctx.Param("tag") - ctx.JSON(http.StatusOK, gin.H{ + ctx.HTML(http.StatusOK, "tag-detail.html", gin.H{ "tag": tag, "scriptPath": tc.scriptPath, "cssPath": tc.cssPath, @@ -80,7 +80,7 @@ func (tc *TemplateController) TagInfo(ctx *gin.Context) { // UserInfo user info func (tc *TemplateController) UserInfo(ctx *gin.Context) { username := ctx.Param("username") - ctx.JSON(http.StatusOK, gin.H{ + ctx.HTML(http.StatusOK, "homepage.html", gin.H{ "username": username, "scriptPath": tc.scriptPath, "cssPath": tc.cssPath, diff --git a/internal/router/template_router.go b/internal/router/template_router.go index 4ccedb43..6ee28746 100644 --- a/internal/router/template_router.go +++ b/internal/router/template_router.go @@ -19,6 +19,7 @@ func NewTemplateRouter( // TemplateRouter template router func (a *TemplateRouter) RegisterTemplateRouter(r *gin.RouterGroup) { + r.GET("/", a.templateController.Index) r.GET("/index", a.templateController.Index) r.GET("/questions", a.templateController.Index) diff --git a/ui/build/index.html b/ui/build/index.html index 33e11053..d07af796 100644 --- a/ui/build/index.html +++ b/ui/build/index.html @@ -1 +1 @@ -Answer
+Answer
diff --git a/ui/static.go b/ui/static.go index 74002b6b..a78eef7e 100644 --- a/ui/static.go +++ b/ui/static.go @@ -7,3 +7,6 @@ import ( //go:embed build var Build embed.FS + +//go:embed template +var Template embed.FS diff --git a/ui/template/homepage.html b/ui/template/homepage.html index bcc3f051..a49ec341 100644 --- a/ui/template/homepage.html +++ b/ui/template/homepage.html @@ -4,7 +4,9 @@ - + + + @@ -104,4 +106,4 @@ - \ No newline at end of file + diff --git a/ui/template/question.html b/ui/template/question.html index f2e1135a..757753a9 100644 --- a/ui/template/question.html +++ b/ui/template/question.html @@ -4,10 +4,13 @@ - + + + + 111
- \ No newline at end of file +