/
main.go
80 lines (66 loc) · 1.84 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"path/filepath"
"github.com/krisrang/fancypants/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/krisrang/fancypants/Godeps/_workspace/src/github.com/elazarl/go-bindata-assetfs"
"github.com/krisrang/fancypants/Godeps/_workspace/src/github.com/thoas/stats"
"github.com/krisrang/fancypants/handlers"
)
func main() {
log.SetFlags(log.Ldate | log.Lmicroseconds)
app := cli.NewApp()
app.Name = "fancypants"
app.Usage = "http file server with fancy autoindex"
app.Version = "0.0.1"
app.Action = serve
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "path",
Value: "./",
Usage: "path to serve",
},
cli.StringFlag{
Name: "port",
Value: "8080",
Usage: "port to serve on",
},
}
app.Run(os.Args)
}
func serve(c *cli.Context) {
port := c.GlobalString("port")
fullPath, err := filepath.Abs(c.GlobalString("path"))
if err != nil {
log.Fatalf("Error setting path: %v", err)
}
logger := log.New(os.Stderr, "", log.Flags())
stat := stats.New()
mux := http.DefaultServeMux
mux.HandleFunc("/stats", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
bytes, err := json.Marshal(stat.Data())
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Write(bytes)
}))
http.Handle("/autoindexassets/",
stat.Handler(
http.StripPrefix("/autoindexassets/",
http.FileServer(
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "autoindexassets"}))))
http.HandleFunc("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
stat.ServeHTTP(w, r, handlers.Autodir(fullPath))
}))
server := &http.Server{
Addr: ":" + port,
Handler: handlers.NewApacheLoggingHandler(mux, logger),
}
log.Printf("Listening on %v", port)
server.ListenAndServe()
}