Created
March 17, 2018 15:12
-
-
Save tzmartin/4d68bcf16ca2a7e742fb82aa2acb64b3 to your computer and use it in GitHub Desktop.
Simple static server in go
This file contains 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 printRecovery() { | |
if r := recover(); r != nil { | |
log.Fatalln(fmt.Sprintf("ERROR: %+v", r)) | |
} | |
} | |
func main() { | |
defer printRecovery() | |
if len(os.Args) < 2 { | |
panic("Require first command-line argument to be port") | |
} | |
portStr := os.Args[1] | |
log.Println(fmt.Sprintf("Listening on %s...", portStr)) | |
panic(http.ListenAndServe(":"+portStr, http.FileServer(http.Dir(".")))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment