Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Commit 9afe969

Browse files
Switch to Postgres (#64)
Also add a Dockerfile that can build a container image for the app.
1 parent de234da commit 9afe969

25 files changed

+129
-638
lines changed

.circleci/config.yml

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ jobs:
66
integration-test:
77
docker:
88
- image: circleci/golang:1.15
9-
- image: cockroachdb/cockroach:v20.1.4
10-
command: [start-single-node, --insecure, --listen-addr, ":26258"]
9+
- image: postgres:12
10+
environment:
11+
POSTGRES_USER: ahead
12+
POSTGRES_PASSWORD: 123
1113
environment:
1214
TEST_RESULTS: /tmp/test-results
1315
steps:
@@ -23,8 +25,8 @@ jobs:
2325
paths:
2426
- "/go/pkg/mod"
2527
- run:
26-
name: Waiting for CockroachDB to be ready
27-
command: dockerize -wait tcp://localhost:26258 -timeout 1m
28+
name: Waiting for Postgres to be ready
29+
command: dockerize -wait tcp://localhost:5432 -timeout 1m
2830
- run:
2931
name: Run tests
3032
command: |

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/data/

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/ahead
22
/cert.pem
3-
/certs/
4-
/cockroach-data/
53
/cover.out
4+
/data/
65
/generate_cert.go
76
/key.pem
87
/node_modules/

Dockerfile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM golang:1.15-buster AS builder
2+
WORKDIR /src
3+
4+
# Copy go.mod and go.sum first, to enable caching of dependencies even after local code changes
5+
COPY go.mod go.sum ./
6+
RUN go mod download -x
7+
8+
COPY . ./
9+
RUN go build -v -o /bin/server cmd/server/*.go
10+
11+
FROM debian:buster-slim
12+
RUN set -x && apt-get update && \
13+
DEBIAN_FRONTEND=noninteractive apt-get install -y ca-certificates && \
14+
rm -rf /var/lib/apt/lists/*
15+
16+
WORKDIR /app
17+
COPY development.toml ./
18+
COPY --from=builder /bin/server ./
19+
20+
CMD ["./server", "-config", "development.toml"]

Makefile

+11-24
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
.PHONY: bindata build build-css certs clean cockroach-certs cockroach-sql cover down lint \
2-
migrate-create migrate-goto migrate-up start test test-down test-integration test-up up
1+
.PHONY: build build-css certs clean cover down lint migrate-create migrate-down migrate-goto migrate-up start test test-down test-integration test-up up up-build
32

43
export NAME := ahead
54
export VERSION := `git rev-parse --short HEAD`
6-
export MIGRATE_DB_URL := "cockroachdb://root@localhost:26257/${NAME}?sslmode=verify-full&sslcert=certs/client.root.crt&sslkey=certs/client.root.key&sslrootcert=certs/ca.crt"
7-
8-
bindata:
9-
go-bindata -nometadata -pkg storage -o storage/migrations.go -ignore '.*\.go' -ignore '.DS_Store' -prefix storage/migrations storage/migrations
5+
export MIGRATE_DB_URL := "postgres://${NAME}:123@localhost:5432/${NAME}?sslmode=disable"
106

117
build:
128
sed -i.bak "s/VERSION/${VERSION}/g" cmd/server/version.go
@@ -21,19 +17,7 @@ certs: generate_cert.go
2117
go run generate_cert.go --rsa-bits=2048 --host=localhost
2218

2319
clean:
24-
rm -rf certs
25-
rm -rf cockroach-data
26-
27-
cockroach-certs:
28-
rm -rf certs
29-
mkdir -p certs/my-safe-directory
30-
cockroach cert create-ca --certs-dir=certs --ca-key=certs/my-safe-directory/ca.key
31-
cockroach cert create-node db localhost 127.0.0.1 --certs-dir=certs --ca-key=certs/my-safe-directory/ca.key
32-
cockroach cert create-client root --certs-dir=certs --ca-key=certs/my-safe-directory/ca.key
33-
cockroach cert create-client ${NAME} --certs-dir=certs --ca-key=certs/my-safe-directory/ca.key
34-
35-
cockroach-sql:
36-
cockroach sql --certs-dir certs
20+
rm -rf data
3721

3822
cover:
3923
go tool cover -html=cover.out
@@ -50,6 +34,9 @@ lint:
5034
migrate-create:
5135
migrate create -ext sql -dir storage/migrations -seq $(name)
5236

37+
migrate-down:
38+
migrate -path storage/migrations -database ${MIGRATE_DB_URL} down
39+
5340
migrate-goto:
5441
migrate -path storage/migrations -database ${MIGRATE_DB_URL} goto $(version)
5542

@@ -72,9 +59,9 @@ test-up:
7259
docker-compose -p ${NAME}-test -f docker-compose-test.yaml up -d
7360

7461
up:
75-
mkdir -p cockroach-data
62+
mkdir -p data
7663
docker-compose -p ${NAME} up -d
77-
sleep 1
78-
cockroach sql --certs-dir certs -e "create database if not exists ${NAME};"
79-
cockroach sql --certs-dir certs -e "create user if not exists ${NAME};"
80-
cockroach sql --certs-dir certs -e "grant select, insert, update, delete on database ${NAME} to ${NAME};"
64+
65+
up-build:
66+
mkdir -p data
67+
docker-compose -p ${NAME} up --build -d

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A SaaS web app starter template written in Go.
88

99
- Web app with best practice project layout and single-binary deployment
1010
- Stateless app ready for working behind a load balancer, such as [Caddy](https://caddyserver.com)
11-
- Storage using [CockroachDB](https://www.cockroachlabs.com) (but can be easily changed to another database system)
11+
- Storage using [Postgres](https://www.postgresql.org) (but can be easily changed to another database system)
1212
- Metrics using [Prometheus](https://prometheus.io)
1313
- Transactional emails using [Postmark](https://postmarkapp.com)
1414
- Unit and integration tests using [Docker Compose](https://docs.docker.com/compose/),

cmd/server/config.go

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
// Config holds configuration data read from a config file.
1010
type Config struct {
11-
Name string
1211
ExternalHost string
1312
ExternalPort int
1413
InternalHost string

cmd/server/main.go

-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func main() {
2929
ExternalPort: c.ExternalPort,
3030
InternalHost: c.InternalHost,
3131
InternalPort: c.InternalPort,
32-
Name: c.Name,
3332
Version: Version,
3433
Cert: c.Cert,
3534
Key: c.Key,
@@ -43,7 +42,6 @@ func main() {
4342
func createStorer(c Config, logger *log.Logger) *storage.Storer {
4443
return storage.NewStorer(storage.NewStorerOptions{
4544
Logger: logger,
46-
AppName: c.Name,
4745
User: c.Storer.User,
4846
Host: c.Storer.Host,
4947
Port: c.Storer.Port,

development.toml

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
Name = "ahead"
21
ExternalHost = "localhost"
32
ExternalPort = 2020
43
InternalHost = "localhost"
@@ -7,10 +6,8 @@ Cert = "cert.pem"
76
Key = "key.pem"
87

98
[Storer]
10-
Host = "localhost"
11-
Port = 26257
129
User = "ahead"
10+
Password = "123"
11+
Host = "localhost"
12+
Port = 5432
1313
Database = "ahead"
14-
Cert = "certs/client.ahead.crt"
15-
Key = "certs/client.ahead.key"
16-
RootCert = "certs/ca.crt"

docker-compose-test.yaml

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
version: '3.7'
1+
version: '3.8'
22
services:
33
db:
4-
command: start-single-node --insecure --listen-addr=0.0.0.0:26257
5-
image: cockroachdb/cockroach:v20.1.4
4+
image: postgres:12
5+
environment:
6+
POSTGRES_USER: ahead
7+
POSTGRES_PASSWORD: 123
68
ports:
7-
- 26258:26257
9+
- 5432:5432

docker-compose.yaml

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
version: '3.7'
1+
version: '3.8'
22
services:
33
db:
4-
command: start-single-node --certs-dir=/certs --store=/cockroach-data --listen-addr=0.0.0.0:26257
5-
image: cockroachdb/cockroach:v20.1.4
4+
image: postgres:12
5+
environment:
6+
POSTGRES_USER: ahead
7+
POSTGRES_PASSWORD: 123
68
ports:
7-
- 26257:26257
9+
- 5432:5432
810
volumes:
911
- type: bind
10-
source: ./certs
11-
target: /certs
12-
- type: bind
13-
source: ./cockroach-data
14-
target: /cockroach-data
12+
source: ./data
13+
target: /var/lib/postgresql/data
14+
app:
15+
build:
16+
context: ./
17+
dockerfile: Dockerfile
18+
ports:
19+
- "2020:2020"
20+
- "2021:2021"
21+
depends_on:
22+
- db
23+
restart: always

docker.toml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
ExternalHost = "localhost"
2+
ExternalPort = 2020
3+
InternalHost = "localhost"
4+
InternalPort = 2021
5+
Cert = "cert.pem"
6+
Key = "key.pem"
7+
8+
[Storer]
9+
User = "ahead"
10+
Password = "123"
11+
Host = "db"
12+
Port = 5432
13+
Database = "ahead"

storage/migrate.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package storage
22

33
import (
44
"github.com/golang-migrate/migrate/v4"
5-
_ "github.com/golang-migrate/migrate/v4/database/cockroachdb"
6-
"github.com/golang-migrate/migrate/v4/source/go_bindata"
5+
_ "github.com/golang-migrate/migrate/v4/database/postgres"
6+
_ "github.com/golang-migrate/migrate/v4/source/file"
77

88
"github.com/maragudk/go-ahead/errors2"
99
)
@@ -37,17 +37,8 @@ func (s *Storer) MigrateDown() error {
3737
}
3838

3939
func (s *Storer) getMigrator() (*migrate.Migrate, error) {
40-
// From https://github.com/golang-migrate/migrate/tree/master/source/go_bindata
41-
source := bindata.Resource(AssetNames(), func(name string) ([]byte, error) {
42-
return Asset(name)
43-
})
44-
driver, err := bindata.WithInstance(source)
45-
if err != nil {
46-
return nil, errors2.Wrap(err, "could not create bindata source driver")
47-
}
48-
dataSourceName := s.createDataSourceName(true)
49-
s.log.Println("Connecting on", dataSourceName)
50-
m, err := migrate.NewWithSourceInstance("go-bindata", driver, dataSourceName)
40+
dataSourceName := s.createDataSourceName()
41+
m, err := migrate.New("file://migrations", dataSourceName)
5142
if err != nil {
5243
return nil, errors2.Wrap(err, "could not create migrator")
5344
}

storage/migrate_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ func TestStorer_MigrateUp(t *testing.T) {
2525
}
2626

2727
func TestStorer_MigrateDown(t *testing.T) {
28-
t.Run("migrates down to version 1", func(t *testing.T) {
29-
s, cleanup := storagetest.CreateRootStorer()
30-
defer cleanup()
28+
t.Run("migrates down", func(t *testing.T) {
29+
s, _ := storagetest.CreateRootStorer()
3130

3231
err := s.MigrateDown()
3332
require.NoError(t, err)

0 commit comments

Comments
 (0)