diff --git a/go.mod b/go.mod index e4b5602..be37580 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/bogdanfinn/tls-client v1.7.8 github.com/btcsuite/btcutil v1.0.2 github.com/gin-contrib/cors v1.7.2 + github.com/gin-contrib/multitemplate v1.0.1 github.com/gin-gonic/gin v1.10.0 github.com/redis/go-redis/v9 v9.7.0 github.com/robfig/cron/v3 v3.0.1 @@ -40,7 +41,6 @@ require ( github.com/cloudwego/iasm v0.2.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/gabriel-vasile/mimetype v1.4.6 // indirect - github.com/gin-contrib/multitemplate v1.0.1 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/jsonreference v0.21.0 // indirect diff --git a/server/route.go b/server/route.go index 172a0ce..3c1dadf 100644 --- a/server/route.go +++ b/server/route.go @@ -1,6 +1,10 @@ package server import ( + "embed" + "errors" + "io/fs" + "net/http" "path/filepath" "pcgamedb/crawler" "pcgamedb/db" @@ -8,6 +12,7 @@ import ( "pcgamedb/server/handler" "pcgamedb/server/middleware" "strconv" + "strings" "github.com/gin-contrib/cors" "github.com/gin-contrib/multitemplate" @@ -21,6 +26,9 @@ import ( ginSwagger "github.com/swaggo/gin-swagger" ) +//go:embed templates templates/layouts static +var frontendFS embed.FS + func initRoute(app *gin.Engine) { app.Use(cors.New(cors.Config{ AllowAllOrigins: true, @@ -31,30 +39,49 @@ func initRoute(app *gin.Engine) { } func initFrontend(app *gin.Engine) { + // Load static files + staticFs, err := fs.Sub(frontendFS, "static") + if err != nil { + log.Logger.Fatal("Error loading static files", zap.Error(err)) + return + } + app.StaticFS("/static", http.FS(staticFs)) + // Load templates + // directly using templates app.LoadHTMLFiles() to load all templates leads to error + // because use templates with same name in different html file will case overridden + // so we need to load all templates manually and use multitemplate to render them r := multitemplate.NewRenderer() - app.Static("/static", "server/static") - - layoutFiles, err := filepath.Glob("server/templates/layouts/*.html") + layoutFiles, err := frontendFS.ReadDir("templates/layouts") if err != nil { log.Logger.Fatal("Error loading layout templates", zap.Error(err)) return } - rootFiles, err := filepath.Glob("server/templates/*.html") + rootFiles, err := frontendFS.ReadDir("templates") if err != nil { log.Logger.Fatal("Error loading root templates", zap.Error(err)) return } for _, rootFile := range rootFiles { - name := filepath.Base(rootFile) - r.AddFromFiles(name, append([]string{rootFile}, layoutFiles...)...) + if rootFile.IsDir() { + continue + } + name := filepath.Base(rootFile.Name()) + templateFiles := []string{"templates/" + name} + for _, layout := range layoutFiles { + if !layout.IsDir() { + templateFiles = append(templateFiles, "templates/layouts/"+layout.Name()) + } + } + r.AddFromFS(name, frontendFS, templateFiles...) } app.HTMLRender = r + // Load routes app.GET("/", func(ctx *gin.Context) { monthTop, err := crawler.GetSteam250MonthTop50Cache() if err != nil { @@ -102,8 +129,9 @@ func initFrontend(app *gin.Engine) { app.GET("/search", func(ctx *gin.Context) { key := ctx.Query("key") page := ctx.Query("page") + key = strings.TrimSpace(key) if len(key) < 2 { - ctx.HTML(400, "400.html", nil) + ctx.HTML(400, "400.html", errors.New("Search key should be at least 2 characters long")) return } if page == "" { @@ -111,12 +139,7 @@ func initFrontend(app *gin.Engine) { } pageInt, err := strconv.Atoi(page) if err != nil { - ctx.HTML(400, "400.html", nil) - return - } - - if key == "" { - ctx.HTML(400, "400.html", nil) + ctx.HTML(400, "400.html", err) return } games, totalPage, err := db.SearchGameInfos(key, pageInt, 10) diff --git a/server/static/favicon.png b/server/static/favicon.png new file mode 100644 index 0000000..945d558 Binary files /dev/null and b/server/static/favicon.png differ diff --git a/server/templates/400.html b/server/templates/400.html new file mode 100644 index 0000000..5610811 --- /dev/null +++ b/server/templates/400.html @@ -0,0 +1,7 @@ +{{template "base" .}} +{{define "title"}}Bad Request{{end}} +{{define "styles"}} {{end}} +{{define "content"}} +Bad Request +{{.}} +{{end}} \ No newline at end of file diff --git a/server/templates/game.html b/server/templates/game.html index 09778ca..7018e16 100644 --- a/server/templates/game.html +++ b/server/templates/game.html @@ -1,6 +1,6 @@ {{template "base" .}} -{{define "title"}}{{.Name}} - 游戏详情{{end}} +{{define "title"}}{{.Name}} - Details{{end}} {{define "styles"}}