| 55 | } |
| 56 | |
| 57 | func NewHttpServer( |
| 58 | ctx context.Context, |
| 59 | logger *zerolog.Logger, |
| 60 | cfg *common.ServerConfig, |
| 61 | healthCheckCfg *common.HealthCheckConfig, |
| 62 | adminCfg *common.AdminConfig, |
| 63 | erpc *ERPC, |
| 64 | ) (*HttpServer, error) { |
| 65 | reqMaxTimeout := 150 * time.Second |
| 66 | if cfg.MaxTimeout != nil { |
| 67 | reqMaxTimeout = cfg.MaxTimeout.Duration() |
| 68 | } |
| 69 | readTimeout := 30 * time.Second |
| 70 | if cfg.ReadTimeout != nil { |
| 71 | readTimeout = cfg.ReadTimeout.Duration() |
| 72 | } |
| 73 | writeTimeout := 120 * time.Second |
| 74 | if cfg.WriteTimeout != nil { |
| 75 | writeTimeout = cfg.WriteTimeout.Duration() |
| 76 | } |
| 77 | |
| 78 | draining := atomic.Bool{} |
| 79 | go func() { |
| 80 | <-ctx.Done() |
| 81 | draining.Store(true) |
| 82 | logger.Info().Msg("entering draining mode → healthcheck will fail") |
| 83 | }() |
| 84 | |
| 85 | gzipPool := util.NewGzipReaderPool() |
| 86 | |
| 87 | srv := &HttpServer{ |
| 88 | logger: logger, |
| 89 | appCtx: ctx, |
| 90 | serverCfg: cfg, |
| 91 | healthCheckCfg: healthCheckCfg, |
| 92 | adminCfg: adminCfg, |
| 93 | erpc: erpc, |
| 94 | draining: &draining, |
| 95 | gzipPool: gzipPool, |
| 96 | } |
| 97 | |
| 98 | if cfg != nil { |
| 99 | srv.trustedForwarderIPs = make(map[string]struct{}, len(cfg.TrustedIPForwarders)) |
| 100 | var forwarders []string |
| 101 | forwarders = append(forwarders, cfg.TrustedIPForwarders...) |
| 102 | for _, entry := range forwarders { |
| 103 | val := strings.TrimSpace(entry) |
| 104 | if val == "" { |
| 105 | continue |
| 106 | } |
| 107 | if strings.Contains(val, "/") { |
| 108 | if _, ipnet, err := net.ParseCIDR(val); err == nil && ipnet != nil { |
| 109 | srv.trustedForwarderNets = append(srv.trustedForwarderNets, *ipnet) |
| 110 | } else { |
| 111 | logger.Warn().Str("trustedForwarder", val).Msg("invalid CIDR in trusted forwarders; ignoring") |
| 112 | } |
| 113 | } else { |
| 114 | if ip := net.ParseIP(val); ip != nil { |