portal/cmd/webstat/main.go

41 lines
867 B
Go

package main
import (
"flag"
"io"
"log"
"net/http"
"os"
)
type FileSender struct {
path string
}
func (s *FileSender) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f, err := os.Open(s.path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
w.Header().Set("Content-Type", "text/plain")
if _, err := io.Copy(w, f); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func main() {
address := flag.String("address", ":8080", "Address to bind")
web := flag.String("web", "web", "Web directory")
http.Handle("/proc/stat", &FileSender{"/proc/stat"})
http.Handle("/proc/meminfo", &FileSender{"/proc/meminfo"})
http.Handle("/", http.FileServer(http.Dir(*web)))
log.Println("Listening on", *address)
http.ListenAndServe(*address, http.DefaultServeMux)
}