提供静态文件Go似乎没有效率......或许它只是我:-) [英] Serving static files Go seems inefficient..or maybe its just me :-)

查看:109
本文介绍了提供静态文件Go似乎没有效率......或许它只是我:-)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月来,我阅读了大量关于Go和最佳实践的文章。这些文章中有许多谷歌搜索和SOF搜索关于如何以及提供静态文件的最佳方式。



这是我目前拥有的

  fs:= http。 FileServer(http.Dir(static /))
myRouter.PathPrefix(/ static /)。Handler(http.StripPrefix(/ static /,fs))

myRouter.PathPrefix(/ admin / static /)。Handler(http.StripPrefix(/ admin / static /,fs))
myRouter.PathPrefix(/ admin / accounts / static /)。Handler (http.StripPrefix(/ admin / accounts / static /,fs))
myRouter.PathPrefix(/ admin / admin_tools / static /)。Handler(http.StripPrefix(/ admin / admin_tools / static (/ admin / audit_tools / static /,fs))
myRouter。 PathPrefix(/ admin / demand / static /)。Handler(http.StripPrefix(/ admin / demand / static /,fs))
myRouter.PathPrefix(/ admin / optimization / static /) .Handler(http.StripPrefix(/ admin / optimization / static /,fs))
myRouter.PathPrefix(/ admin / reports / static /)。Handler(http.StripPrefix(/ admin / reports / static /,fs))
myRouter.PathPrefix(/ admin / setu (http:// stripprefix(/ admin / setups / static /,fs))
myRouter.PathPrefix(/ admin / queue / static /)。Handler(http.StripPrefix (/ admin / queue / static /,fs))
myRouter.PathPrefix(/ admin / tagservers / static /)。Handler(http.StripPrefix(/ admin / tagservers / static /,fs ))
myRouter.PathPrefix(/ admin / client / static /)。Handler(http.StripPrefix(/ admin / client / static /,fs))
myRouter.PathPrefix(/客户端/静态/)。Handler(http.StripPrefix(/ client / static /,fs))

我的问题是,对于每个路径,我必须添加一行以覆盖静态文件的来源。大多数示例都显示,如果您的单一登录页面没有内置实际导航,它将如何完成。从Python背景来看,我认为我被一些轻量级框架弄坏了,比如Flask和Tornado,其中一个只是指向一个静态文件夹。



另一个目前的设置问题与NGINX不兼容。再次使用像Flask和Tornado这样的框架,您在NGINX中所做的所有工作都需要设置一次静态位置。使用Go,我必须像上面的代码一样设置静态位置(通过定义每个路径)。

有没有人找到更好的方法来提供静态文件? 我理论上可以编写一个函数使其自动化,但它不会真正改变每个路径必须在应用程序级别和NGINX级别进行考虑的事实。

更新:在回复@mkopriva时,我附上了两张截图。第一种是浏览器运行应用程序,打开开发工具以在静态文件上显示404。第二个是服务器代码,为了产生这些错误,我只做了一行注释。处理audit_tools路径的行。如果我取消注释,所有路线都没有问题。






编辑时,@ mkopriva和@sberry都做得很好。我希望我可以选择两个正确的答案。 调用似乎对我来说在功能上毫无意义。如果您的静态目录位于您的Go项目中,并且其结构如下所示:

 
├──main.go
└──static
├──admin
│├──accounts
││└──file.txt
│├──file.txt
│└──举报
│└──file.txt
└──file.txt

然后通过Go提供来自该静态目录的文件,所有你应该真正需要的就是这个。



<$ (
log
net / http

$ b $ 包主

func main(){
fs:= http.FileServer(http.Dir(static /))
http.Handle(/,fs)

log .Fatal(http://ListenAndServe(:8080,nil))
}

< a href =https://i.stack.imgur.com/futaA.png =nofollow noreferrer>


Over months I've read tons of articles about Go and best practices. Among those articles are numerous google searches and SOF searches regarding how to and the best way to serve static files.

This is what I currently have

fs := http.FileServer(http.Dir("static/"))
myRouter.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))

myRouter.PathPrefix("/admin/static/").Handler(http.StripPrefix("/admin/static/", fs))
myRouter.PathPrefix("/admin/accounts/static/").Handler(http.StripPrefix("/admin/accounts/static/", fs))
myRouter.PathPrefix("/admin/admin_tools/static/").Handler(http.StripPrefix("/admin/admin_tools/static/", fs))
myRouter.PathPrefix("/admin/audit_tools/static/").Handler(http.StripPrefix("/admin/audit_tools/static/", fs))
myRouter.PathPrefix("/admin/demand/static/").Handler(http.StripPrefix("/admin/demand/static/", fs))
myRouter.PathPrefix("/admin/optimization/static/").Handler(http.StripPrefix("/admin/optimization/static/", fs))
myRouter.PathPrefix("/admin/reports/static/").Handler(http.StripPrefix("/admin/reports/static/", fs))
myRouter.PathPrefix("/admin/setups/static/").Handler(http.StripPrefix("/admin/setups/static/", fs))
myRouter.PathPrefix("/admin/queue/static/").Handler(http.StripPrefix("/admin/queue/static/", fs))
myRouter.PathPrefix("/admin/tagservers/static/").Handler(http.StripPrefix("/admin/tagservers/static/", fs))
myRouter.PathPrefix("/admin/client/static/").Handler(http.StripPrefix("/admin/client/static/", fs))
myRouter.PathPrefix("/client/static/").Handler(http.StripPrefix("/client/static/", fs))

My issue with this is that for every path I have to add a new line to cover where the static files come from. Most examples show how it's done when you have a single landing page with no real navigations built into it. Coming from a Python background I suppose I was bit spoiled with some of the lightweight frameworks, such as Flask and Tornado, where one just points to the static folder once.

Another issue with this current setup is it doesn't play nice with NGINX. Again with frameworks like Flask and Tornado, all you have to do in NGINX is set the static location once. With Go I have to set the static location just like the code above (by defining each path).

Has anyone found a better way to serve static files? I know in theory a function could probably be written to automate it, but it wouldn't really change the fact that each path has to be accounted for on the app level and NGINX level.

UPDATE: In reply to @mkopriva I've attached two screenshots. The first is of the browser running the app with dev tools open to show the 404s on the static files. The Second is of the server code, to produce those errors all I did was comment out one line. The line that handles the audit_tools path. If I uncomment it everything routes no problem.

Edit, both @mkopriva and @sberry did a great job. I wish I could pick two correct answers.

解决方案

All those PathPrefix and StripPrefix calls seem to me to be functionally pointless. If your static directory is inside your Go project and its structure looks something like this:

.
├── main.go
└── static
    ├── admin
    │   ├── accounts
    │   │   └── file.txt
    │   ├── file.txt
    │   └── reports
    │       └── file.txt
    └── file.txt

then to serve files from that static directory with Go, all you should really need is this.

package main

import (
    "log"
    "net/http"
)

func main() {
    fs := http.FileServer(http.Dir("static/"))
    http.Handle("/", fs)

    log.Fatal(http.ListenAndServe(":8080", nil))
}

这篇关于提供静态文件Go似乎没有效率......或许它只是我:-)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