Skip to content

Commit 7d9488a

Browse files
committed
Switch to docker on CloudRun, name from golang to go
1 parent 1857b33 commit 7d9488a

13 files changed

+205
-73
lines changed

.air.toml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
root = "."
2+
tmp_dir = "tmp"
3+
4+
[build]
5+
bin = "./tmp/main"
6+
cmd = "go build -o ./tmp/main ."
7+
delay = 1000
8+
exclude_dir = ["tmp"]
9+
exclude_file = []
10+
exclude_regex = []
11+
exclude_unchanged = false
12+
follow_symlink = false
13+
full_bin = ""
14+
include_dir = []
15+
include_ext = ["go", "tmpl"]
16+
kill_delay = "0s"
17+
log = "build-errors.log"
18+
send_interrupt = false
19+
stop_on_error = true
20+
21+
[color]
22+
app = ""
23+
build = "yellow"
24+
main = "magenta"
25+
runner = "green"
26+
watcher = "cyan"
27+
28+
[log]
29+
time = false
30+
31+
[misc]
32+
clean_on_exit = false

.dockerignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.air.toml
2+
.DS_Store
3+
.git
4+
.github
5+
*.md
6+
*.sh
7+
*.tmp
8+
tmp*

.github/workflows/appengine-deploy.yaml

Lines changed: 0 additions & 43 deletions
This file was deleted.

