From 9a3d5c07e88e4d3df956061ae3d838639c69de5a Mon Sep 17 00:00:00 2001 From: txthinking Date: Tue, 21 Jul 2020 05:39:08 +0000 Subject: [PATCH] with dir --- main.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index 7ee411d..04549a0 100644 --- a/main.go +++ b/main.go @@ -1,22 +1,22 @@ package main import ( + "flag" "fmt" "log" "net/http" - "os" - "strings" ) +var address string +var dir string + +func init() { + flag.StringVar(&address, "l", ":8080", "listen address") + flag.StringVar(&dir, "d", ".", "web root dir") +} + func main() { - s := ":8080" - if len(os.Args) == 2 { - if strings.Contains(os.Args[1], "h") { - fmt.Println("$ httpserver [:8080]") - return - } - s = os.Args[1] - } - fmt.Println("listen http on", s) - log.Println(http.ListenAndServe(s, http.FileServer(http.Dir(".")))) + flag.Parse() + fmt.Println("listen http on", address) + log.Println(http.ListenAndServe(address, http.FileServer(http.Dir(dir)))) }