diff --git a/Dockerfile b/Dockerfile index 5e7713c..3576334 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,15 +3,16 @@ WORKDIR /build COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN go build -o ipinfo . +RUN go build -ldflags="-s -w" -o ipinfo . +RUN go build -ldflags="-s -w" -o healthcheck ./healthcheck/healthcheck.go RUN go install github.com/maxmind/geoipupdate/v7/cmd/geoipupdate@latest -RUN mkdir -p /build/data FROM alpine:latest RUN apk add --no-cache curl tzdata busybox-suid WORKDIR /app COPY --from=builder /build/ipinfo . +COPY --from=builder /build/healthcheck . COPY --from=builder /go/bin/geoipupdate /usr/local/bin/geoipupdate ENV GEOIPUPDATE_ACCOUNT_ID=${GEOIPUPDATE_ACCOUNT_ID} @@ -27,8 +28,8 @@ RUN echo "AccountID ${GEOIPUPDATE_ACCOUNT_ID}" > /etc/GeoIP.conf && \ RUN echo "0 0 * * * geoipupdate >> /var/log/geoipupdate.log 2>&1" > /etc/crontabs/root RUN cat /etc/crontabs/root -HEALTHCHECK --interval=30s --timeout=5s --start-period=5s CMD curl --fail http://localhost:3000/health || exit 1 +HEALTHCHECK --interval=30s --timeout=5s --start-period=5s CMD ["./healthcheck"] EXPOSE 3000 -CMD ["sh", "-c", "geoipupdate && ./ipinfo"] +CMD ["./ipinfo"] diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 0543021..92033b9 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -1,6 +1,8 @@ services: ipinfo: - build: . + build: + context: . + dockerfile: Dockerfile container_name: ipinfo restart: unless-stopped ports: @@ -8,11 +10,3 @@ services: environment: GEOIPUPDATE_ACCOUNT_ID: ${GEOIPUPDATE_ACCOUNT_ID} GEOIPUPDATE_LICENSE_KEY: ${GEOIPUPDATE_LICENSE_KEY} - GEOIPUPDATE_EDITION_IDS: "GeoLite2-City GeoLite2-ASN" - GEOIPUPDATE_DB_DIR: /app - healthcheck: - test: ["CMD", "curl", "-f", "http://localhost:3000/health"] - interval: 30s - timeout: 10s - retries: 3 - start_period: 40s diff --git a/docker-compose.yml b/docker-compose.yml index a6f3140..592bb88 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,11 +8,3 @@ services: environment: GEOIPUPDATE_ACCOUNT_ID: ${GEOIPUPDATE_ACCOUNT_ID} GEOIPUPDATE_LICENSE_KEY: ${GEOIPUPDATE_LICENSE_KEY} - GEOIPUPDATE_EDITION_IDS: "GeoLite2-City GeoLite2-ASN" - GEOIPUPDATE_DB_DIR: /app - healthcheck: - test: ["CMD", "curl", "-f", "http://localhost:3000/health"] - interval: 30s - timeout: 10s - retries: 3 - start_period: 40s diff --git a/go.mod b/go.mod index 1fccb66..a728ea9 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module skidoodle/ipinfo -go 1.22.4 +go 1.24.0 require github.com/oschwald/maxminddb-golang v1.13.1 -require golang.org/x/sys v0.29.0 // indirect +require golang.org/x/sys v0.30.0 // indirect diff --git a/go.sum b/go.sum index 91c8558..deb5072 100644 --- a/go.sum +++ b/go.sum @@ -8,5 +8,7 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= +golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/healthcheck/go.mod b/healthcheck/go.mod new file mode 100644 index 0000000..8abade9 --- /dev/null +++ b/healthcheck/go.mod @@ -0,0 +1,3 @@ +module healthcheck + +go 1.24.0 diff --git a/healthcheck/healthcheck.go b/healthcheck/healthcheck.go new file mode 100644 index 0000000..97f2601 --- /dev/null +++ b/healthcheck/healthcheck.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "os" +) + +func main() { + resp, err := http.Get("http://localhost:3000/health") + if err != nil { + log.Fatalf("Error performing health check: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + log.Printf("Health check failed: Status code %d", resp.StatusCode) + os.Exit(1) + } + + fmt.Println("OK") + os.Exit(0) +} diff --git a/readme.md b/readme.md index 49b18ec..4ec2705 100644 --- a/readme.md +++ b/readme.md @@ -48,10 +48,8 @@ cd ipinfo docker build -t ipinfo:main . docker run \ -p 3000:3000 --e GEOIPUPDATE_ACCOUNT_ID=<> \ --e GEOIPUPDATE_LICENSE_KEY=<> \ --e GEOIPUPDATE_EDITION_IDS=<> \ --e GEOIPUPDATE_DB_DIR=<> \ +-e GEOIPUPDATE_ACCOUNT_ID=${GEOIPUPDATE_ACCOUNT_ID} \ +-e GEOIPUPDATE_LICENSE_KEY=${GEOIPUPDATE_LICENSE_KEY} \ ipinfo:main ``` @@ -78,14 +76,6 @@ services: environment: GEOIPUPDATE_ACCOUNT_ID: ${GEOIPUPDATE_ACCOUNT_ID} GEOIPUPDATE_LICENSE_KEY: ${GEOIPUPDATE_LICENSE_KEY} - GEOIPUPDATE_EDITION_IDS: "GeoLite2-City GeoLite2-ASN" - GEOIPUPDATE_DB_DIR: /app - healthcheck: - test: ["CMD", "curl", "-f", "http://localhost:3000/"] - interval: 30s - timeout: 10s - retries: 3 - start_period: 40s ``` ### Docker Run @@ -96,10 +86,8 @@ docker run \ --name=ipinfo \ --restart=unless-stopped \ -p 3000:3000 \ - -e GEOIPUPDATE_ACCOUNT_ID=<> \ - -e GEOIPUPDATE_LICENSE_KEY=<> \ - -e GEOIPUPDATE_EDITION_IDS=<> \ - -e GEOIPUPDATE_DB_DIR=<> \ + -e GEOIPUPDATE_ACCOUNT_ID=${GEOIPUPDATE_ACCOUNT_ID} \ + -e GEOIPUPDATE_LICENSE_KEY=${GEOIPUPDATE_LICENSE_KEY} \ ghcr.io/skidoodle/ipinfo:main ``` diff --git a/utils/health/health.go b/utils/health/health.go index 7ea7d48..ebf5932 100644 --- a/utils/health/health.go +++ b/utils/health/health.go @@ -9,7 +9,7 @@ func HealthCheck() http.Handler { mux := http.NewServeMux() mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - w.Write([]byte("Healthy")) + w.Write([]byte("OK")) }) return mux }