Created
March 10, 2017 14:51
-
-
Save jsutlovic/e3c05c0e16896a5422e770ff1fbbdb8e to your computer and use it in GitHub Desktop.
HTTP File Server in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
) | |
func getenvWithDefault(key, fallback string) string { | |
value := os.Getenv(key) | |
if len(value) == 0 { | |
return fallback | |
} | |
return value | |
} | |
func main() { | |
port := getenvWithDefault("PORT", "8080") | |
host := getenvWithDefault("HOST", "") | |
listen := fmt.Sprint(host, ":", port) | |
fmt.Println("Listening on:", listen) | |
log.Fatal(http.ListenAndServe(listen, http.FileServer(http.Dir(".")))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment