| OLD | NEW |
| (Empty) | |
| 1 // Application that serves up the contents of /tmp/glog via HTTP, giving access |
| 2 // to logs w/o needing to SSH into the server. |
| 3 package main |
| 4 |
| 5 import ( |
| 6 "flag" |
| 7 "fmt" |
| 8 "html/template" |
| 9 "net/http" |
| 10 "net/url" |
| 11 "os" |
| 12 "path" |
| 13 "sort" |
| 14 "strings" |
| 15 |
| 16 "github.com/golang/glog" |
| 17 ) |
| 18 |
| 19 var port = flag.String("port", ":8001", "HTTP service address (e.g., ':8001')") |
| 20 |
| 21 // FileServer returns a handler that serves HTTP requests |
| 22 // with the contents of the file system rooted at root. |
| 23 // |
| 24 // To use the operating system's file system implementation, |
| 25 // use http.Dir: |
| 26 // |
| 27 // http.Handle("/", FileServer(http.Dir("/tmp"))) |
| 28 // |
| 29 // Differs from net/http FileServer by making directory listings better. |
| 30 func FileServer(root http.FileSystem) http.Handler { |
| 31 return &fileHandler{root} |
| 32 } |
| 33 |
| 34 type fileHandler struct { |
| 35 root http.FileSystem |
| 36 } |
| 37 |
| 38 func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 39 upath := r.URL.Path |
| 40 if !strings.HasPrefix(upath, "/") { |
| 41 upath = "/" + upath |
| 42 r.URL.Path = upath |
| 43 } |
| 44 serveFile(w, r, f.root, path.Clean(upath)) |
| 45 } |
| 46 |
| 47 // FileInfoSlice is for sorting. |
| 48 type FileInfoSlice []os.FileInfo |
| 49 |
| 50 func (p FileInfoSlice) Len() int { return len(p) } |
| 51 func (p FileInfoSlice) Less(i, j int) bool { return p[i].Name() < p[j].Name() } |
| 52 func (p FileInfoSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 53 |
| 54 func dirList(w http.ResponseWriter, f http.File) { |
| 55 w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 56 fmt.Fprintf(w, "<pre>\n") |
| 57 for { |
| 58 dirs, err := f.Readdir(10000) |
| 59 sort.Sort(FileInfoSlice(dirs)) |
| 60 if err != nil || len(dirs) == 0 { |
| 61 break |
| 62 } |
| 63 for _, d := range dirs { |
| 64 name := d.Name() |
| 65 if d.IsDir() { |
| 66 name += "/" |
| 67 } |
| 68 url := url.URL{Path: name} |
| 69 fmt.Fprintf(w, "%s <a href=\"%s\">%s</a>\n", d.ModTime()
, url.String(), template.HTMLEscapeString(name)) |
| 70 } |
| 71 } |
| 72 fmt.Fprintf(w, "</pre>\n") |
| 73 } |
| 74 |
| 75 func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name
string) { |
| 76 f, err := fs.Open(name) |
| 77 if err != nil { |
| 78 http.NotFound(w, r) |
| 79 return |
| 80 } |
| 81 defer f.Close() |
| 82 |
| 83 d, err1 := f.Stat() |
| 84 if err1 != nil { |
| 85 http.NotFound(w, r) |
| 86 return |
| 87 } |
| 88 |
| 89 url := r.URL.Path |
| 90 if d.IsDir() { |
| 91 if url[len(url)-1] != '/' { |
| 92 w.Header().Set("Location", path.Base(url)+"/") |
| 93 w.WriteHeader(http.StatusMovedPermanently) |
| 94 return |
| 95 } |
| 96 } |
| 97 |
| 98 if d.IsDir() { |
| 99 dirList(w, f) |
| 100 return |
| 101 } |
| 102 |
| 103 http.ServeContent(w, r, d.Name(), d.ModTime(), f) |
| 104 } |
| 105 |
| 106 func main() { |
| 107 flag.Parse() |
| 108 |
| 109 http.Handle("/", http.StripPrefix("/", FileServer(http.Dir("/tmp/glog"))
)) |
| 110 glog.Fatal(http.ListenAndServe(*port, nil)) |
| 111 } |
| OLD | NEW |