Last active
August 29, 2015 14:06
-
-
Save sokrato/e9c8a600452d62720fcc to your computer and use it in GitHub Desktop.
simple http server serving file sys with 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 ( | |
"flag" | |
"fmt" | |
"net/http" | |
"os" | |
) | |
var addr, dir, prefix string | |
func init() { | |
flag.StringVar(&addr, "bind", ":8888", "where to bind") | |
flag.StringVar(&dir, "dir", ".", "which dir to serve") | |
flag.StringVar(&prefix, "prefix", "", "strip some prefix?") | |
flag.Parse() | |
} | |
func err(e error) { | |
if e != nil { | |
fmt.Println(e) | |
os.Exit(1) | |
} | |
} | |
func main() { | |
handler := http.FileServer(http.Dir(dir)) | |
if prefix != "" { | |
handler = http.StripPrefix(prefix, handler) | |
} | |
fmt.Printf("Starting at: %v, dir=%v, prefix=%v\n", addr, dir, prefix) | |
err(http.ListenAndServe(addr, handler)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
sh: go build http.go && ./http -bind :8080 -dir /var/www/static