-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
91 lines (77 loc) · 1.81 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
package main
import (
"fmt"
"log"
"net/http"
"os/exec"
"strconv"
"time"
"github.com/jpillora/opts"
"github.com/jpillora/requestlog"
"github.com/jpillora/serve/serve"
)
var version string = "0.0.0"
type Config struct {
Host string `help:"Host interface"`
Port int `help:"Listening port"`
Open bool `help:"On server startup, open the webroot in the default browser (uses the 'open' command)"`
KeyPath string `help:"TLS Key file path"`
CertPath string `help:"TLS Certicate file path"`
serve.Config `type:"embedded"`
}
func main() {
//defaults
c := Config{
Host: "0.0.0.0",
Port: 3000,
Config: serve.Config{
Directory: ".",
},
}
//parse
opts.New(&c).
Name("serve").
Version(version).
Repo("github.com/jpillora/serve").
Parse()
isTLS := c.CertPath != "" || c.KeyPath != "" //poor man's XOR
if isTLS && (c.CertPath == "" || c.KeyPath == "") {
log.Fatal("You must provide both key and cert paths")
}
//ready!
h, err := serve.NewHandler(c.Config)
if err != nil {
log.Fatal(err)
}
port := strconv.Itoa(c.Port)
if c.Open {
go func() {
proto := "http"
if isTLS {
proto += "s"
}
host := c.Host
if host == "0.0.0.0" {
host = "localhost"
}
time.Sleep(500 * time.Millisecond)
cmd := exec.Command("open", proto+"://"+host+":"+port)
cmd.Run()
}()
}
fmt.Printf("%sserving %s%s %son port %s%d%s\n",
requestlog.DefaultOptions.Colors.Grey,
requestlog.DefaultOptions.Colors.Cyan, c.Config.Directory,
requestlog.DefaultOptions.Colors.Grey,
requestlog.DefaultOptions.Colors.Cyan, c.Port,
requestlog.DefaultOptions.Colors.Reset,
)
server := http.Server{
Addr: c.Host + ":" + port,
Handler: h,
}
if isTLS {
log.Fatal(server.ListenAndServeTLS(c.CertPath, c.KeyPath))
}
log.Fatal(server.ListenAndServe())
}