mirror of
				https://github.com/linuxkit/linuxkit.git
				synced 2025-11-04 04:50:17 +00:00 
			
		
		
		
	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:
		@@ -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":
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										34
									
								
								src/cmd/linuxkit/serve.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/cmd/linuxkit/serve.go
									
									
									
									
									
										Normal 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)))
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user