1

EDIT: I am usinng chi as the router. It works differently from the standard one. See the answer for the key difference, relevant to my question.

I am confused about how the files from my static site served by my application are targeted for retrieval.

I have a typical

r.Handle("/", http.FileServer(http.Dir("spa")))

and the content of spa is

│   favicon.ico
│   index.html
│
├───css
│       510.e095f672.css
│       app.f05c50d3.css
│       vendor.9add3052.css
│
├───fonts
│       flUhRq6tzZclQEJ-Vdg-IuiaDsNa.1dd1bb36.woff
│       flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.f54bbe10.woff2
│       KFOkCnqEu92Fr1MmgVxIIzQ.9391e6e2.woff
│       KFOlCnqEu92Fr1MmEU9fBBc-.ddd11dab.woff
│       KFOlCnqEu92Fr1MmSU5fBBc-.877b9231.woff
│       KFOlCnqEu92Fr1MmWUlfBBc-.0344cc3c.woff
│       KFOlCnqEu92Fr1MmYUtfBBc-.b555d228.woff
│       KFOmCnqEu92Fr1Mu4mxM.9b78ea3b.woff
│
├───icons
│       favicon-128x128.png
│       favicon-16x16.png
│       favicon-32x32.png
│       favicon-96x96.png
│
└───js
        361.136e16c3.js
        510.546967b6.js
        898.3690f332.js
        997.92f440f8.js
        app.42bde279.js
        app.578482b2.js
        vendor.8c20bf3b.js

When accessing http://localhost:15555/ I get in my logs

2022-03-31T20:20:10+02:00 | INFO  | / → [::1]:23750
2022-03-31T20:20:10+02:00 | INFO  | /js/vendor.8c20bf3b.js → [::1]:23750
2022-03-31T20:20:10+02:00 | INFO  | /js/app.42bde279.js → [::1]:23751
2022-03-31T20:20:10+02:00 | INFO  | /css/vendor.9add3052.css → [::1]:23752
2022-03-31T20:20:10+02:00 | INFO  | /css/app.f05c50d3.css → [::1]:23753

Only the first call is successful (200), the others are 404

This first call is finally supposed to retrieve index.html, and it does. This is why the linked .js and .css files are accessed.

This said, trying http://localhost:15555/ I also get a 404 on the retrieval

2022-03-31T20:26:58+02:00 | INFO  | /index.html → [::1]:24233

My questions:

  • why does / succeed as it ultimately gets /index.html?
  • why are other files 404?
blackgreen
  • 34,072
  • 23
  • 111
  • 129
WoJ
  • 27,165
  • 48
  • 180
  • 345
  • 1
    Can you provide a [mcve]? What you describe works just fine for me, see: https://imgur.com/a/gFHHohO – mkopriva Mar 31 '22 at 18:56
  • Also note that when you set up the handler as described, then a request to `/index.html` will not be 404 but instead 301, a redirect, with the location header set to `/`. That's a special case mentioned in http.FileServer's documentation: https://pkg.go.dev/net/http@go1.18#FileServer – mkopriva Mar 31 '22 at 19:05
  • @mkopriva: interesting, I ultimately had to add a wildcard to my handler for it to work (I added an answer, hopefully correct) – WoJ Mar 31 '22 at 19:08

1 Answers1

2

I found the problem, the handler must be

r.Handle("/*", http.FileServer(http.Dir("spa")))

I thought that / would match everything and that a wildcard was not needed.

WoJ
  • 27,165
  • 48
  • 180
  • 345
  • Note that wildcard is not needed for routing handlers with the stdlib's net/http package. You are probably not using the standard lib to route the handlers and that unnamed 3rd party lib obviously does require you to supply a wildcard. – mkopriva Mar 31 '22 at 19:09
  • Mentioning what tools you use in the question, especially if they are 3rd party, can be quite useful to anyone who would like to help you. – mkopriva Mar 31 '22 at 19:11
  • 1
    @mkopriva: ha, that's a good point. I am new to Go and was under the impression that there is a homogeneity around methods that are called the same way, same type etc. That was obviously a wrong preconception. The library I used was [`chi`](https://github.com/go-chi/chi) – WoJ Mar 31 '22 at 19:17