You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
975 B
Go

10 years ago
package main
import (
"flag"
"fmt"
10 years ago
"log"
10 years ago
"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)
10 years ago
http.Handle("/", &logWrapper{http.FileServer(dir)})
10 years ago
addr := fmt.Sprintf("%s:%d", options.hostname, options.port)
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")
10 years ago
log.SetOutput(os.Stdout)
10 years ago
log.SetFlags(log.Ldate | log.Lmicroseconds)
10 years ago
}