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

1
go.mod
View File

@@ -6,6 +6,7 @@ require github.com/urfave/cli/v2 v2.25.7
require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/lmittmann/tint v1.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)

2
go.sum
View File

@@ -1,5 +1,7 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/lmittmann/tint v1.0.2 h1:9XZ+JvEzjvd3VNVugYqo3j+dl0NRju8k9FquAusJExM=
github.com/lmittmann/tint v1.0.2/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=

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