feat: 解决刷新404的问题

This commit is contained in:
zhengkunwang223 2022-10-17 17:13:47 +08:00 committed by zhengkunwang223
parent 115b75c1cb
commit 31990c7735
4 changed files with 32 additions and 13 deletions

View File

@ -6,18 +6,41 @@ import (
"github.com/1Panel-dev/1Panel/backend/i18n" "github.com/1Panel-dev/1Panel/backend/i18n"
"github.com/1Panel-dev/1Panel/backend/middleware" "github.com/1Panel-dev/1Panel/backend/middleware"
rou "github.com/1Panel-dev/1Panel/backend/router" rou "github.com/1Panel-dev/1Panel/backend/router"
"github.com/1Panel-dev/1Panel/cmd/server/web"
ginI18n "github.com/gin-contrib/i18n" ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files" swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger" ginSwagger "github.com/swaggo/gin-swagger"
"html/template" "html/template"
"net/http"
) )
func setWebStatic(rootRouter *gin.Engine) {
rootRouter.StaticFS("/kubepi/login/onepanel", http.FS(web.IndexHtml))
rootRouter.StaticFS("/favicon.ico", http.FS(web.Favicon))
rootRouter.GET("/assets/*filepath", func(c *gin.Context) {
staticServer := http.FileServer(http.FS(web.Assets))
staticServer.ServeHTTP(c.Writer, c.Request)
})
rootRouter.GET("/", func(c *gin.Context) {
staticServer := http.FileServer(http.FS(web.IndexHtml))
staticServer.ServeHTTP(c.Writer, c.Request)
})
rootRouter.NoRoute(func(c *gin.Context) {
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write(web.IndexByte)
c.Writer.Header().Add("Accept", "text/html")
c.Writer.Flush()
})
}
func Routers() *gin.Engine { func Routers() *gin.Engine {
Router := gin.Default() Router := gin.Default()
Router.Use(middleware.CSRF()) //Router.Use(middleware.CSRF())
Router.Use(middleware.LoadCsrfToken()) //Router.Use(middleware.LoadCsrfToken())
setWebStatic(Router)
docs.SwaggerInfo.BasePath = "/api/v1" docs.SwaggerInfo.BasePath = "/api/v1"
Router.Use(i18n.GinI18nLocalize()) Router.Use(i18n.GinI18nLocalize())

View File

@ -3,8 +3,6 @@ package server
import ( import (
"encoding/gob" "encoding/gob"
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/cmd/server/web"
"net/http"
"time" "time"
"github.com/1Panel-dev/1Panel/backend/cron" "github.com/1Panel-dev/1Panel/backend/cron"
@ -38,14 +36,6 @@ func Start() {
rootRouter := router.Routers() rootRouter := router.Routers()
address := fmt.Sprintf(":%d", global.CONF.System.Port) address := fmt.Sprintf(":%d", global.CONF.System.Port)
rootRouter.StaticFS("/login/onepanel", http.FS(web.IndexHtml))
rootRouter.StaticFS("/1panel", http.FS(web.IndexHtml))
rootRouter.GET("/assets/*filepath", func(c *gin.Context) {
staticServer := http.FileServer(http.FS(web.Assets))
staticServer.ServeHTTP(c.Writer, c.Request)
})
s := initServer(address, rootRouter) s := initServer(address, rootRouter)
global.LOG.Infof("server run success on %d", global.CONF.System.Port) global.LOG.Infof("server run success on %d", global.CONF.System.Port)
if err := s.ListenAndServe(); err != nil { if err := s.ListenAndServe(); err != nil {

View File

@ -7,3 +7,9 @@ var IndexHtml embed.FS
//go:embed assets/* //go:embed assets/*
var Assets embed.FS var Assets embed.FS
//go:embed index.html
var IndexByte []byte
//go:embed favicon.ico
var Favicon embed.FS

View File

@ -71,7 +71,7 @@ export const routes: RouteRecordRaw[] = [
}, },
]; ];
const router = createRouter({ const router = createRouter({
history: createWebHistory('/kubepi/'), history: createWebHistory('/1panel/'),
routes: routes as RouteRecordRaw[], routes: routes as RouteRecordRaw[],
strict: false, strict: false,
scrollBehavior: () => ({ left: 0, top: 0 }), scrollBehavior: () => ({ left: 0, top: 0 }),