Skip to content

Commit

Permalink
deploy: add healthcheck
Browse files Browse the repository at this point in the history
unfortunately healthchecks do not work with scratch images since they do not have systemd, thats why switch runner image to alpine
  • Loading branch information
dartt0n committed Sep 19, 2024
1 parent 2fb964d commit e79d478
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
48 changes: 48 additions & 0 deletions cmd/healthcheck/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"net/http"
"os"
"strings"

"github.com/go-playground/validator/v10"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/v2"
)

type Config struct {
Addr string `koanf:"addr" validate:"required" json:"addr"`
}

func main() {
conf := Config{Addr: "localhost:8080"}

k := koanf.New(".")
k.Load(env.Provider("UNOTONE_", ".", func(s string) string {
s = strings.TrimPrefix(s, "UNOTONE_")
s = strings.ToLower(s)
return s
}), nil)
k.Unmarshal("", &conf)

validate := validator.New(validator.WithRequiredStructEnabled())

if err := validate.Struct(&conf); err != nil {
fmt.Printf("failed ot validate config: %v\n", err)
os.Exit(1)
}

resp, err := http.Get(fmt.Sprintf("http://%s/health", conf.Addr))
if err != nil {
fmt.Printf("failed to get healthcheck: %v\n", err)
os.Exit(1)
}

if resp.StatusCode != http.StatusOK {
fmt.Errorf("health check failed with status code %d\n", resp.StatusCode)
os.Exit(1)
}

os.Exit(0)
}
8 changes: 6 additions & 2 deletions dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ COPY cmd ./cmd
COPY controllers ./controllers
COPY --from=templbuilder /build/controllers/web/components/*_templ.go ./controllers/web/components/
RUN go build -ldflags="-s -w" -o /app/unotone ./cmd/server/main.go
RUN go build -ldflags="-s -w" -o /app/healthcheck ./cmd/healthcheck/main.go
RUN chmod +x /app/unotone

# runner image
FROM scratch
FROM alpine
WORKDIR /app
ENV UNOTONE_STATIC_DIR /var/www/static
ENV PATH /app:$PATH
COPY --from=gobuilder /build/controllers/web/static ${UNOTONE_STATIC_DIR}
COPY --from=tailwindbuilder /build/static/output.css ${UNOTONE_STATIC_DIR}/output.css
COPY --from=gobuilder /app/unotone /app/unotone
CMD ["./unotone"]
COPY --from=gobuilder /app/healthcheck /app/healthcheck
HEALTHCHECK --start-period=1s --interval=1m --timeout=3s --retries=3 CMD ["healthcheck"]
CMD ["unotone"]
5 changes: 5 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ run-pod: podman-build
--restart unless-stopped \
--name unotone-server \
--detach \
--health-cmd 'healthcheck --port 8080' \
--health-start-period 1s \
--health-interval 1m \
--health-timeout 3s \
--health-retries 3 \
-e UNOTONE_ADDR=0.0.0.0:8080 \
-e UNOTONE_DEBUG={{ DEBUG }} \
unotone-server:latest
Expand Down

0 comments on commit e79d478

Please sign in to comment.