github.com/matm/etcd@v0.3.1-0.20140328024009-5b4a473f1453/mod/dashboard/dashboard.go (about)

     1  package dashboard
     2  
     3  import (
     4  	"bytes"
     5  	"net/http"
     6  	"os"
     7  	"path"
     8  	"time"
     9  
    10  	"github.com/coreos/etcd/log"
    11  	"github.com/coreos/etcd/mod/dashboard/resources"
    12  )
    13  
    14  func memoryFileServer(w http.ResponseWriter, req *http.Request) {
    15  	log.Debugf("[recv] %s %s [%s]", req.Method, req.URL.Path, req.RemoteAddr)
    16  	upath := req.URL.Path
    17  	if len(upath) == 0 {
    18  		upath = "index.html"
    19  	}
    20  
    21  	// TODO: use the new mux to do this work
    22  	dir, file := path.Split(upath)
    23  	if file == "browser" || file == "stats" {
    24  		file = file + ".html"
    25  	}
    26  	upath = path.Join(dir, file)
    27  	b, err := resources.Asset(upath)
    28  
    29  	if err != nil {
    30  		http.Error(w, upath+": File not found", http.StatusNotFound)
    31  		return
    32  	}
    33  
    34  	http.ServeContent(w, req, upath, time.Time{}, bytes.NewReader(b))
    35  	return
    36  }
    37  
    38  // DashboardHttpHandler either uses the compiled in virtual filesystem for the
    39  // dashboard assets or if ETCD_DASHBOARD_DIR is set uses that as the source of
    40  // assets.
    41  func HttpHandler() (handler http.Handler) {
    42  	handler = http.HandlerFunc(memoryFileServer)
    43  
    44  	// Serve the dashboard from a filesystem if the magic env variable is enabled
    45  	dashDir := os.Getenv("ETCD_DASHBOARD_DIR")
    46  	if len(dashDir) != 0 {
    47  		log.Debugf("Using dashboard directory %s", dashDir)
    48  		handler = http.FileServer(http.Dir(dashDir))
    49  	}
    50  
    51  	return handler
    52  }