From 7ff78ce1f74d768a07e219464355cb5c45bf1030 Mon Sep 17 00:00:00 2001 From: James Mills Date: Tue, 10 Aug 2021 23:19:30 +1000 Subject: [PATCH] Fix Dockerfile image and add /health endpoint --- Dockerfile | 39 ++++++++++++++++++++++++++++++++++++--- Makefile | 4 ++-- app.go | 1 + handlers.go | 5 +++++ version.go | 9 +++------ 5 files changed, 47 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7a54b74..7d2f8e9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,49 @@ # Build -FROM prologic/go-builder:latest AS build +FROM golang:alpine AS build + +RUN apk add --no-cache -U build-base git make + +RUN mkdir -p /src + +WORKDIR /src + +# Copy Makefile +COPY Makefile ./ + +# Copy go.mod and go.sum and install and cache dependencies +COPY go.mod . +COPY go.sum . + +# Install deps +RUN go mod download + +# Copy sources +COPY *.go ./ + +# Version/Commit (there there is no .git in Docker build context) +# NOTE: This is fairly low down in the Dockerfile instructions so +# we don't break the Docker build cache just be changing +# unrelated files that actually haven't changed but caused the +# COMMIT value to change. +ARG VERSION="0.0.0" +ARG COMMIT="HEAD" + +# Build server binary +RUN make VERSION=$VERSION COMMIT=$COMMIT # Runtime FROM alpine:latest -RUN apk --no-cache -U add ca-certificates +RUN apk --no-cache -U add curl ca-certificates tzdata WORKDIR / VOLUME /feeds -COPY .dockerfiles/config.yaml /config.yaml +# force cgo resolver +ENV GODEBUG=netdns=cgo + COPY --from=build /src/rss2twtxt /rss2twtxt +HEALTHCHECK CMD curl -qsfSL http://127.0.0.1:8000/health || exit 1 ENTRYPOINT ["/rss2twtxt"] CMD [""] diff --git a/Makefile b/Makefile index 74cad29..83e3918 100644 --- a/Makefile +++ b/Makefile @@ -13,8 +13,8 @@ build: @go build \ -tags "netgo static_build" -installsuffix netgo \ -ldflags "-w \ - -X $(shell go list).Version=$(VERSION) \ - -X $(shell go list).Commit=$(COMMIT)" \ + -X main.Version=$(VERSION) \ + -X main.Commit=$(COMMIT)" \ . install: build diff --git a/app.go b/app.go index eb4f5cd..afb2c6a 100644 --- a/app.go +++ b/app.go @@ -39,6 +39,7 @@ func (app *App) initRoutes() *mux.Router { router := mux.NewRouter().StrictSlash(true) router.HandleFunc("/", app.IndexHandler).Methods(http.MethodGet, http.MethodHead, http.MethodPost) + router.HandleFunc("/health", app.HealthHandler).Methods(http.MethodGet, http.MethodHead) router.HandleFunc("/feeds", app.FeedsHandler).Methods(http.MethodGet, http.MethodHead) router.HandleFunc("/we-are-feeds.txt", app.WeAreFeedsHandler).Methods(http.MethodGet, http.MethodHead) router.HandleFunc("/{name}/twtxt.txt", app.FeedHandler).Methods(http.MethodGet, http.MethodHead) diff --git a/handlers.go b/handlers.go index 12567e3..181c931 100644 --- a/handlers.go +++ b/handlers.go @@ -43,6 +43,11 @@ func renderMessage(w http.ResponseWriter, status int, title, message string) err return nil } +func (app *App) HealthHandler(w http.ResponseWriter, r *http.Request) { + defer r.Body.Close() + return +} + func (app *App) IndexHandler(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodHead || r.Method == http.MethodGet { w.Header().Set("Content-Type", "text/html") diff --git a/version.go b/version.go index f788310..5c769b1 100644 --- a/version.go +++ b/version.go @@ -8,14 +8,11 @@ var ( // Version release version Version = "0.0.1" - // Build will be overwritten automatically by the build system - Build = "dev" - - // GitCommit will be overwritten automatically by the build system - GitCommit = "HEAD" + // Commit will be overwritten automatically by the build system + Commit = "HEAD" ) // FullVersion returns the full version, build and commit hash func FullVersion() string { - return fmt.Sprintf("%s-%s@%s", Version, Build, GitCommit) + return fmt.Sprintf("%s@%s", Version, Commit) }