Report this

What is the reason for this report?

Gin: Deeply Rooted Static Files Not Loading

Posted on October 3, 2022

I have a gin application in which not all the static files load. Deeply rooted static files do not load.

### **Static file path**

##    repository

        assets

           project_1

             css/style.css

             vendor/aos/bootstrap

Files in the **aos ** folder do not load, but files in the **css ** folder load. I have tried router.Static(“/asset”, “./repository/assets”) but it does not work.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

The issue you’re facing with Gin where some deeply nested static files are not loading while others are could be related to how the static file serving is set up. When you use router.Static("/asset", "./repository/assets"), it should theoretically serve all files under the ./repository/assets directory accessible via the /asset endpoint. However, there might be nuances affecting the accessibility of files in deeper nested directories.

You need to verify the URL you’re using to access the files. For instance, the file at assets/project_1/css/style.css should be accessed via http://yourserver/asset/project_1/css/style.css. Similarly, a file at assets/project_1/vendor/aos/bootstrap/file.css should be accessible at http://yourserver/asset/project_1/vendor/aos/bootstrap/file.css.

Also, check that the server has the necessary permissions to read the files in the aos and other nested directories.

If the above steps are correct, consider testing with a more explicit static file serving setup for the problematic directory to see if it resolves the issue:

router := gin.Default()

router.Static("/asset", "./repository/assets")
// Explicitly define the path for nested directories if needed
router.Static("/asset/project_1/vendor/aos", "./repository/assets/project_1/vendor/aos")

router.Run(":8080")

Along with the above add logging to your application to check whether the requests for the static files are hitting the server and how they’re being resolved. It might provide insights into why certain files aren’t being served.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.