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