fix: 修复了当文件名为index.html时被golang贴心自动重定向的bug
This commit is contained in:
1 parent
817603cade
commit
871906808c
1 file changed
+15
-3
+15
-3
@@ -1,13 +1,15 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"io/ioutil"
|
"strings"
|
||||||
)
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
func GetHandler(c *gin.Context) {
|
func GetHandler(c *gin.Context) {
|
||||||
decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path")))
|
decodedPath, err := filepath.Abs(filepath.Join(staticPath, c.Param("path")))
|
||||||
@@ -53,7 +55,17 @@ func GetHandler(c *gin.Context) {
|
|||||||
} else {
|
} else {
|
||||||
c.JSON(http.StatusOK, fileDetails)
|
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 {
|
} else {
|
||||||
c.File(decodedPath)
|
c.File(decodedPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user