diff --git a/README.md b/README.md new file mode 100644 index 0000000..0a3d7c9 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +Static File Server +================== + +This is a bare bones simple static file server, written in Go. Its about as +basic as it can get. It logs all requests to STDOUT. I use this as a Python +SimpleHTTPServer replacement. + +Install +======= + +Recommended to have $GOPATH/bin in your $PATH + $ go get github.com/drj42/gostatic + +Example Usage +======= + +Serve $HOME on 192.168.0.10:8000 + $ gostatic -port=8000 -host=192.168.0.10 -root=$HOME + +Serve current dir on 127.0.0.1:8080 + $ gostatic + diff --git a/gostatic.go b/gostatic.go new file mode 100644 index 0000000..f9d3a08 --- /dev/null +++ b/gostatic.go @@ -0,0 +1,28 @@ +package main + +import ( + "log" + "net/http" + "flag" +) + +var root, port, host string + +func init() { + flag.StringVar(&root, "root", ".", "Specify the directory to serve files from. Default is the current working directory") + flag.StringVar(&port, "port", "8080", "Specify server port. Default is 8080") + flag.StringVar(&host, "host", "127.0.0.1", "Specify host or IP. Default is 127.0.0.1") + flag.Parse() +} + + +func main() { + http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) { + log.Printf("%v %v", r.Method, r.URL.String()) + http.FileServer(http.Dir(root)).ServeHTTP(w,r) + return + }) + + log.Printf("Initializing static web server on %v:%v in %v\n", host, port, root) + log.Fatal(http.ListenAndServe(host + ":" + port , nil)) +}