cmd/serve: Add a new 'linuxkit serve' command

This simply starts a web server serving the specified directory.
It's useful for PXE booting.

Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
Rolf Neugebauer 2018-01-10 17:37:17 +00:00
parent 16ae50b593
commit 1cef947ee1
2 changed files with 37 additions and 0 deletions

View File

@ -76,6 +76,7 @@ func main() {
fmt.Printf(" pkg Package building\n") fmt.Printf(" pkg Package building\n")
fmt.Printf(" push Push a VM image to a cloud or image store\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(" 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(" version Print version information\n")
fmt.Printf(" help Print this message\n") fmt.Printf(" help Print this message\n")
fmt.Printf("\n") fmt.Printf("\n")
@ -124,6 +125,8 @@ func main() {
push(args[1:]) push(args[1:])
case "run": case "run":
run(args[1:]) run(args[1:])
case "serve":
serve(args[1:])
case "version": case "version":
printVersion() printVersion()
case "help": case "help":

34
src/cmd/linuxkit/serve.go Normal file
View File

@ -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)))
}