Skip to content

Commit

Permalink
Switch to docker on CloudRun, name from golang to go
Browse files Browse the repository at this point in the history
  • Loading branch information
fileformat committed Nov 10, 2024
1 parent 1857b33 commit 7d9488a
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 73 deletions.
32 changes: 32 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
root = "."
tmp_dir = "tmp"

[build]
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 1000
exclude_dir = ["tmp"]
exclude_file = []
exclude_regex = []
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tmpl"]
kill_delay = "0s"
log = "build-errors.log"
send_interrupt = false
stop_on_error = true

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
time = false

[misc]
clean_on_exit = false
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.air.toml
.DS_Store
.git
.github
*.md
*.sh
*.tmp
tmp*
43 changes: 0 additions & 43 deletions .github/workflows/appengine-deploy.yaml

This file was deleted.

64 changes: 64 additions & 0 deletions .github/workflows/gcr-deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: deploy

on:
push:
branches:
- main

workflow_dispatch:

# Environment variables available to all jobs and steps in this workflow
# NOTE: these aren't really secret, but there aren't non-secret settings
env:
RUN_PROJECT: ${{ secrets.RUN_PROJECT }}
RUN_REGION: ${{ secrets.RUN_REGION }}
RUN_SERVICE: ${{ secrets.RUN_SERVICE }}

jobs:
deploy:
name: Deploy to CloudRun
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: gcloud auth
id: 'auth'
uses: 'google-github-actions/auth@v2'
with:
credentials_json: '${{ secrets.GCP_SA_KEY }}'

# Setup gcloud CLI
- name: gcloud setup
uses: google-github-actions/setup-gcloud@v2

- name: gcloud docker-auth
run: gcloud auth configure-docker

# Build and push image to Google Container Registry
- name: Build
run: |
docker build \
--build-arg COMMIT=${GITHUB_SHA:0:7} \
--build-arg LASTMOD=$(date -u +%Y-%m-%dT%H:%M:%SZ) \
--tag gcr.io/${RUN_PROJECT}/${RUN_SERVICE}:$GITHUB_SHA \
.
- name: GCloud auth to docker
run: |
gcloud auth configure-docker
- name: Push to registry
run: |
docker push gcr.io/${RUN_PROJECT}/${RUN_SERVICE}:$GITHUB_SHA
# Deploy image to Cloud Run
- name: Deploy
run: |
gcloud run deploy ${RUN_SERVICE} \
--allow-unauthenticated \
--image gcr.io/${RUN_PROJECT}/${RUN_SERVICE}:$GITHUB_SHA \
--platform managed \
--project ${RUN_PROJECT} \
--region ${RUN_REGION}
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# syntax=docker/dockerfile:1
FROM golang:1.23-alpine AS builder
RUN apk update && \
apk upgrade && \
apk --no-cache add git
RUN mkdir /build
ADD . /build/
WORKDIR /build
ARG COMMIT
ARG LASTMOD
RUN echo "INFO: building for $COMMIT on $LASTMOD"
RUN \
CGO_ENABLED=0 GOOS=linux go build \
-a \
-installsuffix cgo \
-ldflags "-X main.COMMIT=$COMMIT -X main.LASTMOD=$LASTMOD -extldflags '-static'" \
-o regexplanet-go *.go

FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /build/regexplanet-go /app/
WORKDIR /app
ENV PORT=4000
ENTRYPOINT ["./regexplanet-go"]
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# regexplanet-golang
# regexplanet-go

[![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)

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

Expand Down
17 changes: 0 additions & 17 deletions app.yaml

This file was deleted.

16 changes: 16 additions & 0 deletions docker-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
#
# run locally but in docker

set -o errexit
set -o pipefail
set -o nounset

docker build -t regexplanet-go .
docker run \
--publish 4000:4000 \
--expose 4000 \
--env PORT='4000' \
--env COMMIT=$(git rev-parse --short HEAD) \
--env LASTMOD=$(date -u +%Y-%m-%dT%H:%M:%SZ) \
regexplanet-go
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/regexplanet/regexplanet-go

go 1.22
go 1.23
8 changes: 8 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import (
"log/slog"
"os"
)

var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
23 changes: 14 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"html"
"log"
"net/http"
"os"
"regexp"
Expand All @@ -15,7 +14,8 @@ import (
)

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

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

func main() {

http.HandleFunc("/", root_handler)
http.HandleFunc("/{$}", root_handler)
http.HandleFunc("/status.json", status_handler)
http.HandleFunc("/test.json", test_handler)
http.HandleFunc("/robots.txt", staticHandler.ServeHTTP)
http.HandleFunc("/favicon.ico", staticHandler.ServeHTTP)
http.HandleFunc("/favicon.svg", staticHandler.ServeHTTP)

var listenAddress = os.Getenv("ADDRESS")

port := os.Getenv("PORT")
if port == "" {
port = "4000"
var listenPort, portErr = strconv.Atoi(os.Getenv("PORT"))
if portErr != nil {
listenPort = 4000
}

log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
logger.Info("Listening", "address", listenAddress, "port", listenPort)
if err := http.ListenAndServe(listenAddress+":"+strconv.Itoa(listenPort), nil); err != nil {
logger.Error("unable to listen", "address", listenAddress, "port", listenPort, "error", err)
}
}
15 changes: 13 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#!/bin/bash
#!/usr/bin/env bash
#
# run locally
#

go run src/regexplanet.go
set -o errexit
set -o pipefail
set -o nounset

if [ -f ".env" ]; then
export $(cat .env)
fi

~/go/bin/air
22 changes: 22 additions & 0 deletions staticHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"embed"
"io/fs"
"net/http"
)

//go:embed static
var embeddedFiles embed.FS
var staticHandler = initStaticHandler()

func initStaticHandler() http.Handler {

fsys, err := fs.Sub(embeddedFiles, "static")
if err != nil {
logger.Error("unable to create static file system", "error", err)
panic(err)
}

return http.FileServer(http.FS(fsys))
}

0 comments on commit 7d9488a

Please sign in to comment.