Echo: [v3 question] - Serving static content

Created on 2 Feb 2017  ·  22Comments  ·  Source: labstack/echo

I have asked this on the gitter chat, but there was no response, so I thought I'd open an issue for it.

Here's the text I posted there:

Any way to serve static from two different folders? In v2 we could do:

    e.Use(m.Static("")) // serves from root
    e.Use(m.Static("client/dev")) // serves from client/dev

and in the index.html you could have:

    <link href="index.css" rel="stylesheet"/> <!-- in client/dev -->
    <link href="index2.css" rel="stylesheet"/> <!-- in the root -->

With v3 I'm trying something like:

    e := echo.New()

    e.Static("/", "")
    e.Static("/", "client/dev")

But it only serves stuff from the client/dev - because it's the last one being declared, I guess. So, any way to get that to work in v3?

Here's a repo trying out v3 - without success: https://github.com/ericmdantas/echo-static

Thanks in advance.

question

Most helpful comment

@reilg can you share your structure? Because you might want e.Static("/", "public") instead of e.Static("/public", "p").

Here's an working example:

package main

import (
    "net/http"
    "github.com/labstack/echo"
)

func main() {
    e := echo.New() 

    e.Static("/", "public")

    e.Logger.Fatal(e.Start(":1234"))
}
<!doctype html>
<html>
    <head>
        <link type="text/css" rel="stylesheet" href="./assets/style.css" />
        <title>test</title>
    </head>
    <body>
        <h1>!</h1>
    </body>
</html>

Structure:

/
main.go
public/
├── index.html
└── assets/
        └── style.css

All 22 comments

So, I was just taking a look at https://github.com/labstack/armor/blob/master/plugin/static.go and, if I remember correctly, this is pretty much what echo's v2 used to do.

@ericmdantas I have added static middleware in master, can you check if it covers all your cases?

Sure, I'll take a look tomorrow morning.

Thank you for all the hard work, @vishr.

Seems to be working perfectly. Thanks once again!

@ericmdantas Awesome!

Hmm, actually, it's not working right now.

Given the server:

package main

import (
    "fmt"

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

const port = ":3000"

func main() {
    e := echo.New()

    e.Use(middleware.Static(""))
    e.Use(middleware.Static("client/dev"))

    e.GET("/api", func(c echo.Context) error {
        return c.String(200, "1")
    })

    fmt.Println(port)

    if err := e.Start(port); err != nil {
        panic(err)
    }
}

After running go run main.go, and navigating to http://localhost:3000/api, you'll get a page with the error:

{"message":"Not Found"}

Even if you try to access the /api, you'll also the the error above.

Here's the updated repo: https://github.com/ericmdantas/echo-static

Let me know if you'd like me to open another issue to cover this.

@ericmdantas Can you verify this?

Thanks for the quick fix, @vishr.

Now the /api works, but the static serving doesn't. I've run both go get -u and deleted the echo folder and installed it again - no luck.

@ericmdantas Your html code makes requests to the following files:

GET http://localhost:3000/client/index.css
GET http://localhost:3000/client/index2.css

Which doesn't exists, so if you include them with / - it works.

The problem I'm seeing is that the index.html is not being served.

The following works:

  • http://localhost:3000/api - shows 1
  • http://localhost:3000/index2.css - the css that lives in the root
  • http://localhost:3000/client/dev/index.css - the css that lives in the client/dev

The following doesn't work:

  • Opening the app doesn't show the index.html and its dependencies.

To get it to work I had to add the following snippet (which is before the middleware.Static calls):

    e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
        Root:  "client/dev",
    }))

Just pointing it out, I'm ok if that's the new way to get it to work.

Edit: Updated snippet

@ericmdantas Can you check with the very latest commit. For me it works.

@vishr, nope - doesn't work.

I ran go get -u, removed the labstack folder and ran go get, and it still won't serve the index.html.

This works:

    e := echo.New()

    e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
        Root: "client/dev",
    }))

    e.Use(middleware.Static(""))
    e.Use(middleware.Static("client/dev"))

This doesn't:

    e := echo.New()

    e.Use(middleware.Static(""))
    e.Use(middleware.Static("client/dev"))

looking into it.

Not sure if it helps, but I'm on Windows, by the way.

I have that problem too - env issues.

@ericmdantas It should be fixed now, please verify.

Working as expected, thanks, @vishr!

Can someone please confirm that echo.Static() this is working for them?

I have a very simple implementation that doesn't seem to be working.

e := echo.New()
e.Static("/public", "p")

I save an image file in public and then try to load it on the browser:
http://localhost:8080/p/img.png. I'm getting message: Not Found.

@reilg can you share your structure? Because you might want e.Static("/", "public") instead of e.Static("/public", "p").

Here's an working example:

package main

import (
    "net/http"
    "github.com/labstack/echo"
)

func main() {
    e := echo.New() 

    e.Static("/", "public")

    e.Logger.Fatal(e.Start(":1234"))
}
<!doctype html>
<html>
    <head>
        <link type="text/css" rel="stylesheet" href="./assets/style.css" />
        <title>test</title>
    </head>
    <body>
        <h1>!</h1>
    </body>
</html>

Structure:

/
main.go
public/
├── index.html
└── assets/
        └── style.css

@ericmdantas
Structure:

main.go
app/
 - app.go
public/
  - img.png
  - otherimg.png

Might be worth noting that echo.Static() is being called in app/app.go
Does that mean I need to move my public/ folder in app/?

About my issue; I'm trying to expose this folder so I can serve images in http://site.com/p

@ericmdantas thank you very much, your example is working!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

leoycx picture leoycx  ·  4Comments

spielstein picture spielstein  ·  3Comments

linux-support picture linux-support  ·  3Comments

arun0009 picture arun0009  ·  3Comments

montanaflynn picture montanaflynn  ·  3Comments