Skip to content

Commit

Permalink
Setup to deploy to app engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Piszmog committed Nov 20, 2023
1 parent b092fdd commit b31d834
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 1 deletion.
64 changes: 64 additions & 0 deletions .gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

### Go template
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

### AppEngine template
# Google App Engine generated folder
appengine-generated/

/components/*.go
.idea
*.iml
/assets/css
*.sqlite3
/tmp

# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
db.sqlite3
Makefile
tmp/
72 changes: 72 additions & 0 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: CICD
on:
push:
branches:
- main
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 1.21.x
- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 1.21.x
- run: go mod download
- name: Test
run: go test -race ./...
tag:
name: Tag
permissions:
contents: write
runs-on: ubuntu-latest
needs:
- lint
- test
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Get Version
run: echo "VERSION=$(cat version.txt)" >> $GITHUB_ENV
- name: Create Tag
uses: negz/create-tag@v1
with:
version: ${{ env.VERSION }}
token: ${{ secrets.GITHUB_TOKEN }}
deploy:
name: Deploy
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
needs:
- tag
steps:
- uses: actions/checkout@v4
- name: Get Version
run: echo "VERSION=$(cat version.txt)" >> $GITHUB_ENV
- name: Replace token secret
run: sed -i 's/\${secrets.DB_TOKEN}/'"$DB_TOKEN"'/' app.yaml
env:
DB_TOKEN: ${{ secrets.DB_TOKEN }}
- id: 'auth'
uses: 'google-github-actions/auth@v1'
with:
workload_identity_provider: 'projects/811165903449/locations/global/workloadIdentityPools/gh-pool/providers/gh-provider'
service_account: 'my-service-account@my-project.iam.gserviceaccount.com'
- id: 'deploy'
uses: 'google-github-actions/deploy-appengine@v1'
with:
version: ${{ env.VERSION }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ Icon
Network Trash Folder
Temporary Items
.apdisk

db.sqlite3
tmp/
11 changes: 11 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
runtime: go121

env_variables:
DB_TYPE: turso
DB_URL: pathwise
DB_TOKEN: ${secrets.DB_TOKEN}

handlers:
- url: .*
script: auto
secure: always
28 changes: 27 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,25 @@ var assets embed.FS
func main() {
l := logger.New(os.Getenv("LOG_LEVEL"))

database, err := db.New(db.DatabaseTypeFile, db.DatabaseOpts{URL: "./db.sqlite3"})
databaseType := getDatabaseType()

var database db.Database
var err error
switch databaseType {
case db.DatabaseTypeFile:
l.Info("using file database")
database, err = db.New(db.DatabaseTypeFile, db.DatabaseOpts{URL: "./db.sqlite3"})
case db.DatabaseTypeTurso:
l.Info("using turso database")
database, err = db.New(
db.DatabaseTypeTurso,
db.DatabaseOpts{URL: os.Getenv("DB_URL"), Token: os.Getenv("DB_TOKEN")},
)
default:
l.Error("unknown database type", "type", databaseType)
return
}

if err != nil {
l.Error("failed to create database", "error", err)
return
Expand All @@ -41,3 +59,11 @@ func main() {

server.New(l, ":8080", server.WithHandler(r)).StartAndWait()
}

func getDatabaseType() db.DatabaseType {
dbType := os.Getenv("DB_TYPE")
if dbType == "" {
return db.DatabaseTypeFile
}
return db.DatabaseType(dbType)
}
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.0.1

0 comments on commit b31d834

Please sign in to comment.