Skip to content

Instantly share code, notes, and snippets.

@surjikal
Forked from crosbymichael/server.go
Created July 15, 2013 03:00
Show Gist options
  • Save surjikal/5997221 to your computer and use it in GitHub Desktop.
Save surjikal/5997221 to your computer and use it in GitHub Desktop.
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