From bdb2fcc68f9488563abb51de9200e9e927cd8bd2 Mon Sep 17 00:00:00 2001 From: amirHosein Date: Wed, 13 May 2026 10:45:07 +0330 Subject: [PATCH] fix(devops): Dockerfile & .local.env support added --- .gitignore | 10 +++++----- Dockerfile | 42 +++++++++++++++++++++++++++++++++++++++ fly.Dockerfile | 40 ------------------------------------- internal/config/config.go | 13 ++++++++++++ linux.Dockerfile | 37 ---------------------------------- 5 files changed, 60 insertions(+), 82 deletions(-) create mode 100644 Dockerfile delete mode 100644 fly.Dockerfile delete mode 100644 linux.Dockerfile diff --git a/.gitignore b/.gitignore index 7d49e64..429b27b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,20 +1,20 @@ -# Executable names /scribblers *.exe __debug_bin -# IntelliJ state + .idea/ +.vscode +.DS_Store # Folder that contains code used for trying stuff locally playground/ # Temporary output text files *.out -# System metadata files -.DS_Store # Configuration files -.env +.local.env # Anything temporary *.tmp # Profiling results *.pprof # Additional files for hosting. Workaround for now ig. public/ + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..07cb33c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,42 @@ +FROM docker.io/golang:1.25.5 AS builder + +WORKDIR /app + +# 1. Cache modules +# RUN go env -w GOPROXY=https://package-mirror.liara.ir/repository/go/ +# RUN go env -w GOSUMDB=off +COPY go.mod go.sum ./ +RUN go mod download -x + +# 2. Copy source code +COPY . . + +# 3. Build arguments +ARG VERSION="dev" + +# 4. Build the binary +ENV CGO_ENABLED=0 +RUN go build -trimpath -ldflags "-w -s -X 'medad.fun/internal/version.Version=${VERSION}'" -tags timetzdata -o /s ./cmd/main.go + +# ----------------------------------------- +# Stage 2: Runner +# ----------------------------------------- +FROM scratch + +# 1. Copy the binary (CRITICAL) +COPY --from=builder /s /dist + +# 2. Copy public assets (FIXED syntax) +COPY .env /.env +# COPY --from=builder /public /public + +# 3. Copy SSL certs (UNCOMMENTED) +# COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ + +# 4. Security +USER 248:248 + +EXPOSE 3000 + +# 5. Entry point +ENTRYPOINT ["/dist"] \ No newline at end of file diff --git a/fly.Dockerfile b/fly.Dockerfile deleted file mode 100644 index 7224084..0000000 --- a/fly.Dockerfile +++ /dev/null @@ -1,40 +0,0 @@ -# -# Builder for Golang -# -# We explicitly use a certain major version of go, to make sure we don't build -# with a newer verison than we are using for CI tests, as we don't directly -# test the produced binary but from source code directly. -FROM docker.io/golang:1.25.5 AS builder - -WORKDIR /app - -# This causes caching of the downloaded go modules and makes repeated local -# builds much faster. We must not copy the code first though, as a change in -# the code causes a redownload. -COPY go.mod go.sum ./ -RUN go mod download -x - -# Import that this comes after mod download, as it breaks caching. -ARG VERSION="dev" - -# Copy actual codebase, since we only have the go.mod and go.sum so far. -COPY . /app/ -ENV CGO_ENABLED=0 -RUN go build -trimpath -ldflags "-w -s -X 'github.com/scribble-rs/scribble.rs/internal/version.Version=${VERSION}'" -tags timetzdata -o ./scribblers ./cmd/scribblers - -# -# Runner -# -FROM scratch - -# Additionally hosted files -COPY public /public - -COPY --from=builder /app/scribblers /scribblers -# The scratch image doesn't contain any certificates, therefore we use -# the builders certificate, so that we can send HTTP requests. -COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ - -ENTRYPOINT ["/scribblers"] -# Random uid to avoid having root privileges. Linux doesn't care that there's no user for it. -USER 248:248 diff --git a/internal/config/config.go b/internal/config/config.go index c5eb579..dfc66e7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -133,6 +133,19 @@ func Load() (*Config, error) { maps.Copy(envVars, envFileContent) } + dotEnvPath = ".local.env" + if _, err := os.Stat(dotEnvPath); err != nil { + if !os.IsNotExist(err) { + return nil, fmt.Errorf("error checking for existence of .local.env file: %w", err) + } + } else { + envFileContent, err := gotenv.Read(dotEnvPath) + if err != nil { + return nil, fmt.Errorf("error reading .local.env file: %w", err) + } + maps.Copy(envVars, envFileContent) + } + // Add local environment variables to EnvVars map for _, keyValuePair := range os.Environ() { pair := strings.SplitN(keyValuePair, "=", 2) diff --git a/linux.Dockerfile b/linux.Dockerfile deleted file mode 100644 index bd8cadf..0000000 --- a/linux.Dockerfile +++ /dev/null @@ -1,37 +0,0 @@ -# -# Builder for Golang -# -# We explicitly use a certain major version of go, to make sure we don't build -# with a newer verison than we are using for CI tests, as we don't directly -# test the produced binary but from source code directly. -FROM docker.io/golang:1.25.5 AS builder - -WORKDIR /app - -# This causes caching of the downloaded go modules and makes repeated local -# builds much faster. We must not copy the code first though, as a change in -# the code causes a redownload. -COPY go.mod go.sum ./ -RUN go mod download -x - -# Import that this comes after mod download, as it breaks caching. -ARG VERSION="dev" - -# Copy actual codebase, since we only have the go.mod and go.sum so far. -COPY . /app/ -ENV CGO_ENABLED=0 -RUN go build -trimpath -ldflags "-w -s -X 'github.com/scribble-rs/scribble.rs/internal/version.Version=${VERSION}'" -tags timetzdata -o ./scribblers ./cmd/scribblers - -# -# Runner -# -FROM scratch - -COPY --from=builder /app/scribblers /scribblers -# The scratch image doesn't contain any certificates, therefore we use -# the builders certificate, so that we can send HTTP requests. -COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ - -ENTRYPOINT ["/scribblers"] -# Random uid to avoid having root privileges. Linux doesn't care that there's no user for it. -USER 248:248