-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmain.go
130 lines (118 loc) · 3.08 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
)
const (
defaultAddr = ":8080"
addrEnvVarName = "ADDR"
portEnvVarName = "PORT"
quietEnvVarName = "QUIET"
rootRoute = "/"
)
var (
addrFlag = os.Getenv(addrEnvVarName)
portFlag64, _ = strconv.ParseInt(os.Getenv(portEnvVarName), 10, 64)
portFlag = int(portFlag64)
quietFlag bool
routesFlag routes
)
func init() {
log.SetFlags(log.LUTC | log.Ldate | log.Ltime)
log.SetOutput(os.Stderr)
if addrFlag == "" {
addrFlag = defaultAddr
}
flag.StringVar(&addrFlag, "addr", addrFlag, fmt.Sprintf("address to listen on (environment variable %q)", addrEnvVarName))
flag.StringVar(&addrFlag, "a", addrFlag, "(alias for -addr)")
flag.IntVar(&portFlag, "port", portFlag, fmt.Sprintf("port to listen on (overrides -addr port) (environment variable %q)", portEnvVarName))
flag.IntVar(&portFlag, "p", portFlag, "(alias for -port)")
flag.BoolVar(&quietFlag, "quiet", quietFlag, fmt.Sprintf("disable all log output (environment variable %q)", quietEnvVarName))
flag.BoolVar(&quietFlag, "q", quietFlag, "(alias for -quiet)")
flag.Var(&routesFlag, "route", routesFlag.help())
flag.Var(&routesFlag, "r", "(alias for -route)")
flag.Parse()
if quietFlag {
log.SetOutput(ioutil.Discard)
}
for i := 0; i < flag.NArg(); i++ {
arg := flag.Arg(i)
err := routesFlag.Set(arg)
if err != nil {
log.Fatalf("%q: %v", arg, err)
}
}
}
func main() {
addr, err := addr()
if err != nil {
log.Fatalf("address/port: %v", err)
}
err = server(addr, routesFlag)
if err != nil {
log.Fatalf("start server: %v", err)
}
}
func server(addr string, routes routes) error {
mux := http.DefaultServeMux
handlers := make(map[string]http.Handler)
paths := make(map[string]string)
if len(routes.Values) == 0 {
routes.Set(".")
}
for _, route := range routes.Values {
handlers[route.Route] = &fileHandler{
route: route.Route,
path: route.Path,
}
paths[route.Route] = route.Path
}
for route, path := range paths {
mux.Handle(route, handlers[route])
log.Printf("serving local path %q on %q", path, route)
}
_, rootRouteTaken := handlers[rootRoute]
if !rootRouteTaken {
route := routes.Values[0].Route
mux.Handle(rootRoute, http.RedirectHandler(route, http.StatusTemporaryRedirect))
log.Printf("redirecting to %q from %q", route, rootRoute)
}
binaryPath, _ := os.Executable()
if binaryPath == "" {
binaryPath = "server"
}
log.Printf("%s listening on %q", filepath.Base(binaryPath), addr)
return http.ListenAndServe(addr, mux)
}
func addr() (string, error) {
portSet := portFlag != 0
addrSet := addrFlag != ""
switch {
case portSet && addrSet:
a, err := net.ResolveTCPAddr("tcp", addrFlag)
if err != nil {
return "", err
}
a.Port = portFlag
return a.String(), nil
case !portSet && addrSet:
a, err := net.ResolveTCPAddr("tcp", addrFlag)
if err != nil {
return "", err
}
return a.String(), nil
case portSet && !addrSet:
return fmt.Sprintf(":%d", portFlag), nil
case !portSet && !addrSet:
fallthrough
default:
return defaultAddr, nil
}
}