From 5ad7a6bb138f11f343476dc6a5d94adf0b069897 Mon Sep 17 00:00:00 2001 From: spinach Date: Wed, 19 Feb 2025 20:37:37 -0500 Subject: [PATCH] added caching to build stage to vastly reduce build times and moved docker stuff to separate folder. Also added priv folder to minimize chance of leaking secrets. Lastly removed build script and replaced with a simple makefile to rebuild container and spin up compose file. --- .gitignore | 4 +++- Makefile | 8 ++++++++ build.sh | 4 ---- env => config/env | 0 Dockerfile => docker/Dockerfile | 5 ++++- docker-compose.yml => docker/docker-compose.yml | 6 +++++- internal/db/db.go | 9 ++++++--- main.go | 2 +- 8 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 Makefile delete mode 100755 build.sh rename env => config/env (100%) rename Dockerfile => docker/Dockerfile (76%) rename docker-compose.yml => docker/docker-compose.yml (70%) diff --git a/.gitignore b/.gitignore index d43a39d..1f177a1 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,6 @@ # Go workspace file go.work -.env +priv +# ensures readme still available +!priv/readme.md diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b56558a --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +build: + docker build -t adonis -f docker/Dockerfile . + +up: build + docker compose -f docker/docker-compose.yml --env-file priv/env up + +clean: + go clean diff --git a/build.sh b/build.sh deleted file mode 100755 index c59bad5..0000000 --- a/build.sh +++ /dev/null @@ -1,4 +0,0 @@ -#! /usr/bin/env bash - -# ensures correct tag is applied -docker build . -t adonis diff --git a/env b/config/env similarity index 100% rename from env rename to config/env diff --git a/Dockerfile b/docker/Dockerfile similarity index 76% rename from Dockerfile rename to docker/Dockerfile index 74a48e2..0e7e134 100644 --- a/Dockerfile +++ b/docker/Dockerfile @@ -9,7 +9,10 @@ RUN go mod download # this could be simplified a lot by being stricter on what we copy. COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -o adonis . + +ENV GOCACHE=/root/.cache/go-build + +RUN --mount=type=cache,target="/root/.cache/go-build" CGO_ENABLED=0 GOOS=linux go build -o adonis . # copy only binary and static files to slim image size FROM alpine diff --git a/docker-compose.yml b/docker/docker-compose.yml similarity index 70% rename from docker-compose.yml rename to docker/docker-compose.yml index c53fcfb..9165381 100644 --- a/docker-compose.yml +++ b/docker/docker-compose.yml @@ -14,7 +14,11 @@ services: image: adonis:latest ports: - "42069:80" - env_file: ".env" + environment: + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} + - POSTGRES_USER=${POSTGRES_USER} + - POSTGRES_DB=${POSTGRES_DB} + - POSTGRES_HOSTNAME=${POSTGRES_HOSTNAME} depends_on: - postgres networks: diff --git a/internal/db/db.go b/internal/db/db.go index c7c25d4..d973687 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "os" + "time" "github.com/jackc/pgx/v5/pgxpool" ) @@ -19,9 +20,11 @@ func NewDatabase(username, password, hostname, database string) *Database { url := url(username, password, hostname, database) dbpool, err := pgxpool.New(context.Background(), url) - if err != nil { - fmt.Fprintf(os.Stderr, "Unable to create connection pool: %v\n", err) - os.Exit(1) + for err != nil { + dbpool, err = pgxpool.New(context.Background(), url) + fmt.Fprintf(os.Stderr, "Unable to create connection pool: %v\nRetrying...\n", err) + //os.Exit(1) + time.Sleep(1 * time.Second) } var greeting string err = dbpool.QueryRow(context.Background(), "select 'Hello, world!'").Scan(&greeting) diff --git a/main.go b/main.go index 78d16d4..5802d40 100644 --- a/main.go +++ b/main.go @@ -45,7 +45,7 @@ func main() { database := os.Getenv("POSTGRES_DB") hostname := os.Getenv("POSTGRES_HOSTNAME") - db.NewDatabase(username, password, hostname, database) + go db.NewDatabase(username, password, hostname, database) // attach webserver log.Fatal(http.ListenAndServe(":80", nil))