diff options
| author | mo khan <mo@mokhan.ca> | 2021-09-03 10:19:01 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2021-09-03 10:19:01 -0600 |
| commit | b11cd4ccd8a9c8c3327f71809ec2182026f16a4b (patch) | |
| tree | f2fb834e1d44b3456db9e3e5ccd955aeed612d9d | |
build a minimal http server
| -rw-r--r-- | go.mod | 3 | ||||
| -rw-r--r-- | main.go | 35 |
2 files changed, 38 insertions, 0 deletions
@@ -0,0 +1,3 @@ +module github.com/xlg-pkg/http-server + +go 1.17 @@ -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) +} |
