Commit b11cd4c

mo khan <mo@mokhan.ca>
2021-09-03 16:19:01
build a minimal http server
Changed files (2)
go.mod
@@ -0,0 +1,3 @@
+module github.com/xlg-pkg/http-server
+
+go 1.17
main.go
@@ -0,0 +1,35 @@
+package main
+
+import (
+	"fmt"
+	"net/http"
+	"os"
+)
+
+func host() string {
+	host := os.Getenv("HOST")
+	if host == "" {
+		return "localhost"
+	}
+	return host
+}
+
+func port() string {
+	host := os.Getenv("PORT")
+	if host == "" {
+		return "8080"
+	}
+	return host
+}
+
+func listenAddress() string {
+	return fmt.Sprintf("%s:%s", host(), port())
+}
+
+func main() {
+	address := listenAddress()
+	fmt.Printf("HTTP Server Ready at http://%s\n", address)
+
+	http.Handle("/", http.FileServer(http.Dir(".")))
+	http.ListenAndServe(address, nil)
+}