summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2021-09-03 10:19:01 -0600
committermo khan <mo@mokhan.ca>2021-09-03 10:19:01 -0600
commitb11cd4ccd8a9c8c3327f71809ec2182026f16a4b (patch)
treef2fb834e1d44b3456db9e3e5ccd955aeed612d9d
build a minimal http server
-rw-r--r--go.mod3
-rw-r--r--main.go35
2 files changed, 38 insertions, 0 deletions
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..793574a
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,3 @@
+module github.com/xlg-pkg/http-server
+
+go 1.17
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..695e4b7
--- /dev/null
+++ b/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)
+}