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.

This commit is contained in:
spinach 2025-02-19 20:37:37 -05:00
parent 7088a05f8e
commit 5ad7a6bb13
8 changed files with 27 additions and 11 deletions

4
.gitignore vendored
View File

@ -21,4 +21,6 @@
# Go workspace file
go.work
.env
priv
# ensures readme still available
!priv/readme.md

8
Makefile Normal file
View File

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

View File

@ -1,4 +0,0 @@
#! /usr/bin/env bash
# ensures correct tag is applied
docker build . -t adonis

View File

View File

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

View File

@ -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:

View File

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

View File

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