-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathserve.go
More file actions
38 lines (31 loc) · 852 Bytes
/
serve.go
File metadata and controls
38 lines (31 loc) · 852 Bytes
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
package main
import (
"net/http"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Infof("%s %s", r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func serveCmd() *cobra.Command {
var (
port string
dir string
)
cmd := &cobra.Command{
Use: "serve",
Short: "serve a directory over http",
Long: `Serve a directory over http.`,
RunE: func(cmd *cobra.Command, args []string) error {
http.Handle("/", http.FileServer(http.Dir(dir)))
log.Fatal(http.ListenAndServe(port, logRequest(http.DefaultServeMux)))
return nil
},
}
cmd.Flags().StringVar(&port, "port", ":8080", "Local port to serve on")
cmd.Flags().StringVar(&dir, "directory", ".", "Directory to serve")
return cmd
}