@@ -28,14 +28,22 @@ const (
28
28
)
29
29
30
30
var (
31
- httpListen * string
32
- openBrowser * bool
33
- httpAddr string
31
+ httpListen * string
32
+ openBrowser * bool
33
+ webSocketOrigin * string
34
+ webSocketScheme * string
35
+
36
+ httpAddr string
37
+ scheme string
38
+ origin string
34
39
)
35
40
36
41
func Main () {
42
+
37
43
httpListen = flag .String ("http" , "127.0.0.1:3999" , "host:port to listen on" )
38
44
openBrowser = flag .Bool ("openbrowser" , true , "open browser automatically" )
45
+ webSocketOrigin = flag .String ("origin" , "" , "host:port used for web socket origin" )
46
+ webSocketScheme = flag .String ("scheme" , "" , "http or https, used for web socket origin scheme" )
39
47
40
48
flag .Parse ()
41
49
@@ -49,6 +57,27 @@ func Main() {
49
57
if host != "127.0.0.1" && host != "localhost" {
50
58
log .Print (localhostWarning )
51
59
}
60
+
61
+ // Used for when deploying to Cloud Run. The ENV var is set by Cloud Run as to the port to listen on.
62
+ envPort := os .Getenv ("PORT" )
63
+ if envPort != "" {
64
+ port = envPort
65
+ }
66
+
67
+ // If origin scheme is specified we use that instead of http.
68
+ scheme = "http"
69
+
70
+ if * webSocketScheme != "" {
71
+ scheme = * webSocketScheme
72
+ }
73
+
74
+ // If origin is specified we use that instead of host:port we are listening on.
75
+ origin = host + ":" + port
76
+
77
+ if * webSocketOrigin != "" {
78
+ origin = * webSocketOrigin
79
+ }
80
+
52
81
httpAddr = host + ":" + port
53
82
54
83
if err := initTour (http .DefaultServeMux , "SocketTransport" ); err != nil {
@@ -62,14 +91,15 @@ func Main() {
62
91
http .Handle ("/robots.txt" , fs )
63
92
http .Handle ("/images/" , fs )
64
93
65
- origin := & url.URL {Scheme : "http" , Host : host + ":" + port }
66
- http .Handle (socketPath , socket .NewHandler (origin ))
94
+ // Specifies the origin scheme and host for web socket. For deployment they are different than running locally.
95
+ originURL := & url.URL {Scheme : scheme , Host : origin }
96
+ http .Handle (socketPath , socket .NewHandler (originURL ))
67
97
68
98
h := webtest .HandlerWithCheck (http .DefaultServeMux , "/_readycheck" ,
69
99
os .DirFS ("." ), "tour/testdata/*.txt" )
70
100
71
101
go func () {
72
- url := "http://" + httpAddr
102
+ url := "http://" + host + ":" + port
73
103
if waitServer (url ) && * openBrowser && startBrowser (url ) {
74
104
log .Printf ("A browser window should open. If not, please visit %s" , url )
75
105
} else {
@@ -169,7 +199,14 @@ func startBrowser(url string) bool {
169
199
var prepContent = func (r io.Reader ) io.Reader { return r }
170
200
171
201
// socketAddr returns the WebSocket handler address.
172
- var socketAddr = func () string { return "ws://" + httpAddr + socketPath }
202
+ var socketAddr = func () string {
203
+
204
+ if scheme == "https" {
205
+ return "wss://" + origin + socketPath
206
+ }
207
+
208
+ return "ws://tour.ardanlabs.com:443" + socketPath
209
+ }
173
210
174
211
// analyticsHTML is optional analytics HTML to insert at the beginning of <head>.
175
212
var analyticsHTML template.HTML
0 commit comments