Skip to content

Commit 2f90772

Browse files
authored
Added deployment to GCR(Google Cloud Run) (ardanlabs#14)
1 parent ce7e7f2 commit 2f90772

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

Dockerfile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM golang:1.20.1-alpine
2+
3+
RUN apk update && apk upgrade && \
4+
apk add bash ca-certificates
5+
6+
COPY . /go/src/github.com/ardanlabs/tour/
7+
8+
WORKDIR /go/src/github.com/ardanlabs/tour
9+
10+
RUN go build -o /opt/tour/tour ./cmd/tour
11+
12+
WORKDIR /opt/tour
13+
14+
EXPOSE 8080
15+
16+
CMD ["./tour", "-http", "0.0.0.0:8080", "-origin", "tour.ardanlabs.com:443", "-scheme", "https", "-openbrowser", "false"]

cloudbuild.yaml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
steps:
2+
# Build the container image
3+
- name: 'gcr.io/cloud-builders/docker'
4+
args:
5+
- 'build'
6+
- '-t'
7+
- 'gcr.io/$PROJECT_ID/tour:$COMMIT_SHA'
8+
- '.'
9+
# Push the image to Container Registry
10+
- name: 'gcr.io/cloud-builders/docker'
11+
args: ['push', 'gcr.io/$PROJECT_ID/tour:$COMMIT_SHA']
12+
# Deploy container image tour to Cloud Run
13+
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
14+
entrypoint: gcloud
15+
args:
16+
- 'run'
17+
- 'deploy'
18+
- 'gotour'
19+
- '--image'
20+
- 'gcr.io/$PROJECT_ID/tour:$COMMIT_SHA'
21+
- '--region'
22+
- 'us-central1'
23+
- '--platform'
24+
- 'managed'

internal/tour/local.go

+44-7
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,22 @@ const (
2828
)
2929

3030
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
3439
)
3540

3641
func Main() {
42+
3743
httpListen = flag.String("http", "127.0.0.1:3999", "host:port to listen on")
3844
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")
3947

4048
flag.Parse()
4149

@@ -49,6 +57,27 @@ func Main() {
4957
if host != "127.0.0.1" && host != "localhost" {
5058
log.Print(localhostWarning)
5159
}
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+
5281
httpAddr = host + ":" + port
5382

5483
if err := initTour(http.DefaultServeMux, "SocketTransport"); err != nil {
@@ -62,14 +91,15 @@ func Main() {
6291
http.Handle("/robots.txt", fs)
6392
http.Handle("/images/", fs)
6493

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))
6797

6898
h := webtest.HandlerWithCheck(http.DefaultServeMux, "/_readycheck",
6999
os.DirFS("."), "tour/testdata/*.txt")
70100

71101
go func() {
72-
url := "http://" + httpAddr
102+
url := "http://" + host + ":" + port
73103
if waitServer(url) && *openBrowser && startBrowser(url) {
74104
log.Printf("A browser window should open. If not, please visit %s", url)
75105
} else {
@@ -169,7 +199,14 @@ func startBrowser(url string) bool {
169199
var prepContent = func(r io.Reader) io.Reader { return r }
170200

171201
// 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+
}
173210

174211
// analyticsHTML is optional analytics HTML to insert at the beginning of <head>.
175212
var analyticsHTML template.HTML

0 commit comments

Comments
 (0)