-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
57 lines (54 loc) · 1003 Bytes
/
web.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
/*
Simple static web server for UNIX files.
*/
package main
import (
"clive/cmd"
"clive/cmd/opt"
"net/http"
)
var (
dir = "/zx/usr/web"
port = "8080"
tport = "8083"
opts = opt.New("[dir]")
)
func main() {
cmd.UnixIO()
local := false
opts.NewFlag("p", "port: port used (8080 by default)", &port)
opts.NewFlag("t", "port: TLS port (8083 by default)", &tport)
opts.NewFlag("l", "localhost and TLS only", &local)
args := opts.Parse()
switch len(args) {
case 0:
if local {
dir = "/zx"
}
case 1:
dir = args[0]
default:
cmd.Warn("too many arguments")
opts.Usage()
}
cert := "/zx/lib/webcert.pem"
key := "/zx/lib/webcert.key"
addr := ":"
go func() {
err := http.ListenAndServeTLS(addr+tport, cert, key, http.FileServer(http.Dir(dir)))
if err != nil {
cmd.Fatal(err)
}
}()
go func() {
if local {
return
}
err := http.ListenAndServe(addr+port, http.FileServer(http.Dir(dir)))
if err != nil {
cmd.Fatal(err)
}
}()
c := make(chan bool)
<-c
}