-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
80 lines (68 loc) · 1.88 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
package main
import (
"crypto/tls"
"flag"
"log"
"net/http"
"os"
"strings"
"golang.org/x/crypto/acme/autocert"
)
var (
HTML_FILE = flag.String("html", "", "the html bot file")
HTTP_SERVER = flag.String("http", ":http", "the http listen address")
HTTPS_SERVER = flag.String("https", ":https", "the https listen address")
SSL_CACHE_DIR = flag.String("cache-dir", ".autocert", "the autocert cache directory")
SERVER_NAME = flag.String("server-name", "", "")
)
func main() {
// parse the command line flags
flag.Parse()
// open the specified html file to be parsed
log.Println("Compiling from", *HTML_FILE, " ...")
file, err := os.Open(*HTML_FILE)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// Initialize the parser
bot, err := NewBotFromReader(file)
if err != nil {
log.Fatal(err)
}
// add a messenger handler
http.Handle("/messenger", NewMessenger(bot))
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Hello, Bkit ;)"))
})
// our global error channel
errchan := make(chan error)
// the main handler
var handler http.Handler
// starts the HTTPS server if required
if *HTTPS_SERVER != "" {
m := &autocert.Manager{
Cache: autocert.DirCache(*SSL_CACHE_DIR),
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(strings.Split(*SERVER_NAME, ",")...),
}
handler = m.HTTPHandler(nil)
s := &http.Server{
Addr: *HTTPS_SERVER,
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
}
go (func() {
log.Println("Start serving HTTPS traffic on", *HTTPS_SERVER)
errchan <- s.ListenAndServeTLS("", "")
})()
}
// starts the HTTP server if required
if *HTTP_SERVER != "" {
go (func() {
log.Println("Start serving HTTP traffic on", *HTTP_SERVER)
errchan <- http.ListenAndServe(*HTTP_SERVER, handler)
})()
}
// panic with the errors
log.Fatal(<-errchan)
}