使用bindata嵌入JS文件的Go代码。

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

Go: embed JS files with bindata

问题

这个问题是我之前提出的问题的后续。我关闭了那个问题,所以我希望在这里问一个新的但相关的问题。https://stackoverflow.com/questions/27889779/go-embed-static-files-in-binary

我如何使用go-bindata来提供JS文件?我是否应该像这样将它传递到HTML中:

hi.html

<script>{{.Bindata}}</script>

尽管我没有编译错误或JS错误,但似乎不起作用。

英文:

This question is a follow up to an earlier question of mine. I've closed the question so I hope its okay that I ask a fresh but related question here. https://stackoverflow.com/questions/27889779/go-embed-static-files-in-binary

How do I serve JS files with go-bindata? Do I pass it into html like this

hi.html

&lt;script&gt;{{.Bindata}}&gt;&lt;/script&gt;

Doesn't seem to work even though I have no compile or JS errors.

答案1

得分: 2

使用https://github.com/elazarl/go-bindata-assetfs

假设你有以下结构:

myprojectdirectory
├───api
├───cmd
├───datastores
└───ui
    ├───css
    └───js

其中ui是你想要打包到应用程序中的目录结构...

生成源文件

go-bindata-assetfs工具非常简单。它将查看你传递给它的目录,并生成一个包含这些文件中二进制数据的变量的源文件。所以确保你的静态文件在那里,然后从myprojectdirectory运行以下命令:

go-bindata-assetfs ./ui/...

现在,默认情况下,这将在main包中创建一个源文件。有时,这是可以的。在我的情况下,不是这样。如果你愿意,可以生成一个具有不同包名的文件:

go-bindata-assetfs.exe -pkg cmd ./ui/...

将源文件放在正确的位置

在这种情况下,生成的文件bindata_assetfs.go被创建在myprojectdirectory目录中(这是不正确的)。在我的情况下,我只是手动将文件移动到cmd目录中。

更新应用程序代码

在我的应用程序中,我已经有一些代码从目录中提供文件:

import (
	"net/http"
	"github.com/gorilla/mux"
)

// 创建一个路由器并设置路由
var Router = mux.NewRouter()    
Router.PathPrefix("/ui").Handler(http.StripPrefix("/ui", http.FileServer(http.Dir("./ui"))))

// 开始监听
http.ListenAndServe("127.0.0.1:3000", Router)

确保像这样的代码正常工作。然后,将FileServer行轻松更改为:

Router.PathPrefix("/ui").Handler(http.StripPrefix("/ui", http.FileServer(assetFS())))

编译应用程序

现在,你有一个生成的源文件,其中包含你的静态资源。你现在可以安全地删除ui子目录结构。使用以下命令编译:

go install ./...

然后,你应该有一个正确提供静态资源的二进制文件。

英文:

Using https://github.com/elazarl/go-bindata-assetfs

Assuming you have the following structure:

myprojectdirectory
├───api
├───cmd
├───datastores
└───ui
    ├───css
    └───js

Where ui is the directory structure you'd like to wrap up and pack into your app...

Generate a source file

The go-bindata-assetfs tool is pretty simple. It will look at the directories you pass to it and generate a source file with variables that can contain the binary data in those files. So make sure your static files are there, and then run the following command from myprojectdirectory:

go-bindata-assetfs ./ui/...

Now, by default, this will create a source file in the package main. Sometimes, this is ok. In my case, it isn't. You can generate a file with a different package name if you'd like:

go-bindata-assetfs.exe -pkg cmd ./ui/...

#Put the source file in the correct location
In this case, the generated file bindata_assetfs.go is created in the myprojectdirectory directory (which is incorrect). In my case, I just manually move the file to the cmd directory.

#Update your application code
In my app, I already had some code that served files from a directory:

import (
	&quot;net/http&quot;
	&quot;github.com/gorilla/mux&quot;
)

// Create a router and setup routes
var Router = mux.NewRouter()    
Router.PathPrefix(&quot;/ui&quot;).Handler(http.StripPrefix(&quot;/ui&quot;, http.FileServer(http.Dir(&quot;./ui&quot;))))

// Start listening
http.ListenAndServe(&quot;127.0.0.1:3000&quot;, Router)

Make sure something like this works properly, first. Then it's trivial to change the FileServer line to:

Router.PathPrefix(&quot;/ui&quot;).Handler(http.StripPrefix(&quot;/ui&quot;, http.FileServer(assetFS())))

#Compile the app
Now you have a generated source file with your static assets in them. You can now safely remove the 'ui' subdirectory structure. Compile with

go install ./...

And you should have a binary that serves your static assets properly.

答案2

得分: 0

使用https://github.com/elazarl/go-bindata-assetfs

从自述文件中:

go-bindata-assetfs data/...

在你的代码中设置一个带有文件服务器的路由

http.Handle("/", http.FileServer(assetFS()))
英文:

Use https://github.com/elazarl/go-bindata-assetfs

From the readme:

go-bindata-assetfs data/...

In your code setup a route with a file server

http.Handle(&quot;/&quot;, http.FileServer(assetFS()))

答案3

得分: 0

这是你要翻译的内容:

在这里找到了答案:https://stackoverflow.com/questions/27906812/go-unescape-css-input-in-html/27907002#27907002

var safeCss = template.CSS(`body {background-image: url(&quot;paper.gif&quot;);}`)
英文:

Got my answer here: https://stackoverflow.com/questions/27906812/go-unescape-css-input-in-html/27907002#27907002

var safeCss = template.CSS(`body {background-image: url(&quot;paper.gif&quot;);}`)

huangapple
  • 本文由 发表于 2015年1月12日 21:54:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/27903565.html
匿名

发表评论

匿名网友

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

确定