Skip to content

Instantly share code, notes, and snippets.

@crosbymichael
Created May 26, 2013 02:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crosbymichael/5651516 to your computer and use it in GitHub Desktop.
Save crosbymichael/5651516 to your computer and use it in GitHub Desktop.
Quick and simple static file server in Go. No more python -m SimpleHttpServer
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
)
//Type to wrap the default file handler to log requests
type logFileServer struct {
fileHandler http.Handler
}
func (h *logFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("%s: %s\n", r.Method, r.URL.String())
h.fileHandler.ServeHTTP(w, r)
}
func getServer(cwd string) http.Handler {
fileHandler := http.FileServer(http.Dir(cwd))
return &logFileServer{fileHandler}
}
func main() {
port := flag.String("p", "8080", "Port to serve from.")
flag.Parse()
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
location := fmt.Sprintf(":%s", *port)
fmt.Printf("Serving files from %s on port %s\n", cwd, location)
server := getServer(cwd)
if err = http.ListenAndServe(location, server); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment