Gin: 深度根源的静态文件未加载

huangapple go评论67阅读模式
英文:

Gin: Deeply Rooted Static Files Not Loading

问题

我有一个 gin 应用程序,其中并非所有静态文件都加载。根目录下的静态文件无法加载。

静态文件路径

repository
  assets
       project_1
          css/style.css
           vendor/aos/bootstrap

aos 文件夹中的文件无法加载,但 css 文件夹中的文件可以加载。我尝试了 router.Static("/asset", "./repository/assets"),但没有起作用。

英文:

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

答案1

得分: 1

我熟悉这个问题,并通过以下代码解决了它:

router.Static("/public", "./public")

我当时的文件结构如下:

root-folder
    pkg
        handler
            handler.go
    public
        file1.jpg
        file2.html
        [其他文件]

在文件 "handler.go" 中,我使用了上述代码,通过它我能够访问静态文件。

"root-folder" 表示整个 gin 项目的根目录。

我认为我的文件结构和代码示例将有助于你解决问题。

尝试将 "assets" 文件夹移动到根目录级别,并使用我的代码,只需将 "public" 改为 "assets"。

我的代码也适用于嵌套目录。也就是说,如果存在以下结构:

root-folder
    pkg
        handler
            handler.go
    public
        file1.jpg
        file2.html
        folder1
            file3.png
            folder2
                file4.html

那么访问文件 /public/folder1/folder2/file4.html 不会引起任何问题。

英文:

I am familiar with this problem, but I managed it with the help of the following code:

router.Static("/public", "./public")

And the file structure I had was as follows:

root-folder
    pkg
        handler
            handler.go
    public
        file1.jpg
        file2.html
        [other files]

In the file "handler.go" was the code listed above and with it I was able to access static files.

"root-folder" denotes the root directory of the entire gin project.

I think an example of my file structure and code will help you in solving your problem.

Try to move the "assets" folder to the root directory level and use my code, only specify "assets" instead of "public".

My code also works with nested directories. That is, if there is the following structure:

root-folder
    pkg
        handler
            handler.go
    public
        file1.jpg
        file2.html
        folder1
            file3.png
            folder2
                file4.html

Then access the file /public/folder1/folder2/file 4.html won't cause any problems.

huangapple
  • 本文由 发表于 2022年10月3日 17:31:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/73933533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定