Color logging. Timeouts
All checks were successful
Go / build (push) Successful in 1m20s

This commit is contained in:
2023-10-27 17:21:21 +13:00
parent 2064a593d1
commit 373013a74d
3 changed files with 22 additions and 7 deletions

26
main.go
View File

@@ -8,6 +8,7 @@ import (
"os"
"time"
"github.com/lmittmann/tint"
"github.com/urfave/cli/v2"
)
@@ -73,11 +74,11 @@ func pingHandler(w http.ResponseWriter, r *http.Request) {
func main() {
logLevel := &slog.LevelVar{} // INFO
opts := slog.HandlerOptions{
opts := tint.Options{
Level: logLevel,
}
logLevel.Set(slog.LevelDebug)
handler := slog.NewTextHandler(os.Stdout, &opts)
handler := tint.NewHandler(os.Stdout, &opts)
logger := slog.New(handler)
slog.SetDefault(logger)
@@ -98,19 +99,27 @@ func main() {
Action: func(cCtx *cli.Context) error {
var addr string = host + ":" + port
srv := &http.Server{
Addr: addr,
}
mux := http.NewServeMux()
mux.Handle("/ping", http.HandlerFunc(pingHandler))
fileHandler := http.FileServer(http.Dir(directory))
mux.Handle("/", fileHandler)
srv := &http.Server{
Addr: addr,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
srv.Handler = WithLogging(mux)
listener, err := net.Listen("tcp", addr)
if err != nil {
slog.Error("Listen error", err)
}
slog.Info(fmt.Sprintf("Serving directory %q on http://%v", directory, listener.Addr()))
slog.Info("Starting server",
slog.String("dir", directory),
slog.String("addr", fmt.Sprintf(":%s", listener.Addr())),
)
err = srv.Serve(listener)
if err != nil {
slog.Error("Serve error", err)
@@ -141,7 +150,10 @@ func main() {
Action: func(cCtx *cli.Context) error {
url := fmt.Sprintf("http://127.0.0.1:%s/ping", port)
slog.Debug("Healthcheck: ", slog.String("url", url))
res, err := http.Get(url)
client := http.Client{
Timeout: 1 * time.Second,
}
res, err := client.Get(url)
if err != nil {
return cli.Exit("FAIL", 1)
}