-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
58 lines (50 loc) · 1.6 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
package main
import (
"fmt"
"net/http"
"os"
"runtime"
"github.com/k0swe/kel-agent/internal/config"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var versionInfo string
var conf config.Config
func main() {
versionInfo = fmt.Sprintf("kel-agent %v (%v)", Version, GitCommit)
fmt.Printf("%v %v %v %v %v\n",
versionInfo, runtime.Version(), runtime.GOOS, runtime.GOARCH, BuildTime)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
var err error
if conf, err = config.ParseAllConfigs(); err != nil {
log.Fatal().Err(err).Msg("couldn't get configuration")
}
if conf.Websocket.Key != "" && conf.Websocket.Cert == "" ||
conf.Websocket.Key == "" && conf.Websocket.Cert != "" {
panic("-key and -cert must be used together")
}
secure := false
protocol := "ws://"
if conf.Websocket.Key != "" && conf.Websocket.Cert != "" {
secure = true
protocol = "wss://"
}
hub := newHub()
go hub.run()
http.HandleFunc("/websocket", func(w http.ResponseWriter, r *http.Request) {
serveWs(hub, w, r)
})
http.HandleFunc("/", indexHandler)
addrAndPort := fmt.Sprintf("%s:%d", conf.Websocket.Address, conf.Websocket.Port)
log.Info().Msgf("ready to serve at %s%s", protocol, addrAndPort)
if secure {
log.Fatal().Err(
http.ListenAndServeTLS(addrAndPort, conf.Websocket.Cert, conf.Websocket.Key, nil)).Msg("")
} else {
log.Fatal().Err(http.ListenAndServe(addrAndPort, nil)).Msg("")
}
}
func indexHandler(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("Congratulations, you've reached kel-agent! " +
"If you can see this, you should be able to connect to the websocket."))
}