about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--main.go39
1 files changed, 39 insertions, 0 deletions
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)
+	})
+}