英文:
How does the root of a Go web application get resolved?
问题
我正在使用Go的net/http
库来提供一个Web应用程序。当用户访问网站时,他们可以简单地转到/,然后它会提供index.html(就像任何其他网站一样)。
这是通过以下方式实现的:
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./")))
然而,有两种方法可以组织这样的应用程序。首先,您可以将所有文件打包在同一个目录中。这对我来说似乎是一个不稳定的解决方案(如果我直接运行go文件,它会失败)。
src/bin/app.go
src/bin/index.html
或者,更"传统"的方式是将index.html置于顶层。
src/bin/app.go
index.html
- 在Go Web应用程序中,index.html或其他静态资源的正确位置是什么?
- 一旦我们将go文件打包成二进制文件,如何确定./的位置?
- 一个Go二进制文件是否可以保留非Go资源的目录结构,以便在开发和生产环境之间保持一致?
更新(下面的答案是正确的,并且我实现了Not_a_golfer的建议,我的目录树看起来像这样,在任何环境中都可以通过配置文件或服务器中的动态逻辑轻松维护)。
├── src
│ └── main
│ └── app.go
└── static
├── index.html
├── script.js
└── style.css
英文:
Im using go's net/http
library to serve up a web app. When users come to the site, they can simply go to /, and it serves index.html (like any site).
This is implemented using:
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./")))
However, there are two ways to structure such an app. First you could pack all the files in the same directory. This seems to me to be a unstable solution (if I run the go file directly it fails).
src/bin/app.go
src/bin/index.html
Or, the more "traditional" style, where index.html is top level.
src/bin/app.go
index.html
- Where is the right place for index.html, or other static resources in a go web app ?
- How is the location of ./ determined once we pack a go file into a binary ?
- Can a go binary file preserve a directory structure of non go resources so that this is consistent between dev and production environments?
UPDATE (The answer below is correct, and I implemented the suggestion by Not_a_golfer below and my dir tree looks like this, which is easy to maintain in any environment via conf or dynamic logic in the server).
├── src
│ └── main
│ └── app.go
└── static
├── index.html
├── script.js
└── style.css
答案1
得分: 4
由于Go语言不会将资源文件打包到二进制文件中,将代码和资源文件放在一起并不是一个好主意。
我建议采取以下其中一种方式:
首先,在这两种情况下,使你的应用程序可配置,可以通过命令行标志或配置文件指定静态资源的保存位置。这个位置应该是绝对路径,与可执行文件存储的位置无关。
然后,可以选择:
-
为静态资源保持一个完全独立的项目,并在构建过程中确保它们被打包在一起。实际上,你可以将它们分别部署到服务器上,这样如果只有HTML/JS/图像发生变化,就不需要重新启动Go应用程序。
-
将资源文件与源代码放在同一个项目中,但在顶层目录下创建一个名为"html"或"static"的目录。该目录下不允许有Go文件。部署过程应与第一种方式相同。
英文:
Since Go doesn't bundle assets into binaries, it's certainly not a good idea to keep your code and your assets bundled together.
I recommend one of the following:
First of all, in both cases - make your app configurable, so that it accepts where the static assets are saved, either via a command line flag or a config file. It should be an absolute path, as it is irrelevant to where the executable is stored.
And either:
-
Keep a completely separate project for the static assets, and as a build step make sure they are packed together. In fact, you can deploy them separately to your server, thus not having to restart the Go app if you're only deploying HTML/JS/image changes.
-
Keep their sources together in the same project, but make a top level directory named "html" or "static" or whatever. No Go files are allowed under this directory. The deployment process should be the same as in #1.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论