Skip to content

Instantly share code, notes, and snippets.

@jsutlovic
Created March 10, 2017 14:51
Show Gist options
  • Save jsutlovic/e3c05c0e16896a5422e770ff1fbbdb8e to your computer and use it in GitHub Desktop.
Save jsutlovic/e3c05c0e16896a5422e770ff1fbbdb8e to your computer and use it in GitHub Desktop.
HTTP File Server in Golang
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