Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Filesystem middleware #1787

Closed
circa10a opened this issue Feb 20, 2021 · 3 comments
Closed

Feature Request: Filesystem middleware #1787

circa10a opened this issue Feb 20, 2021 · 3 comments
Assignees
Milestone

Comments

@circa10a
Copy link

circa10a commented Feb 20, 2021

Issue Description

It would be very nice to have a filesystem middleware supported by echo. Fiber currently has one implemented that allows much easier integration of the new go embed directive.

With fiber, integration looks like this with the middleware:

package main

import (
	"embed"
	"log"
	"net/http"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/filesystem"
)

//go:embed index.html
var f embed.FS

func main() {
	app := fiber.New()
	app.Use("/", filesystem.New(filesystem.Config{
		Root: http.FS(f),
	}))
	log.Fatal(app.Listen(":3000"))
}

And the current way to get this working with echo looks like this:

package main

import (
	"embed"
	"net/http"

	"github.com/labstack/echo/v4"
)

//go:embed index.html
var f embed.FS

func main() {
	e := echo.New()
	frontendHttpFs := http.FS(f)
	frontendFileServer := http.FileServer(frontendHttpFs)
	e.GET("/*", echo.WrapHandler(frontendFileServer))
	e.Logger.Fatal(e.Start(":3000"))
}

Curious on your thoughts on this

@maciej-jezierski
Copy link

There is currently static middleware with the option to browse: https://echo.labstack.com/middleware/static

@aldas
Copy link
Contributor

aldas commented Feb 20, 2021

Related to #1755

Currently for Echo same thing is achievable with following line if embed.FS is needed.

        //go:embed public/*
...
        var content embed.FS
...
        e.GET("/*", echo.WrapHandler(http.FileServer(http.FS(content))))

We probably should tweak static middleware so user can pass in their FS with other options.

@aldas
Copy link
Contributor

aldas commented Feb 27, 2021

So I created locally branch to add support for fs.FS to serve files. But there is a problem - currently adding support for fs.FS for File() and Static() routes and static middleware is not that simple. For 1.16 it is but when we want to support also 1.15 there are problems as io/fs package does not exist in 1.15 and even with build flags currently go tools are not very happy about it

see: golang/go#44557 and golang/go#40067

Lets wait a little

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants