about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlexander Karl <akarl@darknebu.la>2019-10-10 21:10:17 +0200
committerAlexander Karl <akarl@darknebu.la>2019-10-10 21:10:17 +0200
commit8645e18778d98dc9a4e9686bf3cf84a4c0663f34 (patch)
treeede2f3363ed24042608f003eea343582e4f1eb78
Init commit
-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)
+	})
+}