Compare commits
3 Commits
b9a06f5fd4
...
19304a8d8e
Author | SHA1 | Date | |
---|---|---|---|
19304a8d8e | |||
a04a01f6ca | |||
4cea479d42 |
@ -2,15 +2,17 @@ package application
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
|
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Application interface {
|
type Application interface {
|
||||||
Start(ctx context.Context) error
|
ExecuteContext(ctx context.Context) error
|
||||||
Shutdown() error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(c Config) (Application, error) {
|
func New(c Config) (Application, error) {
|
||||||
@ -31,53 +33,67 @@ type application struct {
|
|||||||
config Config
|
config Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *application) Start(ctx context.Context) error {
|
func (a *application) ExecuteContext(ctx context.Context) error {
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "configuring application shutdown signals", slog.Any("signals", a.config.ShutdownSignals))
|
||||||
|
|
||||||
sigCtx, sigStop := signal.NotifyContext(ctx, a.config.ShutdownSignals...)
|
sigCtx, sigCancel := signal.NotifyContext(ctx, a.config.ShutdownSignals...)
|
||||||
defer sigStop() // Ensure that this gets called.
|
defer sigCancel() // Ensure that this gets called.
|
||||||
|
|
||||||
// Result channel for command
|
// Result channel for command output
|
||||||
chExe := make(chan error)
|
chExe := make(chan error)
|
||||||
go a.execute(sigCtx, chExe)
|
|
||||||
|
|
||||||
|
// Run the application command
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "executing application context")
|
||||||
|
go func(ctx context.Context, chErr chan error) {
|
||||||
|
chErr <- a.cmd.ExecuteContext(ctx)
|
||||||
|
}(sigCtx, chExe)
|
||||||
|
|
||||||
|
// Wait for command output or a shutdown signal
|
||||||
|
var err error
|
||||||
select {
|
select {
|
||||||
// sigCtx.Done() returns a channel that will have a message
|
// sigCtx.Done() returns a channel that will have a message when the context is canceled.
|
||||||
// when the context is cancelled. We wait for that signal, which means
|
// Alternatively, chExe will receive the response from the execution context if the application finishes.
|
||||||
// we received the signal, or our context was cancelled for some other reason.
|
|
||||||
case <-sigCtx.Done():
|
case <-sigCtx.Done():
|
||||||
sigStop()
|
sigCancel()
|
||||||
return a.Shutdown()
|
|
||||||
case err := <-chExe:
|
// Adapt the shutdown scenario if a graceful shutdown period is configured
|
||||||
|
switch a.config.EnableGracefulShutdown && a.config.ShutdownTimeout > 0 {
|
||||||
|
case true:
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "gracefully shutting down application")
|
||||||
|
if err = a.gracefulShutdown(ctx); !errors.Is(err, context.DeadlineExceeded) {
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "graceful shutdown completed with error", slog.Any("error", err))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "graceful shutdown completed")
|
||||||
|
return nil
|
||||||
|
case false:
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "immediately shutting down application")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
case err = <-chExe:
|
||||||
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "application terminated successfully")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (a *application) Shutdown() error {
|
a.config.Logger.LogAttrs(ctx, slogd.LevelTrace, "application terminated unexpectedly")
|
||||||
switch a.config.EnableGracefulShutdown {
|
|
||||||
case true:
|
|
||||||
return a.gracefulShutdown()
|
|
||||||
case false:
|
|
||||||
return a.shutdown()
|
|
||||||
// default:
|
|
||||||
// return a.shutdown()
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *application) execute(ctx context.Context, chErr chan error) {
|
func (a *application) gracefulShutdown(ctx context.Context) error {
|
||||||
chErr <- a.cmd.ExecuteContext(ctx)
|
fmt.Printf("waiting %s for graceful application shutdown... PRESS CTRL+C again to quit now!\n", a.config.ShutdownTimeout)
|
||||||
}
|
|
||||||
|
|
||||||
func (a *application) gracefulShutdown() error {
|
shutdownCtx, shutdownCancel := context.WithTimeout(ctx, a.config.ShutdownTimeout)
|
||||||
fmt.Println("graceful shutdown")
|
|
||||||
|
|
||||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), a.config.ShutdownTimeout)
|
|
||||||
defer shutdownCancel()
|
defer shutdownCancel()
|
||||||
<-shutdownCtx.Done()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *application) shutdown() error {
|
// Wait for the shutdown timeout or a hard exit signal
|
||||||
fmt.Println("shutdown")
|
sigCtx, sigCancel := signal.NotifyContext(shutdownCtx, a.config.ShutdownSignals...)
|
||||||
return nil
|
defer sigCancel() // Ensure that this gets called.
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-shutdownCtx.Done():
|
||||||
|
return shutdownCtx.Err()
|
||||||
|
case <-sigCtx.Done():
|
||||||
|
sigCancel()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package application
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -15,8 +16,9 @@ type Config struct {
|
|||||||
Name string
|
Name string
|
||||||
Title string
|
Title string
|
||||||
Banner string
|
Banner string
|
||||||
Version string
|
Version Version
|
||||||
EnableGracefulShutdown bool
|
EnableGracefulShutdown bool
|
||||||
|
Logger *slog.Logger
|
||||||
OverrideRunE func(cmd *cobra.Command, args []string) error
|
OverrideRunE func(cmd *cobra.Command, args []string) error
|
||||||
PersistentPreRunE []func(cmd *cobra.Command, args []string) error // collection of PreRunE functions
|
PersistentPreRunE []func(cmd *cobra.Command, args []string) error // collection of PreRunE functions
|
||||||
PersistentPostRunE []func(cmd *cobra.Command, args []string) error // collection of PostRunE functions
|
PersistentPostRunE []func(cmd *cobra.Command, args []string) error // collection of PostRunE functions
|
||||||
@ -59,12 +61,7 @@ func (c Config) getRootCommand() (*cobra.Command, error) {
|
|||||||
cmd.AddCommand(subcommand.Initialize(c.SubCommandInitializeFunc))
|
cmd.AddCommand(subcommand.Initialize(c.SubCommandInitializeFunc))
|
||||||
}
|
}
|
||||||
|
|
||||||
var v semver.Version
|
configureVersionFlag(cmd, c.Version) // Configure app for version information
|
||||||
if v, err = c.ParseVersion(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
configureVersionFlag(cmd, v) // Configure app for version information
|
|
||||||
configureOutputFlags(cmd) // Configure verbosity
|
configureOutputFlags(cmd) // Configure verbosity
|
||||||
configureLoggingFlags(cmd) // Configure logging
|
configureLoggingFlags(cmd) // Configure logging
|
||||||
cmd.PersistentFlags().SetNormalizeFunc(normalizeFunc) // normalize persistent flags
|
cmd.PersistentFlags().SetNormalizeFunc(normalizeFunc) // normalize persistent flags
|
||||||
@ -73,7 +70,7 @@ func (c Config) getRootCommand() (*cobra.Command, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c Config) ParseVersion() (semver.Version, error) {
|
func (c Config) ParseVersion() (semver.Version, error) {
|
||||||
return semver.Parse(c.Version)
|
return semver.Parse(c.Version.Full)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Config) RegisterCommand(cmd Commander, f func(*cobra.Command)) {
|
func (c Config) RegisterCommand(cmd Commander, f func(*cobra.Command)) {
|
||||||
@ -103,7 +100,7 @@ func (c Config) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if _, err = semver.Parse(c.Version); err != nil {
|
if _, err = semver.Parse(c.Version.Full); err != nil {
|
||||||
return fmt.Errorf("invalid version: %s", c.Version)
|
return fmt.Errorf("invalid version: %s", c.Version)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -66,7 +66,7 @@ func persistentPreRunFuncE(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
// TODO move to front??
|
// TODO move to front??
|
||||||
if quietFlag {
|
if quietFlag {
|
||||||
slogd.FromContext(cmd.Context()).LogAttrs(cmd.Context(), slogd.LevelDebug, "activating quiet mode")
|
slogd.FromContext(cmd.Context()).LogAttrs(cmd.Context(), slogd.LevelTrace, "activating quiet mode")
|
||||||
outWriter = io.Discard
|
outWriter = io.Discard
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package application
|
package application
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log/slog"
|
||||||
|
|
||||||
|
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -10,20 +13,25 @@ const (
|
|||||||
LogOutputFile = "file"
|
LogOutputFile = "file"
|
||||||
)
|
)
|
||||||
|
|
||||||
var logLevelFlag string
|
var (
|
||||||
var logOutputFlag string
|
logLevelFlagName string = "log-level"
|
||||||
var logTypeFlag string
|
logLevelFlag string
|
||||||
|
logOutputFlagName string = "log-output"
|
||||||
|
logOutputFlag string
|
||||||
|
logTypeFlagName = "log-type"
|
||||||
|
logTypeFlag string
|
||||||
|
)
|
||||||
|
|
||||||
func addLogLevelFlag(cmd *cobra.Command) {
|
func addLogLevelFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().StringVarP(&logLevelFlag, "log-level", "", "info", "Set log level (trace, debug, info, warn, error, fatal)")
|
cmd.PersistentFlags().StringVarP(&logLevelFlag, logLevelFlagName, "", "info", "Set log level (trace, debug, info, warn, error, fatal)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func addLogOutputFlag(cmd *cobra.Command) {
|
func addLogOutputFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().StringVarP(&logOutputFlag, "log-outWriter", "", "stderr", "Set log outWriter (stdout, stderr, file)")
|
cmd.PersistentFlags().StringVarP(&logOutputFlag, logOutputFlagName, "", "stderr", "Set log output (stdout, stderr, file)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func addLogTypeFlag(cmd *cobra.Command) {
|
func addLogTypeFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().StringVarP(&logTypeFlag, "log-type", "", "text", "Set log type (text, json, color)")
|
cmd.PersistentFlags().StringVarP(&logTypeFlag, logTypeFlagName, "", "text", "Set log type (text, json, color)")
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureLoggingFlags(cmd *cobra.Command) {
|
func configureLoggingFlags(cmd *cobra.Command) {
|
||||||
@ -33,3 +41,12 @@ func configureLoggingFlags(cmd *cobra.Command) {
|
|||||||
|
|
||||||
cmd.MarkFlagsMutuallyExclusive("no-color", "log-type")
|
cmd.MarkFlagsMutuallyExclusive("no-color", "log-type")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetLogLevelFromArgs(args []string) slog.Level {
|
||||||
|
for i, arg := range args {
|
||||||
|
if arg == "--log-level" && i+1 < len(args) {
|
||||||
|
return slogd.Level(args[i+1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return slogd.LevelDefault
|
||||||
|
}
|
||||||
|
@ -16,7 +16,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version semver.Version
|
version Version
|
||||||
versionFlag bool
|
versionFlag bool
|
||||||
versionCmd = &cobra.Command{
|
versionCmd = &cobra.Command{
|
||||||
Use: versionName,
|
Use: versionName,
|
||||||
@ -25,11 +25,24 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Version struct {
|
||||||
|
Full string
|
||||||
|
Branch string
|
||||||
|
Tag string
|
||||||
|
Commit string
|
||||||
|
CommitDate string
|
||||||
|
BuildDate string
|
||||||
|
Major string
|
||||||
|
Minor string
|
||||||
|
Patch string
|
||||||
|
PreRelease string
|
||||||
|
}
|
||||||
|
|
||||||
func addVersionFlag(cmd *cobra.Command) {
|
func addVersionFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().BoolVarP(&versionFlag, versionName, versionShortHand, false, versionUsage)
|
cmd.PersistentFlags().BoolVarP(&versionFlag, versionName, versionShortHand, false, versionUsage)
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureVersionFlag(cmd *cobra.Command, v semver.Version) {
|
func configureVersionFlag(cmd *cobra.Command, v Version) {
|
||||||
version = v
|
version = v
|
||||||
cmd.AddCommand(versionCmd)
|
cmd.AddCommand(versionCmd)
|
||||||
addVersionFlag(cmd)
|
addVersionFlag(cmd)
|
||||||
@ -50,19 +63,28 @@ func printVersion(v semver.Version) string {
|
|||||||
if output != "" {
|
if output != "" {
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"Full: %s\nVersion: %s\nChannel: %s\nCommit: %s\nDate: %s",
|
"Full: %s\nBranch: %s\nTag: %s\nCommit: %s\nCommit date: %s\nBuild date: %s\nMajor: %s\nMinor: %s\nPatch: %s\nPreRelease: %s\n",
|
||||||
v.String(),
|
version.Full,
|
||||||
v.Number(),
|
version.Branch,
|
||||||
v.Release(),
|
version.Tag,
|
||||||
v.Commit(),
|
version.Commit,
|
||||||
v.Date(),
|
version.CommitDate,
|
||||||
|
version.BuildDate,
|
||||||
|
version.Major,
|
||||||
|
version.Minor,
|
||||||
|
version.Patch,
|
||||||
|
version.PreRelease,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func versionRunFuncE(cmd *cobra.Command, args []string) error {
|
func versionRunFuncE(cmd *cobra.Command, args []string) error {
|
||||||
if _, err := fmt.Fprintln(outWriter, printVersion(version)); err != nil {
|
var v semver.Version
|
||||||
|
var err error
|
||||||
|
if v, err = semver.Parse(version.Full); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err = fmt.Fprintln(outWriter, printVersion(v)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -2,47 +2,82 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
|
||||||
|
|
||||||
"git.flexabyte.io/flexabyte/go-kit/application"
|
"git.flexabyte.io/flexabyte/go-kit/application"
|
||||||
|
"git.flexabyte.io/flexabyte/go-kit/httpd"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
version string = "0.1.0-alpha.0+metadata.20101112"
|
||||||
|
branch string = "0.1.0-dev"
|
||||||
|
tag string = "0.1.0-dev.0"
|
||||||
|
commit string = "aabbccddee"
|
||||||
|
commitDate string = time.Now().String()
|
||||||
|
buildDate string = time.Now().String()
|
||||||
|
|
||||||
|
major string = "0"
|
||||||
|
minor string = "1"
|
||||||
|
patch string = "0"
|
||||||
|
prerelease string = "dev"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
slogd.Init(slogd.LevelTrace, true)
|
slogd.Init(application.GetLogLevelFromArgs(os.Args), false)
|
||||||
|
slogd.RegisterSink(slogd.HandlerText, slog.NewTextHandler(os.Stdout, slogd.HandlerOptions()), true)
|
||||||
|
ctx := slogd.WithContext(context.Background())
|
||||||
|
|
||||||
config := application.Config{
|
config := application.Config{
|
||||||
Name: "main",
|
Name: "main",
|
||||||
Title: "Main Test",
|
Title: "Main Test",
|
||||||
Banner: "",
|
Banner: "",
|
||||||
Version: "0.1.0-alpha.0+metadata.20101112",
|
// Version: "0.1.0-alpha.0+metadata.20101112",
|
||||||
EnableGracefulShutdown: true,
|
Version: application.Version{
|
||||||
OverrideRunE: func(cmd *cobra.Command, args []string) error {
|
Full: version,
|
||||||
fmt.Println("overrideRunE")
|
Branch: branch,
|
||||||
time.Sleep(5 * time.Second)
|
Tag: tag,
|
||||||
fmt.Println("overrideRunE done")
|
Commit: commit,
|
||||||
return nil
|
CommitDate: commitDate,
|
||||||
|
BuildDate: buildDate,
|
||||||
|
Major: major,
|
||||||
|
Minor: minor,
|
||||||
|
Patch: patch,
|
||||||
|
PreRelease: prerelease,
|
||||||
},
|
},
|
||||||
PersistentPreRunE: nil,
|
EnableGracefulShutdown: true,
|
||||||
PersistentPostRunE: nil,
|
Logger: slogd.Logger(),
|
||||||
// ShutdownSignals: []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT},
|
OverrideRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
ShutdownTimeout: 1 * time.Second,
|
// fmt.Println("overrideRunE")
|
||||||
|
// time.Sleep(1 * time.Second)
|
||||||
|
// fmt.Println("overrideRunE done")
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slogd.FromContext(r.Context()).LogAttrs(r.Context(), slogd.LevelInfo, "request received", slog.String("method", r.Method), slog.String("url", r.URL.String()), slog.String("user-agent", r.UserAgent()))
|
||||||
|
})
|
||||||
|
return httpd.RunHttpServer(cmd.Context(), slogd.FromContext(cmd.Context()), "127.0.0.1", 28000, mux, 1*time.Second)
|
||||||
|
},
|
||||||
|
PersistentPreRunE: nil,
|
||||||
|
PersistentPostRunE: nil,
|
||||||
|
ShutdownSignals: []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT},
|
||||||
|
ShutdownTimeout: 5 * time.Second,
|
||||||
SubCommands: nil,
|
SubCommands: nil,
|
||||||
SubCommandInitializeFunc: nil,
|
SubCommandInitializeFunc: nil,
|
||||||
ValidArgs: nil,
|
ValidArgs: nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
var app application.Application
|
var app application.Application
|
||||||
if app, err = application.New(config); err != nil {
|
if app, err = application.New(config); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = app.Start(context.Background()); err != nil {
|
if err = app.ExecuteContext(ctx); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module git.flexabyte.io/flexabyte/go-kit
|
|||||||
go 1.24.2
|
go 1.24.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250422113213-08cde7c3729c
|
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250428200220-8e65f81d9450
|
||||||
github.com/spf13/cobra v1.9.1
|
github.com/spf13/cobra v1.9.1
|
||||||
github.com/spf13/pflag v1.0.6
|
github.com/spf13/pflag v1.0.6
|
||||||
)
|
)
|
||||||
|
2
go.sum
2
go.sum
@ -2,6 +2,8 @@ git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250416190113-64c1cac4d274 h1:aR9nV8
|
|||||||
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250416190113-64c1cac4d274/go.mod h1:rL08OHw4aycfjkZOS8pBfLapeG3IZHxIInW29hVVSrI=
|
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250416190113-64c1cac4d274/go.mod h1:rL08OHw4aycfjkZOS8pBfLapeG3IZHxIInW29hVVSrI=
|
||||||
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250422113213-08cde7c3729c h1:8ZVNJJ/LNbu4aWozJ6wj0ZOZFeuqRh1xSyQRh+Cq1yM=
|
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250422113213-08cde7c3729c h1:8ZVNJJ/LNbu4aWozJ6wj0ZOZFeuqRh1xSyQRh+Cq1yM=
|
||||||
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250422113213-08cde7c3729c/go.mod h1:rL08OHw4aycfjkZOS8pBfLapeG3IZHxIInW29hVVSrI=
|
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250422113213-08cde7c3729c/go.mod h1:rL08OHw4aycfjkZOS8pBfLapeG3IZHxIInW29hVVSrI=
|
||||||
|
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250428200220-8e65f81d9450 h1:VCstITW9pMgR5EnSLU83UiK7llLOguFLAo26VOOnzrI=
|
||||||
|
git.flexabyte.io/flexabyte/go-slogd v0.0.0-20250428200220-8e65f81d9450/go.mod h1:rL08OHw4aycfjkZOS8pBfLapeG3IZHxIInW29hVVSrI=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
62
httpd/httpd.go
Normal file
62
httpd/httpd.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package httpd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RunHttpServer(ctx context.Context, log *slog.Logger, listenAddress string, port int, h http.Handler, shutdownTimeout time.Duration) error {
|
||||||
|
s := &http.Server{
|
||||||
|
Addr: listenAddress + ":" + strconv.Itoa(port),
|
||||||
|
Handler: h}
|
||||||
|
|
||||||
|
return run(ctx, s, log, shutdownTimeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunSocketHttpServer(ctx context.Context, log *slog.Logger, socketPath string, h http.Handler, shutdownTimeout time.Duration) error {
|
||||||
|
s := &http.Server{
|
||||||
|
Handler: h}
|
||||||
|
|
||||||
|
return run(ctx, s, log, shutdownTimeout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func run(ctx context.Context, s *http.Server, log *slog.Logger, shutdownTimeout time.Duration) error {
|
||||||
|
log.LogAttrs(ctx, slogd.LevelTrace, "starting http server", slog.String("listenAddress", s.Addr))
|
||||||
|
idleConnectionsClosed := make(chan struct{})
|
||||||
|
|
||||||
|
runCtx, runCancel := context.WithCancel(ctx)
|
||||||
|
defer runCancel()
|
||||||
|
|
||||||
|
go func(ctx context.Context) {
|
||||||
|
log.LogAttrs(ctx, slogd.LevelTrace, "awaiting shutdown signal for http server", slog.String("listenAddress", s.Addr))
|
||||||
|
<-ctx.Done()
|
||||||
|
|
||||||
|
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||||||
|
defer shutdownCancel()
|
||||||
|
|
||||||
|
log.LogAttrs(shutdownCtx, slogd.LevelTrace, "shutting down http server", slog.String("listenAddress", s.Addr))
|
||||||
|
// We received an interrupt signal, shut down.
|
||||||
|
if err := s.Shutdown(shutdownCtx); err != nil {
|
||||||
|
// Error from closing listeners, or context timeout:
|
||||||
|
log.LogAttrs(ctx, slogd.LevelTrace, "shutting down http server failed", slog.String("listenAddress", s.Addr), slog.Any("error", err))
|
||||||
|
}
|
||||||
|
log.LogAttrs(shutdownCtx, slogd.LevelTrace, "shutting down http server completed", slog.String("listenAddress", s.Addr))
|
||||||
|
close(idleConnectionsClosed)
|
||||||
|
}(runCtx)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
if err = s.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
// Error starting or closing listener:
|
||||||
|
log.LogAttrs(ctx, slogd.LevelError, "http server start failed", slog.String("error", err.Error()))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
<-idleConnectionsClosed
|
||||||
|
return err
|
||||||
|
}
|
Reference in New Issue
Block a user