enhance: redirect to home if no route #87

This commit is contained in:
0xJacky 2023-03-13 21:51:28 +08:00
parent 445be37f4e
commit 394bd0e578
No known key found for this signature in database
GPG key ID: B6E4A6E4A561BAF0

View file

@ -1,12 +1,10 @@
package router
import (
"bufio"
"github.com/0xJacky/Nginx-UI/server/api"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func InitRouter() *gin.Engine {
@ -20,15 +18,7 @@ func InitRouter() *gin.Engine {
r.Use(static.Serve("/", mustFS("")))
r.NoRoute(func(c *gin.Context) {
accept := c.Request.Header.Get("Accept")
if strings.Contains(accept, "text/html") {
file, _ := mustFS("").Open("index.html")
defer file.Close()
stat, _ := file.Stat()
c.DataFromReader(http.StatusOK, stat.Size(), "text/html",
bufio.NewReader(file), nil)
return
}
c.Redirect(http.StatusMovedPermanently, "/")
})
root := r.Group("/api")