Skip to content

Commit

Permalink
refactor: readd server timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
divyam234 committed Jul 9, 2024
1 parent 5ff157a commit e7865a7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
21 changes: 16 additions & 5 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func NewRun() *cobra.Command {
runCmd.Flags().IntVarP(&config.Server.Port, "server-port", "p", 8080, "Server port")
duration.DurationVar(runCmd.Flags(), &config.Server.GracefulShutdown, "server-graceful-shutdown", 15*time.Second, "Server graceful shutdown timeout")
runCmd.Flags().BoolVar(&config.Server.EnablePprof, "server-enable-pprof", false, "Enable Pprof Profiling")
duration.DurationVar(runCmd.Flags(), &config.Server.ReadTimeout, "server-read-timeout", 1*time.Hour, "Server read timeout")
duration.DurationVar(runCmd.Flags(), &config.Server.WriteTimeout, "server-write-timeout", 1*time.Hour, "Server write timeout")

runCmd.Flags().BoolVar(&config.CronJobs.Enable, "cronjobs-enable", true, "Run cron jobs")

Expand Down Expand Up @@ -233,10 +235,15 @@ func initApp(lc fx.Lifecycle, cfg *config.Config, c *controller.Controller) *gin

r.Use(gin.Recovery())

skipPathRegexps := []*regexp.Regexp{
regexp.MustCompile(`^/assets/.*`),
regexp.MustCompile(`^/images/.*`),
}

r.Use(ginzap.GinzapWithConfig(logging.DefaultLogger().Desugar(), &ginzap.Config{
TimeFormat: time.RFC3339,
UTC: true,
SkipPaths: []string{"/favicon.ico", "/assets"},
TimeFormat: time.RFC3339,
UTC: true,
SkipPathRegexps: skipPathRegexps,
}))

r.Use(middleware.Cors())
Expand All @@ -253,8 +260,12 @@ func initApp(lc fx.Lifecycle, cfg *config.Config, c *controller.Controller) *gin

r = api.InitRouter(r, c, cfg)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Server.Port),
Handler: r,
Addr: fmt.Sprintf(":%d", cfg.Server.Port),
Handler: r,
ReadTimeout: cfg.Server.ReadTimeout,
WriteTimeout: cfg.Server.WriteTimeout,
ReadHeaderTimeout: 10 * time.Second,
IdleTimeout: 60 * time.Second,
}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
Expand Down
4 changes: 3 additions & 1 deletion config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
[server]
graceful-shutdown = "15s"
port = 8080
read-timeout = "1h"
write-timeout = "1h"

[tg]
app-hash = ""
Expand All @@ -47,5 +49,5 @@
threads = 8
[tg.stream]
multi-threads = 0
buffers = 16
buffers = 8

2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type ServerConfig struct {
Port int
GracefulShutdown time.Duration
EnablePprof bool
ReadTimeout time.Duration
WriteTimeout time.Duration
}

type CronJobConfig struct {
Expand Down

0 comments on commit e7865a7

Please sign in to comment.