How to add favicon.ico on beego?

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

How to add favicon.ico on beego?

问题

以下是翻译好的内容:

我的 routers/default.go 文件中,我尝试使用原始的 Go 解决方案,但是失败了,这段代码无法编译。我不知道如何用 faviconHandler 替换路由器:

func faviconHandler(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "static/img/favicon.ico")
}

func init() {
    beego.Router("/", &controllers.MainController{})
    beego.Router("/favicon.ico", faviconHandler)  // 这段代码无法工作
}
英文:

my routers/default.go, I'm trying to use the original Go solution, but failed, this code can't compiled. I don't know how to replace the router with faviconHandler:

func faviconHandler(w http.ResponseWriter, r *http.Request) {                                                                                                                                 
        http.ServeFile(w, r, "static/img/favicon.ico")                                                                                                                                        
}                                                                                                                                                                                             
func init() {                                                                                                                                                                                 
        beego.Router("/", &controllers.MainController{})                                                                                                                                      
        beego.Router("/favicon.ico", faviconHandler)  // this doesn't work                 
}

答案1

得分: 1

我发现至少有一种方法:将其嵌入到index.tpl中。

bee new newapp
cd $GOPATH/src/newapp
bee run  # 你将看到一个蜜蜂的favicon。

将$GOPATH/src/view/index.tpl中的嵌入文本修改为你自己的,这里是一个Linux脚本

cd views
# 假设你已经将favicon.ico放在此目录中
base64 -w0 favicon.ico > favicon.b64
cp index.tpl index.tpl.old
sed 's/base64,.*"/base64,\n"/' index.tpl.old | sed '7r favicon.b64' > index.tpl
# rm favicon.ico favicon.b64  index.tpl.old # 删除临时文件
英文:

I found there is at least one method: embedded into index.tpl.

bee new newapp
cd $GOPATH/src/newapp
bee run  # you will see the favicon of a bee.

Modify the embedded text in $GOPATH/src/view/index.tpl to yours, here is a linux script

cd views
# assume you have put the favicon.ico in this directory
base64 -w0 favicon.ico > favicon.b64
cp index.tpl index.tpl.old
sed 's/base64,.*"/base64,\n"/' index.tpl.old | sed '7r favicon.b64' > index.tpl
# rm favicon.ico favicon.b64  index.tpl.old # remove the temp file

答案2

得分: 0

将您的favicon.ico文件放在./static/目录中。

请查看此链接https://beego.me/docs/quickstart/static.md

英文:

Put your favicon.ico file at ./static/ directory

Check this https://beego.me/docs/quickstart/static.md

huangapple
  • 本文由 发表于 2017年6月23日 15:41:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/44715768.html
匿名

发表评论

匿名网友

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

确定