2023-12-04 10:22:49 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-12-04 15:59:10 +00:00
|
|
|
"context"
|
2023-12-04 10:22:49 +00:00
|
|
|
"errors"
|
|
|
|
"log/slog"
|
2023-12-04 15:59:10 +00:00
|
|
|
"net"
|
2023-12-04 10:22:49 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
2023-12-04 15:59:10 +00:00
|
|
|
|
|
|
|
"code.icb4dc0.de/prskr/nurse/api"
|
2023-12-04 10:22:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
*app
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *server) RunServer(ctx *cli.Context) error {
|
|
|
|
logger := slog.Default()
|
|
|
|
|
|
|
|
mux, err := api.PrepareMux(a.nurseInstance, a.registry, a.lookup)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to prepare server mux", slog.String("err", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
srv := http.Server{
|
2023-12-04 15:59:10 +00:00
|
|
|
Addr: ctx.String(httpAddressFlag),
|
|
|
|
ReadHeaderTimeout: ctx.Duration(httpReadHeaderTimeout),
|
|
|
|
BaseContext: func(listener net.Listener) context.Context {
|
|
|
|
return ctx.Context
|
|
|
|
},
|
|
|
|
Handler: mux,
|
2023-12-04 10:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := srv.ListenAndServe(); err != nil {
|
|
|
|
if errors.Is(err, http.ErrServerClosed) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Error("Failed to serve HTTP", slog.String("err", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|