diff options
author | Jordan <me@jordan.im> | 2022-09-08 14:46:47 -0700 |
---|---|---|
committer | Jordan <me@jordan.im> | 2022-09-08 14:46:47 -0700 |
commit | a4f620df90fc5022c3b217d1e75cc7be65ef6a28 (patch) | |
tree | 72d3bc0e6537d2fea07ee8ea649bcefb1c310bfa | |
parent | 6fba4c80ed803fb806d03ea740c1035725b7f282 (diff) | |
download | tent-a4f620df90fc5022c3b217d1e75cc7be65ef6a28.tar.gz tent-a4f620df90fc5022c3b217d1e75cc7be65ef6a28.zip |
-rw-r--r-- | tent.go | 22 |
1 files changed, 10 insertions, 12 deletions
@@ -10,21 +10,21 @@ import ( "os" "path/filepath" "strconv" - "strings" "time" ) -var path string - -func RequestHandler(w http.ResponseWriter, r *http.Request) { - p := filepath.Join(path, strings.Replace(r.URL.Path, "..", "", -1)) - http.ServeFile(w, r, p) +func RequestHandler(path string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, filepath.Join(path, r.URL.Path)) + } } func main() { - var host string - var port uint64 - + var ( + host string + port uint64 + path string + ) flag.StringVar(&host, "host", "127.0.0.1", "IP address to listen on") flag.Uint64Var(&port, "port", 0, "Port to listen on (default random)") flag.StringVar(&path, "path", "./", "Absolute or relative path to serve") @@ -33,7 +33,6 @@ func main() { if _, err := os.Stat(path); err != nil { panic(err) } - if port == 0 { rand.Seed(time.Now().UnixNano()) for { @@ -47,8 +46,7 @@ func main() { } } } - - http.HandleFunc("/", RequestHandler) + http.HandleFunc("/", RequestHandler(path)) fmt.Printf("Listening on %v port %v (http://%v:%v/)\n", host, port, host, port) log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), nil)) } |