feat: add request timeout #12

Open
bissendorf wants to merge 1 commits from feat/timeout into dev
3 changed files with 11 additions and 4 deletions
Showing only changes of commit 2abebfd01b - Show all commits

View File

@ -1,8 +1,11 @@
package cmd package cmd
const DefaultRequestTimeout uint = 30
type AppConfig struct { type AppConfig struct {
OutputVerbose bool OutputVerbose bool
OutputFormatter string OutputFormatter string
OutputOrderReverse bool OutputOrderReverse bool
PrintConfig bool PrintConfig bool
RequestTimeoutSeconds uint
} }

View File

@ -34,6 +34,7 @@ func initRootCmd() {
rootCmd.PersistentFlags().StringVarP(&appConfig.OutputFormatter, "output", "o", "table", "Set output format") rootCmd.PersistentFlags().StringVarP(&appConfig.OutputFormatter, "output", "o", "table", "Set output format")
rootCmd.PersistentFlags().BoolVar(&appConfig.OutputOrderReverse, "reverse", false, "Reverses output item order") rootCmd.PersistentFlags().BoolVar(&appConfig.OutputOrderReverse, "reverse", false, "Reverses output item order")
rootCmd.PersistentFlags().BoolVar(&appConfig.PrintConfig, "print-config", false, "Enable printing the application config") rootCmd.PersistentFlags().BoolVar(&appConfig.PrintConfig, "print-config", false, "Enable printing the application config")
rootCmd.PersistentFlags().UintVar(&appConfig.RequestTimeoutSeconds, "timeout", DefaultRequestTimeout, "Set the request timeout in seconds")
logger := jlog.New(slog.LevelDebug) logger := jlog.New(slog.LevelDebug)
ctx := jlog.ContextWith(context.Background(), logger) ctx := jlog.ContextWith(context.Background(), logger)

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"slices" "slices"
"strings" "strings"
"time"
"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces" "git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces"
"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces/params" "git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces/params"
@ -92,6 +93,8 @@ func getVerbs(ctx context.Context, config *AppConfig) (commands []*cobra.Command
} }
logger := jlog.New(logLevel) logger := jlog.New(logLevel)
ctx := jlog.ContextWith(context.Background(), logger) ctx := jlog.ContextWith(context.Background(), logger)
ctx, cancel := context.WithTimeout(ctx, time.Duration(config.RequestTimeoutSeconds)*time.Second)
defer cancel()
// Print config // Print config
if config.PrintConfig { if config.PrintConfig {