commit b92089215594a6e399ac891f589de28071a179dd Author: Jordan Orelli Date: Sat Nov 1 11:23:03 2014 -0400 hi it's a server. diff --git a/main.go b/main.go new file mode 100644 index 0000000..6555402 --- /dev/null +++ b/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "flag" + "fmt" + "net/http" + "os" + "strings" +) + +var options struct { + port int + hostname string +} + +func bail(status int, template string, args ...interface{}) { + if !strings.HasSuffix(template, "\n") { + template += "\n" + } + if status == 0 { + fmt.Fprintf(os.Stdout, template, args...) + } else { + fmt.Fprintf(os.Stderr, template, args...) + } + os.Exit(status) +} + +func main() { + flag.Parse() + cwd, err := os.Getwd() + if err != nil { + bail(1, "unable to get working directory: %v", err) + } + dir := http.Dir(cwd) + http.Handle("/", http.FileServer(dir)) + + addr := fmt.Sprintf("%s:%d", options.hostname, options.port) + fmt.Printf("serving %s on %s\n", cwd, addr) + if err := http.ListenAndServe(addr, nil); err != nil { + bail(1, "unable to start server: %v", err) + } +} + +func init() { + flag.IntVar(&options.port, "port", 8000, "port to serve on") + flag.StringVar(&options.hostname, "host", "", "hostname") +}