Browse Source

Fix Dockerfile image and add /health endpoint

master
James Mills 2 months ago
parent
commit
7ff78ce1f7
Signed by: prologic GPG Key ID: AC4C014F1440EBD6
  1. 39
      Dockerfile
  2. 4
      Makefile
  3. 1
      app.go
  4. 5
      handlers.go
  5. 9
      version.go

39
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 [""]

4
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

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

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

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

Loading…
Cancel
Save