diff --git a/src/cmd/linuxkit/main.go b/src/cmd/linuxkit/main.go index fda3ea09d..7db9f9864 100644 --- a/src/cmd/linuxkit/main.go +++ b/src/cmd/linuxkit/main.go @@ -76,6 +76,7 @@ func main() { fmt.Printf(" pkg Package building\n") fmt.Printf(" push Push a VM image to a cloud or image store\n") fmt.Printf(" run Run a VM image on a local hypervisor or remote cloud\n") + fmt.Printf(" serve Run a local http server (for iPXE booting)\n") fmt.Printf(" version Print version information\n") fmt.Printf(" help Print this message\n") fmt.Printf("\n") @@ -124,6 +125,8 @@ func main() { push(args[1:]) case "run": run(args[1:]) + case "serve": + serve(args[1:]) case "version": printVersion() case "help": diff --git a/src/cmd/linuxkit/serve.go b/src/cmd/linuxkit/serve.go new file mode 100644 index 000000000..ebe16da05 --- /dev/null +++ b/src/cmd/linuxkit/serve.go @@ -0,0 +1,34 @@ +package main + +import ( + "flag" + "fmt" + "net/http" + "os" + "path/filepath" + + log "github.com/sirupsen/logrus" +) + +func logRequest(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + log.Infof("%s %s", r.Method, r.URL) + handler.ServeHTTP(w, r) + }) +} + +// serve starts a local web server +func serve(args []string) { + flags := flag.NewFlagSet("serve", flag.ExitOnError) + invoked := filepath.Base(os.Args[0]) + flags.Usage = func() { + fmt.Printf("USAGE: %s serve [options]\n\n", invoked) + fmt.Printf("Options:\n\n") + flags.PrintDefaults() + } + portFlag := flags.String("port", ":8080", "Local port to serve on") + dirFlag := flags.String("directory", ".", "Directory to serve") + + http.Handle("/", http.FileServer(http.Dir(*dirFlag))) + log.Fatal(http.ListenAndServe(*portFlag, logRequest(http.DefaultServeMux))) +}