Created
September 24, 2013 17:12
-
-
Save jrsconfitto/6687990 to your computer and use it in GitHub Desktop.
Simple static file server in Go
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 ( | |
"flag" | |
"fmt" | |
"net/http" | |
"os" | |
"strings" | |
"strconv" | |
) | |
var port *int | |
func init() { | |
port = flag.Int("port", 4000, "The port from which static files will be served.") | |
flag.Parse() | |
} | |
func main() { | |
currentDirectory, err := os.Getwd() | |
if err == nil { | |
thePort := strings.TrimSpace(strconv.Itoa(*port)) | |
fmt.Printf("Serving from http://localhost:%v", thePort) | |
http.ListenAndServe(":"+thePort, http.FileServer(http.Dir(currentDirectory))) | |
} else { | |
fmt.Println(err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment