-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (45 loc) · 1.25 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/mattfarina/simple-web-server/pkg/fs"
"github.com/Masterminds/log-go"
"github.com/Masterminds/log-go/impl/cli"
"github.com/spf13/cobra"
)
func main() {
var debug bool
var trace bool
var port string
rootCmd := &cobra.Command{
Use: "simple-web-server",
Short: "Serve a filesystem on the web",
RunE: func(cmd *cobra.Command, args []string) error {
http.Handle("/", fs.FileServer(http.Dir(args[0])))
ul := fmt.Sprintf("localhost:%s", port)
log.Infof("Receiving traffic on %s", ul)
err := http.ListenAndServe(ul, nil)
return err
},
}
rootCmd.PersistentFlags().StringVarP(&port, "port", "p", "8080", "phe port to serve on")
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "provide debug level output")
rootCmd.PersistentFlags().BoolVarP(&trace, "trace", "t", false, "provide trace level output, including debug level")
rootCmd.PersistentFlags().Parse(os.Args)
lgr := cli.NewStandard()
if debug {
lgr.Level = log.DebugLevel
}
if trace {
lgr.Level = log.TraceLevel
}
log.Current = lgr
log.Debugf("Port: %s", port)
log.Debugf("Debug: %t", debug)
log.Debugf("Trace: %t", trace)
if err := rootCmd.Execute(); err != nil {
log.Error(err)
os.Exit(1)
}
}