Fix healthcheck. Use /srv
All checks were successful
Go / build (push) Successful in 1m22s

This commit is contained in:
2023-10-27 16:33:31 +13:00
parent 6f13e4021e
commit 2064a593d1
2 changed files with 18 additions and 25 deletions

View File

@@ -22,10 +22,12 @@ USER app
EXPOSE 3000 EXPOSE 3000
HEALTHCHECK --timeout=3s CMD /snice healthcheck HEALTHCHECK --timeout=10s CMD ["/snice", "healthcheck"]
COPY --from=builder /etc/passwd /etc/passwd COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /snice / COPY --from=builder /snice /
VOLUME /srv
ENTRYPOINT ["/snice"] ENTRYPOINT ["/snice"]
CMD ["serve"] CMD ["serve"]

39
main.go
View File

@@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"log"
"log/slog" "log/slog"
"net" "net"
"net/http" "net/http"
@@ -67,19 +66,6 @@ func WithLogging(h http.Handler) http.Handler {
return http.HandlerFunc(loggingFn) return http.HandlerFunc(loggingFn)
} }
func LoggingMiddleware(logger *slog.Logger, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
logger.Info("Request:",
slog.String("method", r.Method),
slog.String("path", r.RequestURI),
slog.String("url", r.URL.Path),
slog.String("host", r.Host),
)
})
}
func pingHandler(w http.ResponseWriter, r *http.Request) { func pingHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK")) w.Write([]byte("OK"))
} }
@@ -102,7 +88,7 @@ func main() {
app := &cli.App{ app := &cli.App{
Name: "snice", Name: "snice",
Usage: "Serve Static Files", Usage: "Serve Static Files",
Version: "v0.1.0", Version: "v0.2.0",
DefaultCommand: "", DefaultCommand: "",
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
@@ -122,12 +108,12 @@ func main() {
srv.Handler = WithLogging(mux) srv.Handler = WithLogging(mux)
listener, err := net.Listen("tcp", addr) listener, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
logger.Error("Listen error", err) slog.Error("Listen error", err)
} }
logger.Info(fmt.Sprintf("Serving directory %q on http://%v", directory, listener.Addr())) slog.Info(fmt.Sprintf("Serving directory %q on http://%v", directory, listener.Addr()))
err = srv.Serve(listener) err = srv.Serve(listener)
if err != nil { if err != nil {
logger.Error("Serve error", err) slog.Error("Serve error", err)
} }
return nil return nil
}, },
@@ -136,7 +122,7 @@ func main() {
Name: "directory", Name: "directory",
Aliases: []string{"dir", "d"}, Aliases: []string{"dir", "d"},
EnvVars: []string{"DIRECTORY"}, EnvVars: []string{"DIRECTORY"},
Value: ".", Value: "/srv",
Usage: "Directory to serve", Usage: "Directory to serve",
Destination: &directory, Destination: &directory,
}, },
@@ -153,12 +139,17 @@ func main() {
Aliases: []string{"hc"}, Aliases: []string{"hc"},
Usage: "Call healthcheck endpoint", Usage: "Call healthcheck endpoint",
Action: func(cCtx *cli.Context) error { Action: func(cCtx *cli.Context) error {
_, err := http.Get(fmt.Sprintf("http://127.0.0.1:%s/ping", port)) url := fmt.Sprintf("http://127.0.0.1:%s/ping", port)
slog.Debug("Healthcheck: ", slog.String("url", url))
res, err := http.Get(url)
if err != nil { if err != nil {
os.Exit(1) return cli.Exit("FAIL", 1)
} }
os.Exit(0) if res.StatusCode == 200 {
return nil return cli.Exit("OK", 0)
}
slog.Debug(fmt.Sprintf("Status: %d\n", res.StatusCode))
return cli.Exit("FAIL", 1)
}}}, }}},
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.BoolFlag{Name: "quiet", Aliases: []string{"q"}}, &cli.BoolFlag{Name: "quiet", Aliases: []string{"q"}},
@@ -186,6 +177,6 @@ func main() {
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
log.Fatal(err) os.Exit(1)
} }
} }