-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
64 lines (53 loc) · 1.37 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
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"github.com/vipnode/ethstats/stats"
"golang.org/x/crypto/acme/autocert"
)
func main() {
var (
addr = flag.String("listen", ":8080", "websocket address to listen on")
id = flag.String("id", "vipstats", "id of the ethstats server")
autotls = flag.Bool("autotls", true, "setup TLS on port :443 when listen is on port :80")
)
ethstats := &Server{
Name: stats.ID(*id),
}
_, port, err := net.SplitHostPort(*addr)
if err != nil {
exit(1, "failed to parse address", err)
}
mux := http.NewServeMux()
mux.HandleFunc("/api", ethstats.WebsocketHandler)
mux.HandleFunc("/", ethstats.APIHandler)
if port == "80" && *autotls {
log.Print("starting autocert process")
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache("certs"),
}
https := &http.Server{
Addr: ":443",
Handler: mux,
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
},
}
go http.ListenAndServe(":80", certManager.HTTPHandler(nil))
log.Fatal(https.ListenAndServeTLS("", ""))
} else {
log.Printf("listening on %s", *addr)
log.Fatal(http.ListenAndServe(*addr, mux))
}
}
// exit prints an error and exits with the given code
func exit(code int, msg string, a ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", a...)
os.Exit(code)
}