Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

add template

aichy1268e240b677eda
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 @@
-<!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.1e1d1dd2.js"></script><link href="/static/css/main.a1082d9a.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
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 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
- <link href="https://answer.dev.segmentfault.com/static/css/main.d4180d41.css" rel="stylesheet" />
+ <link rel="manifest" href="/manifest.json"/>
+ <link href="{{.cssPath}}" rel="stylesheet" />
+ <script defer="defer" src="{{.scriptPath}}"></script>
</head>
<body>
@@ -104,4 +106,4 @@
</div>
</body>
-</html>
\ No newline at end of file
+</html>
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 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
- <link href="https://answer.dev.segmentfault.com/static/css/main.d4180d41.css" rel="stylesheet" />
+ <link rel="manifest" href="/manifest.json"/>
+ <link href="{{.cssPath}}" rel="stylesheet" />
+ <!-- <script defer="defer" src="{{.scriptPath}}"></script> -->
</head>
<body>
+ 111
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root">
<nav id="header" class="sticky-top navbar navbar-expand-lg navbar-dark">
@@ -127,4 +130,4 @@
</div>
</body>
-</html>
\ No newline at end of file
+</html>