add template

This commit is contained in:
aichy126 2022-11-29 18:02:42 +08:00
parent bb52bf50ed
commit 8e240b677e
7 changed files with 34 additions and 11 deletions

View File

@ -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
}

View File

@ -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,

View File

@ -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)

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.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>

View File

@ -7,3 +7,6 @@ import (
//go:embed build
var Build embed.FS
//go:embed template
var Template embed.FS

View File

@ -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>
</html>

View File

@ -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>
</html>