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

Static files aren't getting served properly in case of nesting after embedding them in binary #1391

Closed
ansrivas opened this issue Nov 3, 2019 · 5 comments

Comments

@ansrivas
Copy link

ansrivas commented Nov 3, 2019

Hi, I have recently upgraded to v12.0.1 and started experiencing the issue that static files are not getting served properly. My scenario is like this:

  • Static files are nested like this:
$ tree 
├── assets
│   └── static
│       ├── css
│       └── js
├── go.mod
├── go.sum
├── internal
│   └── templates
│       └── templates.go  <- This here is generated as an out of running go-bindata
├── main.go
└── pkg
   └── runner
       └── runner.go
  • They are converted to their "bin" format using go-bindata but with custom pkg name and output dir.

When I try to access the static files, I get 404.
I am attaching a very simple project to replicate the issue.
iris-static-issue.zip

Steps:

// Inside the project 
 $ go get -u github.com/go-bindata/go-bindata/...
 $ go-bindata -o internal/templates/templates.go -pkg templates assets/...
 $ go build .
 $ ./iris-static-issue

// Now try to query the end points
 curl -i http://localhost:8080/static/js/jquery-2.1.1.js  <- Fails with 404
 curl -i http://localhost:8080/static/css/bootstrap.min.css <- Fails with 404
@kataras
Copy link
Owner

kataras commented Nov 3, 2019

Templates are embedded using the 'view.Binary' method, static files through 'app.HandleDir'. I will check your code as soon as I come back to my town office but I dont think there is an issue with embedded files because they are working properly in our servers. We'll talk later on if you still have the issue.

@ansrivas
Copy link
Author

ansrivas commented Nov 3, 2019

Thank you for your response.
Sure, it was a naming mistake in the same project, I named it as "templates" rather than "static".
But the problem still persists.

I can also confirm that the pattern I have described in above zip file works very well in fairly large project with version v11.2.8. But I started encountering this when I attempted to migrate it to v12.x.x

@kataras
Copy link
Owner

kataras commented Nov 5, 2019

Hello @ansrivas, you are very welcome. I thank you for using Iris for so long time!

It shouldn't work on v11.2.8, the only change on static file serving was at: #1383. I think you are mistaken - actually the problem is that you serve http://localhost:8080/static/static/css/bootstrap.min.css but you really wanted the http://localhost:8080/static/css/bootstrap.min.css.

See your internal/templates/templates.go file:

// Code generated by go-bindata. DO NOT EDIT.
// sources:
// assets/static/css/bootstrap.min.css
// assets/static/js/jquery-2.1.1.js

^ Here ./assets/static -> /static request path will serve ./assets, so your code will serve /static/static/$resource based on the zip file you sent above.

More: Based on the templates.go, when you do app.HandleDir("/static", "./assets", ..) it will serve /static/ as ./assets/... but your ./assets bindata does not contain any file, it has only one folder, the /static one which contains the static files(/assets/static/bootstrap.min.css) you want to serve.

Solution

You need to run go-bindata from the static directory itself cd assets && go-bindata -o ../internal/templates/templates.go -pkg templates ./static/... then the internal/templates/templates.go will start with the correct folder /static/... (it's a go-bindata thing not an Iris one, this behavior is expected as AssetInfo and _bindata variable inside templates.go can't locate the file otherwise):

// Code generated by go-bindata. DO NOT EDIT.
// sources:
// static\css\bootstrap.min.css
// static\js\jquery-2.1.1.js

The below is working (see the terminal panel),

fsis


Iris ties to* locate and validate the embedded assets based on the 'virtual directory' given by the second parameter of app.HandleDir/iris.FileServer, we have the iris.StripPrefix too.

@ansrivas
Copy link
Author

ansrivas commented Nov 5, 2019

@kataras thank you for this detailed explanation.
Indeed your suggested solution worked perfectly :)

@ansrivas ansrivas closed this as completed Nov 5, 2019
@kataras
Copy link
Owner

kataras commented Nov 5, 2019

Glad to help @ansrivas!

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

2 participants