-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathserve.go
62 lines (52 loc) · 1.44 KB
/
serve.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
package cmd
import (
"context"
"fmt"
"net/http"
"github.com/cnrancher/autok3s/pkg/common"
"github.com/cnrancher/autok3s/pkg/server"
"github.com/pkg/browser"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
serveCmd = &cobra.Command{
Use: "serve",
Short: "Run as daemon and serve HTTP/HTTPS request",
}
bindPort = "8080"
bindAddress = "127.0.0.1"
)
func init() {
serveCmd.Flags().StringVar(&bindPort, "bind-port", bindPort, "HTTP/HTTPS bind port")
serveCmd.Flags().StringVar(&bindAddress, "bind-address", bindAddress, "HTTP/HTTPS bind address")
}
// ServeCommand serve command.
func ServeCommand() *cobra.Command {
serveCmd.Run = func(_ *cobra.Command, _ []string) {
common.IsCLI = false
router := server.Start()
addr := fmt.Sprintf("%s:%s", bindAddress, bindPort)
// start kube-explorer for K3s clusters
go func(ctx context.Context) {
common.InitExplorer(ctx)
}(serveCmd.Context())
// start helm-dashboard server
go func(ctx context.Context) {
common.InitDashboard(ctx)
}(serveCmd.Context())
stopChan := make(chan struct{})
go func(c chan struct{}) {
logrus.Infof("run as daemon, listening on %s:%s", bindAddress, bindPort)
if err := http.ListenAndServe(addr, router); err != nil {
logrus.Error(err)
}
close(c)
}(stopChan)
if err := browser.OpenURL("http://" + addr); err != nil {
logrus.Warnf("failed to open browser to addr %s", addr)
}
<-stopChan
}
return serveCmd
}