GhostVision
GhostVision

Reputation: 106

Serving static files with echo middleware and embed

project folder
->templates
  ->t.tmpl
->examples
  ->html
    ->blocks
      ->...
    ->assets
      ->...
    ->index.html

im t.tmpl there is call to <link rel="stylesheet" href="./blocks/index.css"> which in turn calls to different static files in examples/html/blocks/ and examples/html/assets

I am using echo with such setup:

   
   e.Static("/i/blocks", "examples/html/blocks")
   e.Static("/i/assets", "examples/html/assets")
   e.Static("/i/blocks/body/examples/html/assets", "examples/html/assets")
   e.Static("/i/blocks/logo/examples/html/assets", "examples/html/assets")

   v1 := s.e.Group("/i")

   v1.GET("/:page-id", s.handlePageDetails)

it works as intended, BUT

I would like to make it simpler and not hardcoded.

Was hoping to solve it like so:

    e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
        Root:       "examples/html",
        Browse:     true,
        IgnoreBase: true,
     })) 

It seems to work locally, but then it turned out that probably e.Static cached or something, because it stopped working after i restarted IDE during update.

So back to square 1.

At the end want something like this(if it is possible) to work:

e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
        Root:       "examples/html",
        Browse:     true,
        IgnoreBase: true,
        Filesystem: http.FS(project.StaticFiles),
     }))

where in some file static.go in the root of the project i have

//go:embed examples/html
var StaticFiles embed.FS

Not using Echo is not an option. If this can not be achieved through middleware.StaticWithConfig then other options are welcome as long as it doesn't look like what I have with bunch of e.Static for every path possibilities.

Also. The way it works. So there's a Get request to http://localhost:8080/i/:some-id - it calls on template, template calls on statics, those requests look like i/blocks and such, that is why i'm so specific with e.statics, to remove that /i/ part

Upvotes: 2

Views: 3038

Answers (2)

GhostVision
GhostVision

Reputation: 106

Update. So I decided to play around a bit more and... Solution offered by @Zeke Lu solved it perfectly without changing frontend.

All I did is made path in tmp absolute. Was <link rel="stylesheet" href="./blocks/index.css"> Made <link rel="stylesheet" href="/static/blocks/index.css">

And middleware gets it now as it supposed to.

This

   e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
        Root:       "examples/html",
        Browse:     true,
        IgnoreBase: true,
     })) 

resolves everything no problem without bunch of e.Static()

checked and double-checked with cache and all, locally and in docker; works perfectly

Thanks for the tip @Zeke Lu

Happy coding to all.

Upvotes: 1

GhostVision
GhostVision

Reputation: 106

Guess this can be considered closed. Had a long discussion with @Zeke Lu

With his help came to conclusion that frontend messed up big time. When it is fixed everything can be done with 1 line of code other then lots and lots of them as service grows.

Upvotes: 0

Related Questions