Repository navigation
Expand file tree
/
Copy pathserve.go
More file actions
107 lines (92 loc) · 2.77 KB
/
Copy pathserve.go
File metadata and controls
107 lines (92 loc) · 2.77 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package console
import (
"fmt"
"net/http"
"os"
"sync"
"github.com/hasura/graphql-engine/cli/v2"
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
"github.com/fatih/color"
"github.com/skratchdot/open-golang/open"
)
type ServeOpts struct {
APIServer *APIServer
ConsoleServer *ConsoleServer
EC *cli.ExecutionContext
DontOpenBrowser bool
Browser string
ConsolePort string
APIPort string
Address string
SignalChanAPIServer chan os.Signal
SignalChanConsoleServer chan os.Signal
}
// Server console and API Server
func Serve(opts *ServeOpts) error {
var op errors.Op = "console.Serve"
// get HTTP servers
apiHTTPServer := opts.APIServer.GetHTTPServer()
consoleHTTPServer, err := opts.ConsoleServer.GetHTTPServer()
if err != nil {
return errors.E(op, fmt.Errorf("cannot create console server: %w", err))
}
go func() {
<-opts.SignalChanAPIServer
if err := apiHTTPServer.Close(); err != nil {
opts.EC.Logger.Debugf("unable to close server running on port %s", opts.APIPort)
}
}()
go func() {
<-opts.SignalChanConsoleServer
if err := consoleHTTPServer.Close(); err != nil {
opts.EC.Logger.Debugf("unable to close server running on port %s", opts.ConsolePort)
}
}()
// Create WaitGroup for running 2 servers
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
if err := apiHTTPServer.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
opts.EC.Logger.Infof("server closed on port %s under signal", opts.APIPort)
} else {
opts.EC.Logger.WithError(err).Errorf("error listening on port %s", opts.APIPort)
}
}
wg.Done()
}()
wg.Add(1)
go func() {
if err := consoleHTTPServer.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
opts.EC.Logger.Infof("server closed on port %s under signal", opts.ConsolePort)
} else {
opts.EC.Logger.WithError(err).Errorf("error listening on port %s", opts.ConsolePort)
}
}
wg.Done()
}()
consoleURL := fmt.Sprintf("http://%s:%s/", opts.Address, opts.ConsolePort)
if !opts.DontOpenBrowser {
if opts.Browser != "" {
opts.EC.Spin(color.CyanString("Opening console on: %s", opts.Browser))
defer opts.EC.Spinner.Stop()
err = open.RunWith(consoleURL, opts.Browser)
if err != nil {
opts.EC.Logger.WithError(err).Warnf("failed opening console in '%s', try to open the url manually", opts.Browser)
}
} else {
opts.EC.Spin(color.CyanString("Opening console using default browser..."))
defer opts.EC.Spinner.Stop()
err = open.Run(consoleURL)
if err != nil {
opts.EC.Logger.WithError(err).Warn("Error opening browser, try to open the url manually?")
}
}
}
opts.EC.Spinner.Stop()
opts.EC.Logger.Infof("console running at: %s", consoleURL)
opts.EC.Telemetry.Beam()
wg.Wait()
return nil
}