Created
September 2, 2012 02:59
-
-
Save minikomi/3594363 to your computer and use it in GitHub Desktop.
Simple static 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" | |
"log" | |
"net/http" | |
"os" | |
) | |
var port string | |
var dir string | |
func init() { | |
flag.StringVar(&port, "p", "8080", "Port for the server to run on.") | |
flag.StringVar(&dir, "d", "", "Path to serve.") | |
} | |
func main() { | |
flag.Parse() | |
if dir == "" { | |
dir, _ = os.Getwd() | |
} | |
http.Handle("/", http.FileServer(http.Dir(dir))) | |
log.Printf("Serving Dir: %s on port %s\n", dir, port) | |
err := http.ListenAndServe(":"+port, nil) | |
if err != nil { | |
log.Fatal("ListenAndServe: ", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment