Skip to content

Instantly share code, notes, and snippets.

@sokrato
Last active August 29, 2015 14:06
Show Gist options
  • Save sokrato/e9c8a600452d62720fcc to your computer and use it in GitHub Desktop.
Save sokrato/e9c8a600452d62720fcc to your computer and use it in GitHub Desktop.
simple http server serving file sys with Go
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))
}
@sokrato
Copy link
Author

sokrato commented Sep 10, 2014

Usage: sh: go build http.go && ./http -bind :8080 -dir /var/www/static

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment