dongyi1111
dongyi1111
2017-03-31 03:37
浏览 68
已采纳

服务静态文件Go似乎效率低下..或者也许只是我自己:-)

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.

Browser Errors Server code with commented out line

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutan2111
    doutan2111 2017-03-31 13:44
    已采纳

    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))
    }
    

    enter image description here

    点赞 评论
  • douliao5467
    douliao5467 2017-03-31 07:34

    If you are using nginx to do this work then you don't need Go to do it too (unless you want to be able to run standalone without nginx). If that is the case then you can use a Path call instead of PathPrefix so you can do pattern matching.

    If you are just trying to do this via nginx then something like this should work:

    server {
        listen 8080;
    
        location ~* ^/static/ {
            root /path/to/directory/containing_static;
        }
    
        location  ~* ^.*/static/(.*)$ {
            rewrite "^.*/static/(.*)$" /static/$1 last;
        }
    
    
        location @forward_to_app {
           # send to your go app here
        }
    
      }
    

    You can probably do without the rewrite, but that was an easy approach to take.

    点赞 评论

相关推荐