gin icon indicating copy to clipboard operation
gin copied to clipboard

Cannot use (*gin.Context).FileFromFS("index.html", fs) to serve any file named index.html

Open Tnze opened this issue 3 years ago • 5 comments

Description

It's not possible to serve any static file named index.html in a FileSystem.

How to reproduce

// The project fold:
// +-- www/
// |  +-- css/
// |  +-- src/
// |  |  +-- index.js
// |  +-- index.html
// +-- server.go <- This file

package main

import (
	"log"
	"github.com/gin-gonic/gin"
)

//go:embed www
var static embed.FS

func main() {
	r := gin.Default()
	r.StaticFS("/css", mustStaticFS("www/css"))
	r.StaticFS("/src", mustStaticFS("www/src"))
	r.Any("/", func(c *gin.Context) {
		c.FileFromFS("www/index.html", http.FS(static))
	})
	if err := r.Run(); err != nil {
		log.Panic(err)
	}
}

Expectations

$ curl http://localhost:8080/ # Expect with status 200
<html>the content of www/index.html</html>

Actual result

Infinite 301 redirection

[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"
[GIN] 2021/03/14 - 20:17:18 | 301 |            0s |       127.0.0.1 | GET      "/"

Environment

  • go version: 1.16
  • gin version (or commit ref): v1.6.3
  • operating system: Windows

Tnze avatar Mar 14 '21 12:03 Tnze