fix: 修复了当文件名为index.html时被golang贴心自动重定向的bug

This commit is contained in:
539943419 committed 2024-08-23 19:42:35 +08:00
1 parent 817603cade
commit 871906808c
1 file changed
+15 -3
+15 -3
View File
@@ -1,13 +1,15 @@
package api
import (
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"io/ioutil"
)
"strings"
"time"
"github.com/gin-gonic/gin"
)
func GetHandler(c *gin.Context) {
decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path")))
@@ -53,7 +55,17 @@ func GetHandler(c *gin.Context) {
} else {
c.JSON(http.StatusOK, fileDetails)
}
} else {
if strings.HasSuffix(decodedPath, "index.html") {
f, err := os.Open(decodedPath)
if err != nil {
c.Writer.WriteHeader(http.StatusInternalServerError)
c.Writer.Write([]byte("Internal Server Error"))
return
}
http.ServeContent(c.Writer, c.Request, "index.html", time.Now(), f)
} else {
c.File(decodedPath)
}
}
}