.github/workflows/gcr-deploy.yaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
workflow_dispatch:
9+
10+
# Environment variables available to all jobs and steps in this workflow
11+
# NOTE: these aren't really secret, but there aren't non-secret settings
12+
env:
13+
RUN_PROJECT: ${{ secrets.RUN_PROJECT }}
14+
RUN_REGION: ${{ secrets.RUN_REGION }}
15+
RUN_SERVICE: ${{ secrets.RUN_SERVICE }}
16+
17+
jobs:
18+
deploy:
19+
name: Deploy to CloudRun
20+
runs-on: ubuntu-latest
21+
22+
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v4
25+
26+
- name: gcloud auth
27+
id: 'auth'
28+
uses: 'google-github-actions/auth@v2'
29+
with:
30+
credentials_json: '${{ secrets.GCP_SA_KEY }}'
31+
32+
# Setup gcloud CLI
33+
- name: gcloud setup
34+
uses: google-github-actions/setup-gcloud@v2
35+
36+
- name: gcloud docker-auth
37+
run: gcloud auth configure-docker
38+
39+
# Build and push image to Google Container Registry
40+
- name: Build
41+
run: |
42+
docker build \
43+
--build-arg COMMIT=${GITHUB_SHA:0:7} \
44+
--build-arg LASTMOD=$(date -u +%Y-%m-%dT%H:%M:%SZ) \
45+
--tag gcr.io/${RUN_PROJECT}/${RUN_SERVICE}:$GITHUB_SHA \
46+
.
47+
48+
- name: GCloud auth to docker
49+
run: |
50+
gcloud auth configure-docker
51+
52+
- name: Push to registry
53+
run: |
54+
docker push gcr.io/${RUN_PROJECT}/${RUN_SERVICE}:$GITHUB_SHA
55+
56+
# Deploy image to Cloud Run
57+
- name: Deploy
58+
run: |
59+
gcloud run deploy ${RUN_SERVICE} \
60+
--allow-unauthenticated \
61+
--image gcr.io/${RUN_PROJECT}/${RUN_SERVICE}:$GITHUB_SHA \
62+
--platform managed \
63+
--project ${RUN_PROJECT} \
64+
--region ${RUN_REGION}

Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# syntax=docker/dockerfile:1
2+
FROM golang:1.23-alpine AS builder
3+
RUN apk update && \
4+
apk upgrade && \
5+
apk --no-cache add git
6+
RUN mkdir /build
7+
ADD . /build/
8+
WORKDIR /build
9+
ARG COMMIT
10+
ARG LASTMOD
11+
RUN echo "INFO: building for $COMMIT on $LASTMOD"
12+
RUN \
13+
CGO_ENABLED=0 GOOS=linux go build \
14+
-a \
15+
-installsuffix cgo \
16+
-ldflags "-X main.COMMIT=$COMMIT -X main.LASTMOD=$LASTMOD -extldflags '-static'" \
17+
-o regexplanet-go *.go
18+
19+
FROM scratch
20+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
21+
COPY --from=builder /build/regexplanet-go /app/
22+
WORKDIR /app
23+
ENV PORT=4000
24+
ENTRYPOINT ["./regexplanet-go"]

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# regexplanet-golang
1+
# regexplanet-go
2+
3+
[![build](https://github.com/regexplanet/regexplanet-go/actions/workflows/gcr-deploy.yaml/badge.svg)](https://github.com/regexplanet/regexplanet-go/actions/workflows/gcr-deploy.yaml)
24

35
This is the [Go language](http://www.golang.org/) backend for RegexPlanet, a tool for online regular expression testing.
46

app.yaml

Lines changed: 0 additions & 17 deletions
This file was deleted.

docker-run.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
#
3+
# run locally but in docker
4+
5+
set -o errexit
6+
set -o pipefail
7+
set -o nounset
8+
9+
docker build -t regexplanet-go .
10+
docker run \
11+
--publish 4000:4000 \
12+
--expose 4000 \
13+
--env PORT='4000' \
14+
--env COMMIT=$(git rev-parse --short HEAD) \
15+
--env LASTMOD=$(date -u +%Y-%m-%dT%H:%M:%SZ) \
16+
regexplanet-go

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/regexplanet/regexplanet-go
22

3-
go 1.22
3+
go 1.23

logger.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import (
4+
"log/slog"
5+
"os"
6+
)
7+
8+
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))

main.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"html"
8-
"log"
98
"net/http"
109
"os"
1110
"regexp"
@@ -15,7 +14,8 @@ import (
1514
)
1615

1716
func root_handler(w http.ResponseWriter, r *http.Request) {
18-
http.Redirect(w, r, "https://www.regexplanet.com/advanced/golang/index.html", http.StatusFound)
17+
w.Header().Set("Content-Type", "text/plain; charset=utf8")
18+
w.Write([]byte("Running " + runtime.Version()))
1919
}
2020

2121
func write_with_callback(w http.ResponseWriter, callback string, v interface{}) {
@@ -305,17 +305,22 @@ func test_handler(w http.ResponseWriter, r *http.Request) {
305305

306306
func main() {
307307

308-
http.HandleFunc("/", root_handler)
308+
http.HandleFunc("/{$}", root_handler)
309309
http.HandleFunc("/status.json", status_handler)
310310
http.HandleFunc("/test.json", test_handler)
311+
http.HandleFunc("/robots.txt", staticHandler.ServeHTTP)
312+
http.HandleFunc("/favicon.ico", staticHandler.ServeHTTP)
313+
http.HandleFunc("/favicon.svg", staticHandler.ServeHTTP)
314+
315+
var listenAddress = os.Getenv("ADDRESS")
311316

312-
port := os.Getenv("PORT")
313-
if port == "" {
314-
port = "4000"
317+
var listenPort, portErr = strconv.Atoi(os.Getenv("PORT"))
318+
if portErr != nil {
319+
listenPort = 4000
315320
}
316321

317-
log.Printf("Listening on port %s", port)
318-
if err := http.ListenAndServe(":"+port, nil); err != nil {
319-
log.Fatal(err)
322+
logger.Info("Listening", "address", listenAddress, "port", listenPort)
323+
if err := http.ListenAndServe(listenAddress+":"+strconv.Itoa(listenPort), nil); err != nil {
324+
logger.Error("unable to listen", "address", listenAddress, "port", listenPort, "error", err)
320325
}
321326
}

run.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
2+
#
3+
# run locally
4+
#
25

3-
go run src/regexplanet.go
6+
set -o errexit
7+
set -o pipefail
8+
set -o nounset
9+
10+
if [ -f ".env" ]; then
11+
export $(cat .env)
12+
fi
13+
14+
~/go/bin/air

staticHandler.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"embed"
5+
"io/fs"
6+
"net/http"
7+
)
8+
9+
//go:embed static
10+
var embeddedFiles embed.FS
11+
var staticHandler = initStaticHandler()
12+
13+
func initStaticHandler() http.Handler {
14+
15+
fsys, err := fs.Sub(embeddedFiles, "static")
16+
if err != nil {
17+
logger.Error("unable to create static file system", "error", err)
18+
panic(err)
19+
}
20+
21+
return http.FileServer(http.FS(fsys))
22+
}

0 commit comments

Comments
 (0)