...

Source file src/github.com/shurcooL/httpfs/httputil/httputil.go

Documentation: github.com/shurcooL/httpfs/httputil

     1  // Package httputil implements HTTP utility functions for http.FileSystem.
     2  package httputil
     3  
     4  import (
     5  	"log"
     6  	"net/http"
     7  
     8  	"github.com/shurcooL/httpgzip"
     9  )
    10  
    11  // FileHandler is an http.Handler that serves the root of File,
    12  // which is expected to be a normal file (not a directory).
    13  type FileHandler struct {
    14  	File http.FileSystem
    15  }
    16  
    17  func (h FileHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    18  	f, err := h.File.Open("/")
    19  	if err != nil {
    20  		log.Printf("FileHandler.File.Open('/'): %v\n", err)
    21  		http.Error(w, err.Error(), http.StatusInternalServerError)
    22  		return
    23  	}
    24  	defer f.Close()
    25  	fi, err := f.Stat()
    26  	if err != nil {
    27  		log.Printf("FileHandler.File.Stat('/'): %v\n", err)
    28  		http.Error(w, err.Error(), http.StatusInternalServerError)
    29  		return
    30  	}
    31  	httpgzip.ServeContent(w, req, fi.Name(), fi.ModTime(), f)
    32  }
    33  

View as plain text