2
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

8
  • Have you considered using absolute path in the HTML and static files? For example, use <link rel="stylesheet" href="/static/blocks/index.css"> instead? Commented May 4, 2023 at 11:54
  • it gives 404 for absolute path Commented May 4, 2023 at 14:02
  • Reconfigure the router accordingly. What I want to say is, most of the time, it is a bad practice to use relative path in HTML. Commented May 4, 2023 at 14:05
  • the only way it allowed me to get through with e.static() is by setting href="./examples/html/blocks/index.css" so that it can remove initial, but that is pretty much the same of what i have in initial question; what I have works - I just don't like the way it looks, cuz it's smth that might require changing code when something else changes; it is possible that templates and statics might be somewhere outside in the future, so that there would be no need to go inside the code all the time and the way I did it makes that impossible ) Commented May 4, 2023 at 14:15
  • It seems that we are not talking about the same thing. I meant to set up a route like this: e.Static("/static", "examples/html"), then reference to the static resources like this: <link rel="stylesheet" href="/static/blocks/index.css">. Have you considered this solution? Commented May 4, 2023 at 14:23

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Update. Got to be careful, when using IgnoreBase. With my set-up it caused POST quaries to ignore handler. So could not add any more data for pages to check it with GET afterwards. My set up now: ``` e.Use(middleware.StaticWithConfig(middleware.StaticConfig{ Root: "examples/html", Browse: true, })) ```
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.