donglin1192 2013-10-08 18:56
浏览 64
已采纳

使用文件服务器为我的单个html页面提供服务

I'm trying to build a sample web application demonstrating rest techniques using go at the back-end, serving json based requests and javascript, jquery in the front-end (I'm not using html/template package).

FileServer "returns a handler that serves HTTP requests with the contents of the file system rooted at root."

supose that I'm publishing my static folder that contains index.html and scripts folder holding some javascript files.

How can I prevent the client from viewing my js files (publishing just the index.html at /) ?

  • 写回答

1条回答 默认 最新

  • dongliang9682 2013-10-08 19:36
    关注

    You can easily restrict the FileServer, which is a HttpHandler by wrapping another HttpHandler around that. For example, take this wrapper which ONLY allows *.js files to be served:

    func GlobFilterHandler(h http.Handler, pattern string) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            path := r.URL.Path
    
            fileName := filepath.Base(path)
    
            if ok, err := filepath.Match(pattern, fileName); !ok || err != nil {
                if err != nil {
                    log.Println("Error in pattern match:", err)
                }
    
                http.NotFound(w, r)
                return
            }
    
            h.ServeHTTP(w, r)
        })
    }
    
    func main() {
        fileHandler := http.FileServer(http.Dir("/tmp/dtest"))
        wrappedHandler := GlobFilterHandler(fileHandler, "*.js")
    }
    

    You can find a blog post here which describes the basic idea pretty good.

    Another option you have is to extend on http.Dir and make your own http.FileSystem implementation which does exactly what you want:

    type GlobDir struct {
        Dir     http.Dir
        Pattern string
    }
    
    func (d GlobDir) Open(name string) (http.File, error) {
        baseName := filepath.Base(name)
    
        if ok, err := filepath.Match(d.Pattern, baseName); !ok || err != nil {
            if err != nil {
                return nil, err
            }
            return nil, fmt.Errorf("%s not match GlobDir pattern.", baseName)
        }
    
        return d.Dir.Open(name)
    }
    
    func main() {
        fileHandler := http.FileServer(GlobDir{
            Dir: http.Dir("/tmp/dtest"),
            Pattern: "*.js",
        })
    
        http.ListenAndServe(":8080", fileHandler)
    }
    

    The second solution implements the http.FileSystem interface which is accepted by http.FileServer. It checks whether the input file name matches the supplied pattern and then hands control down to the original http.Dir. This is probably the way you want to go here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)