Recommended Go project structure and build system for project with static assets that require compilation?

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

Recommended Go project structure and build system for project with static assets that require compilation?

问题

我正在为一个 Web 应用程序使用 Go 语言编写一个服务器。该应用程序由 Go 代码组成,用作 HTTP 服务器和 WebSocket 服务器。它还包括静态资源,如 HTML、CSS、JavaScript、图像等,由 Go 代码提供服务。我计划使用 go-bindata 或类似的工具来打包最终的静态资源。部署时希望得到一个包含所有静态资源的单个可执行二进制文件。

然而,JavaScript 资源需要在打包到 Go 源代码之前进行编译和组织。可以使用 npm 脚本、webpack、gulp 或类似的工具来完成这个过程。

我已经阅读了官方 Go 文档中关于推荐的项目结构的内容,但似乎没有涉及到我这种情况。

我不知道前端代码和后端代码应该放在同一个 Git 仓库中还是分开放。我也不知道应该使用哪个构建过程来处理 JavaScript 编译、go-bindata 过程和 go build 过程。是否有推荐的方法来管理这种类型的项目,或者有类似成功项目的示例吗?

英文:

I'm making a server in Go for a web app. The app consists of Go code which will act as an http server and a websocket server for the app. It will also include static assets to be served by the Go code, such as html/css/javascript/images/etc. I plan to use go-bindata or equivalent to package the final static assets. The desired form of deployment is a single binary executable with all static assets bundled in.

However, the javascript assets are to be built and organized in such a way that they will require a compilation step before they can be packaged into Go source for inclusion into the compiled binary. This will be accomplished with npm scripts/webpack/gulp/equivalent.

I have read the official Go documentation about the recommended project structure, but it doesn't seem to address my kind of use case.

I don't know if the frontend code and backend code should live in the same git repo or separate ones. I don't know what build process should handle running the javascript compilation process, the go-bindata process, and the go build process. Is there a recommended way to manage this type of project or perhaps some examples of similar successful projects?

答案1

得分: 2

这是我做的事情:

$GOPATH/src/github.com/myname/myproject:
-main.go //处理程序和代码
-static.go //生成的
-static
    -js
      -foo.js
    -html
    -css
    -img

我使用 github.com/mjibson/escstatic 中的所有内容转换为 static.go 中的虚拟文件系统。如果有额外的任务需要生成 css/js(例如运行 TypeScript 编译器),可以在嵌入资源之前运行这些任务。

所有这些都与 go generate 相关联,并带有注释:

//go:generate sass or tsc or grunt or whatever
//go:generate esc -o static.go static
英文:

Here's what I do:

$GOPATH/src/github.com/myname/myproject:
-main.go //handlers and code
-static.go //generated
-static
    -js
      -foo.js
    -html
    -css
    -img

I use github.com/mjibson/esc to convert everything in static to a virtual filesystem in static.go. If there are additional tasks to generate css/js (I run typescript compiler), those can be run before you embed the assets.

All of this gets wired up to go generate with comments:

//go:generate sass or tsc or grunt or whatever
//go:generate esc -o static.go static

huangapple
  • 本文由 发表于 2015年11月4日 04:13:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/33508213.html
匿名

发表评论

匿名网友

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

确定