From 8645e18778d98dc9a4e9686bf3cf84a4c0663f34 Mon Sep 17 00:00:00 2001 From: Alexander Karl Date: Thu, 10 Oct 2019 21:10:17 +0200 Subject: Init commit --- main.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..859694d --- /dev/null +++ b/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "flag" + "fmt" + "log" + "net/http" + "strings" +) + +func main() { + + port := flag.String("port", "8080", "Port to serve static content") + directory := flag.String("dir", ".", "directory that contain the static content") + subpath := flag.String("subpath", "", "Domain subpath") + flag.Parse() + + if len(*subpath) > 0 { + http.Handle(fmt.Sprintf("/%s/", *subpath), preventDirListing(http.StripPrefix(fmt.Sprintf("/%s/", *subpath), http.FileServer(http.Dir(*directory))))) + log.Printf("Using subpath: %s", *subpath) + } else { + http.Handle("/", preventDirListing(http.FileServer(http.Dir(*directory)))) + } + + log.Printf("Serving %s on HTTP port: %s\n", *directory, *port) + log.Fatal(http.ListenAndServe(":"+*port, nil)) + +} + +// Some hacky way to skip dir listing on / endpoints +func preventDirListing(h http.Handler) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if strings.HasSuffix(r.URL.Path, "/") { + http.NotFound(w, r) + return + } + h.ServeHTTP(w, r) + }) +} -- cgit 1.4.1