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/middleware"
rou "github.com/1Panel-dev/1Panel/backend/router"
"github.com/1Panel-dev/1Panel/cmd/server/web"
ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"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 {
Router := gin.Default()
Router.Use(middleware.CSRF())
Router.Use(middleware.LoadCsrfToken())
//Router.Use(middleware.CSRF())
//Router.Use(middleware.LoadCsrfToken())
setWebStatic(Router)
docs.SwaggerInfo.BasePath = "/api/v1"
Router.Use(i18n.GinI18nLocalize())

View file

@ -3,8 +3,6 @@ package server
import (
"encoding/gob"
"fmt"
"github.com/1Panel-dev/1Panel/cmd/server/web"
"net/http"
"time"
"github.com/1Panel-dev/1Panel/backend/cron"
@ -38,14 +36,6 @@ func Start() {
rootRouter := router.Routers()
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)
global.LOG.Infof("server run success on %d", global.CONF.System.Port)
if err := s.ListenAndServe(); err != nil {

View file

@ -7,3 +7,9 @@ var IndexHtml embed.FS
//go:embed assets/*
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({
history: createWebHistory('/kubepi/'),
history: createWebHistory('/1panel/'),
routes: routes as RouteRecordRaw[],
strict: false,
scrollBehavior: () => ({ left: 0, top: 0 }),