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
HEALTHCHECK --timeout=3s CMD /snice healthcheck
HEALTHCHECK --timeout=10s CMD ["/snice", "healthcheck"]
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /snice /
VOLUME /srv
ENTRYPOINT ["/snice"]
CMD ["serve"]

39
main.go
View File

@@ -2,7 +2,6 @@ package main
import (
"fmt"
"log"
"log/slog"
"net"
"net/http"
@@ -67,19 +66,6 @@ func WithLogging(h http.Handler) http.Handler {
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) {
w.Write([]byte("OK"))
}
@@ -102,7 +88,7 @@ func main() {
app := &cli.App{
Name: "snice",
Usage: "Serve Static Files",
Version: "v0.1.0",
Version: "v0.2.0",
DefaultCommand: "",
Commands: []*cli.Command{
{
@@ -122,12 +108,12 @@ func main() {
srv.Handler = WithLogging(mux)
listener, err := net.Listen("tcp", addr)
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)
if err != nil {
logger.Error("Serve error", err)
slog.Error("Serve error", err)
}
return nil
},
@@ -136,7 +122,7 @@ func main() {
Name: "directory",
Aliases: []string{"dir", "d"},
EnvVars: []string{"DIRECTORY"},
Value: ".",
Value: "/srv",
Usage: "Directory to serve",
Destination: &directory,
},
@@ -153,12 +139,17 @@ func main() {
Aliases: []string{"hc"},
Usage: "Call healthcheck endpoint",
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 {
os.Exit(1)
return cli.Exit("FAIL", 1)
}
os.Exit(0)
return nil
if res.StatusCode == 200 {
return cli.Exit("OK", 0)
}
slog.Debug(fmt.Sprintf("Status: %d\n", res.StatusCode))
return cli.Exit("FAIL", 1)
}}},
Flags: []cli.Flag{
&cli.BoolFlag{Name: "quiet", Aliases: []string{"q"}},
@@ -186,6 +177,6 @@ func main() {
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
os.Exit(1)
}
}