// Code generated by go-swagger; DO NOT EDIT.

// Copyright Authors of Cilium
// SPDX-License-Identifier: Apache-2.0

package server

import (
	"context"
	"crypto/tls"
	"crypto/x509"
	"errors"
	"fmt"
	"log/slog"
	"net"
	"net/http"
	"os"
	"slices"
	"strconv"
	"sync"
	"time"

	"github.com/cilium/hive/cell"
	"github.com/go-openapi/loads"
	"github.com/go-openapi/runtime/middleware"
	"github.com/go-openapi/swag"
	"github.com/spf13/pflag"
	"golang.org/x/net/netutil"

	"github.com/cilium/cilium/api/v1/server/restapi"
	"github.com/cilium/cilium/api/v1/server/restapi/bgp"
	"github.com/cilium/cilium/api/v1/server/restapi/daemon"
	"github.com/cilium/cilium/api/v1/server/restapi/endpoint"
	"github.com/cilium/cilium/api/v1/server/restapi/ipam"
	"github.com/cilium/cilium/api/v1/server/restapi/policy"
	"github.com/cilium/cilium/api/v1/server/restapi/prefilter"
	"github.com/cilium/cilium/api/v1/server/restapi/service"
	"github.com/cilium/cilium/pkg/api"
	"github.com/cilium/cilium/pkg/hive"
	"github.com/cilium/cilium/pkg/logging"
)

// Cell implements the cilium API REST API server when provided
// the required request handlers.
var Cell = cell.Module(
	"cilium-api-server",
	"cilium API server",

	cell.Provide(newForCell),
	APICell,
)

// APICell provides the restapi.CiliumAPIAPI type, populated
// with the request handlers. This cell is an alternative to 'Cell' when only
// the API type is required and not the full server implementation.
var APICell = cell.Provide(newAPI)

type apiParams struct {
	cell.In

	Spec *Spec

	Logger *slog.Logger

	Middleware middleware.Builder `name:"cilium-api-middleware" optional:"true"`

	EndpointDeleteEndpointHandler        endpoint.DeleteEndpointHandler
	EndpointDeleteEndpointIDHandler      endpoint.DeleteEndpointIDHandler
	PolicyDeleteFqdnCacheHandler         policy.DeleteFqdnCacheHandler
	IpamDeleteIpamIPHandler              ipam.DeleteIpamIPHandler
	PolicyDeletePolicyHandler            policy.DeletePolicyHandler
	PrefilterDeletePrefilterHandler      prefilter.DeletePrefilterHandler
	BgpGetBgpPeersHandler                bgp.GetBgpPeersHandler
	BgpGetBgpRoutePoliciesHandler        bgp.GetBgpRoutePoliciesHandler
	BgpGetBgpRoutesHandler               bgp.GetBgpRoutesHandler
	DaemonGetCgroupDumpMetadataHandler   daemon.GetCgroupDumpMetadataHandler
	DaemonGetClusterNodesHandler         daemon.GetClusterNodesHandler
	DaemonGetConfigHandler               daemon.GetConfigHandler
	DaemonGetDebuginfoHandler            daemon.GetDebuginfoHandler
	EndpointGetEndpointHandler           endpoint.GetEndpointHandler
	EndpointGetEndpointIDHandler         endpoint.GetEndpointIDHandler
	EndpointGetEndpointIDConfigHandler   endpoint.GetEndpointIDConfigHandler
	EndpointGetEndpointIDHealthzHandler  endpoint.GetEndpointIDHealthzHandler
	EndpointGetEndpointIDLabelsHandler   endpoint.GetEndpointIDLabelsHandler
	EndpointGetEndpointIDLogHandler      endpoint.GetEndpointIDLogHandler
	PolicyGetFqdnCacheHandler            policy.GetFqdnCacheHandler
	PolicyGetFqdnCacheIDHandler          policy.GetFqdnCacheIDHandler
	PolicyGetFqdnNamesHandler            policy.GetFqdnNamesHandler
	DaemonGetHealthzHandler              daemon.GetHealthzHandler
	PolicyGetIPHandler                   policy.GetIPHandler
	PolicyGetIdentityHandler             policy.GetIdentityHandler
	PolicyGetIdentityEndpointsHandler    policy.GetIdentityEndpointsHandler
	PolicyGetIdentityIDHandler           policy.GetIdentityIDHandler
	ServiceGetLrpHandler                 service.GetLrpHandler
	DaemonGetMapHandler                  daemon.GetMapHandler
	DaemonGetMapNameHandler              daemon.GetMapNameHandler
	DaemonGetMapNameEventsHandler        daemon.GetMapNameEventsHandler
	DaemonGetNodeIdsHandler              daemon.GetNodeIdsHandler
	PolicyGetPolicyHandler               policy.GetPolicyHandler
	PolicyGetPolicySelectorsHandler      policy.GetPolicySelectorsHandler
	PrefilterGetPrefilterHandler         prefilter.GetPrefilterHandler
	ServiceGetServiceHandler             service.GetServiceHandler
	DaemonPatchConfigHandler             daemon.PatchConfigHandler
	EndpointPatchEndpointIDHandler       endpoint.PatchEndpointIDHandler
	EndpointPatchEndpointIDConfigHandler endpoint.PatchEndpointIDConfigHandler
	EndpointPatchEndpointIDLabelsHandler endpoint.PatchEndpointIDLabelsHandler
	PrefilterPatchPrefilterHandler       prefilter.PatchPrefilterHandler
	IpamPostIpamHandler                  ipam.PostIpamHandler
	IpamPostIpamIPHandler                ipam.PostIpamIPHandler
	EndpointPutEndpointIDHandler         endpoint.PutEndpointIDHandler
	PolicyPutPolicyHandler               policy.PutPolicyHandler
}

func newAPI(p apiParams) *restapi.CiliumAPIAPI {
	api := restapi.NewCiliumAPIAPI(p.Spec.Document)

	// Construct the API from the provided handlers

	api.EndpointDeleteEndpointHandler = p.EndpointDeleteEndpointHandler
	api.EndpointDeleteEndpointIDHandler = p.EndpointDeleteEndpointIDHandler
	api.PolicyDeleteFqdnCacheHandler = p.PolicyDeleteFqdnCacheHandler
	api.IpamDeleteIpamIPHandler = p.IpamDeleteIpamIPHandler
	api.PolicyDeletePolicyHandler = p.PolicyDeletePolicyHandler
	api.PrefilterDeletePrefilterHandler = p.PrefilterDeletePrefilterHandler
	api.BgpGetBgpPeersHandler = p.BgpGetBgpPeersHandler
	api.BgpGetBgpRoutePoliciesHandler = p.BgpGetBgpRoutePoliciesHandler
	api.BgpGetBgpRoutesHandler = p.BgpGetBgpRoutesHandler
	api.DaemonGetCgroupDumpMetadataHandler = p.DaemonGetCgroupDumpMetadataHandler
	api.DaemonGetClusterNodesHandler = p.DaemonGetClusterNodesHandler
	api.DaemonGetConfigHandler = p.DaemonGetConfigHandler
	api.DaemonGetDebuginfoHandler = p.DaemonGetDebuginfoHandler
	api.EndpointGetEndpointHandler = p.EndpointGetEndpointHandler
	api.EndpointGetEndpointIDHandler = p.EndpointGetEndpointIDHandler
	api.EndpointGetEndpointIDConfigHandler = p.EndpointGetEndpointIDConfigHandler
	api.EndpointGetEndpointIDHealthzHandler = p.EndpointGetEndpointIDHealthzHandler
	api.EndpointGetEndpointIDLabelsHandler = p.EndpointGetEndpointIDLabelsHandler
	api.EndpointGetEndpointIDLogHandler = p.EndpointGetEndpointIDLogHandler
	api.PolicyGetFqdnCacheHandler = p.PolicyGetFqdnCacheHandler
	api.PolicyGetFqdnCacheIDHandler = p.PolicyGetFqdnCacheIDHandler
	api.PolicyGetFqdnNamesHandler = p.PolicyGetFqdnNamesHandler
	api.DaemonGetHealthzHandler = p.DaemonGetHealthzHandler
	api.PolicyGetIPHandler = p.PolicyGetIPHandler
	api.PolicyGetIdentityHandler = p.PolicyGetIdentityHandler
	api.PolicyGetIdentityEndpointsHandler = p.PolicyGetIdentityEndpointsHandler
	api.PolicyGetIdentityIDHandler = p.PolicyGetIdentityIDHandler
	api.ServiceGetLrpHandler = p.ServiceGetLrpHandler
	api.DaemonGetMapHandler = p.DaemonGetMapHandler
	api.DaemonGetMapNameHandler = p.DaemonGetMapNameHandler
	api.DaemonGetMapNameEventsHandler = p.DaemonGetMapNameEventsHandler
	api.DaemonGetNodeIdsHandler = p.DaemonGetNodeIdsHandler
	api.PolicyGetPolicyHandler = p.PolicyGetPolicyHandler
	api.PolicyGetPolicySelectorsHandler = p.PolicyGetPolicySelectorsHandler
	api.PrefilterGetPrefilterHandler = p.PrefilterGetPrefilterHandler
	api.ServiceGetServiceHandler = p.ServiceGetServiceHandler
	api.DaemonPatchConfigHandler = p.DaemonPatchConfigHandler
	api.EndpointPatchEndpointIDHandler = p.EndpointPatchEndpointIDHandler
	api.EndpointPatchEndpointIDConfigHandler = p.EndpointPatchEndpointIDConfigHandler
	api.EndpointPatchEndpointIDLabelsHandler = p.EndpointPatchEndpointIDLabelsHandler
	api.PrefilterPatchPrefilterHandler = p.PrefilterPatchPrefilterHandler
	api.IpamPostIpamHandler = p.IpamPostIpamHandler
	api.IpamPostIpamIPHandler = p.IpamPostIpamIPHandler
	api.EndpointPutEndpointIDHandler = p.EndpointPutEndpointIDHandler
	api.PolicyPutPolicyHandler = p.PolicyPutPolicyHandler

	// Inject custom middleware if provided by Hive
	if p.Middleware != nil {
		api.Middleware = func(builder middleware.Builder) http.Handler {
			return p.Middleware(api.Context().APIHandler(builder))
		}
	}

	api.Logger = p.Logger.Info

	return api
}

type serverParams struct {
	cell.In

	Lifecycle  cell.Lifecycle
	Shutdowner hive.Shutdowner
	Logger     *slog.Logger
	Spec       *Spec
	API        *restapi.CiliumAPIAPI
}

func newForCell(p serverParams) (*Server, error) {
	s := NewServer(p.API)
	s.shutdowner = p.Shutdowner
	s.logger = p.Logger
	p.Lifecycle.Append(s)
	return s, nil
}

const (
	schemeHTTP  = "http"
	schemeHTTPS = "https"
	schemeUnix  = "unix"
)

var defaultSchemes []string

func init() {
	defaultSchemes = []string{
		schemeUnix,
	}
}

var (
	enabledListeners []string
	gracefulTimeout  time.Duration
	maxHeaderSize    int

	socketPath string

	host         string
	port         int
	listenLimit  int
	keepAlive    time.Duration
	readTimeout  time.Duration
	writeTimeout time.Duration

	tlsHost           string
	tlsPort           int
	tlsListenLimit    int
	tlsKeepAlive      time.Duration
	tlsReadTimeout    time.Duration
	tlsWriteTimeout   time.Duration
	tlsCertificate    string
	tlsCertificateKey string
	tlsCACertificate  string
)

type ServerConfig struct {
	EnableCiliumAPIServerAccess []string
}

var (
	defaultServerConfig = ServerConfig{
		EnableCiliumAPIServerAccess: []string{"*"},
	}
	AdminEnableFlag = "enable-cilium-api-server-access"
)

func (cfg ServerConfig) Flags(flags *pflag.FlagSet) {
	flags.StringSlice(AdminEnableFlag, cfg.EnableCiliumAPIServerAccess,
		"List of cilium API APIs which are administratively enabled. Supports '*'.")
}

var SpecCell = cell.Module(
	"cilium-api-spec",
	"cilium API Specification",

	cell.Config(defaultServerConfig),
	cell.Provide(newSpec),
)

type Spec struct {
	*loads.Document

	// DeniedAPIs is a set of APIs that are administratively disabled.
	DeniedAPIs api.PathSet
}

func newSpec(cfg ServerConfig) (*Spec, error) {
	swaggerSpec, err := loads.Analyzed(SwaggerJSON, "")
	if err != nil {
		return nil, fmt.Errorf("failed to load swagger spec: %w", err)
	}

	deniedAPIs, err := api.AllowedFlagsToDeniedPaths(swaggerSpec, cfg.EnableCiliumAPIServerAccess)
	if err != nil {
		return nil, fmt.Errorf("failed to parse %q flag: %w",
			AdminEnableFlag, err)
	}

	return &Spec{
		Document:   swaggerSpec,
		DeniedAPIs: deniedAPIs,
	}, nil
}

// NewServer creates a new api cilium API server but does not configure it
func NewServer(api *restapi.CiliumAPIAPI) *Server {
	s := new(Server)
	s.api = api
	return s
}

// ConfigureAPI configures the API and handlers.
func (s *Server) ConfigureAPI() {
	if s.api != nil {
		s.handler = configureAPI(s.logger, s.api)
	}
}

// ConfigureFlags configures the additional flags defined by the handlers. Needs to be called before the parser.Parse
func (s *Server) ConfigureFlags() {
	if s.api != nil {
		configureFlags(s.api)
	}
}

// Server for the cilium API API
type Server struct {
	EnabledListeners []string
	CleanupTimeout   time.Duration
	GracefulTimeout  time.Duration
	MaxHeaderSize    int

	SocketPath    string
	domainSocketL *net.UnixListener

	Host         string
	Port         int
	ListenLimit  int
	KeepAlive    time.Duration
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	httpServerL  net.Listener

	TLSHost           string
	TLSPort           int
	TLSCertificate    string
	TLSCertificateKey string
	TLSCACertificate  string
	TLSListenLimit    int
	TLSKeepAlive      time.Duration
	TLSReadTimeout    time.Duration
	TLSWriteTimeout   time.Duration
	httpsServerL      net.Listener

	api          *restapi.CiliumAPIAPI
	handler      http.Handler
	hasListeners bool
	servers      []*http.Server

	wg         sync.WaitGroup
	shutdowner hive.Shutdowner
	logger     *slog.Logger
}

// Logf logs message either via defined user logger or via system one if no user logger is defined.
func (s *Server) Logf(f string, args ...interface{}) {
	if s.logger != nil {
		s.logger.Info(fmt.Sprintf(f, args...))
	} else if s.api != nil && s.api.Logger != nil {
		s.api.Logger(fmt.Sprintf(f, args...))
	} else {
		slog.Info(fmt.Sprintf(f, args...))
	}
}

// Debugf logs debug messages either via defined user logger or via system one if no user logger is defined.
func (s *Server) Debugf(f string, args ...interface{}) {
	if s.logger != nil {
		s.logger.Debug(fmt.Sprintf(f, args...))
	} else {
		slog.Debug(fmt.Sprintf(f, args...))
	}
}

// Fatalf logs message either via defined user logger or via system one if no user logger is defined.
// Exits with non-zero status after printing
func (s *Server) Fatalf(f string, args ...interface{}) {
	if s.shutdowner != nil {
		s.shutdowner.Shutdown(hive.ShutdownWithError(fmt.Errorf(f, args...)))
	} else if s.api != nil && s.api.Logger != nil {
		s.api.Logger(f, args...)
		os.Exit(1)
	} else {
		logging.Fatal(slog.Default(), fmt.Sprintf(f, args...))
	}
}

// SetAPI configures the server with the specified API. Needs to be called before Serve
func (s *Server) SetAPI(api *restapi.CiliumAPIAPI) {
	if api == nil {
		s.api = nil
		s.handler = nil
		return
	}

	s.api = api
	s.handler = configureAPI(s.logger, api)
}

// GetAPI returns the configured API. Modifications on the API must be performed
// before server is started.
func (s *Server) GetAPI() *restapi.CiliumAPIAPI {
	return s.api
}

func (s *Server) hasScheme(scheme string) bool {
	schemes := s.EnabledListeners
	if len(schemes) == 0 {
		schemes = defaultSchemes
	}

	return slices.Contains(schemes, scheme)
}

func (s *Server) Serve() error {
	// TODO remove when this is not needed for compatibility anymore
	if err := s.Start(context.TODO()); err != nil {
		return err
	}
	s.wg.Wait()
	return nil
}

// Start the server
func (s *Server) Start(cell.HookContext) (err error) {
	if !s.hasListeners {
		if err = s.Listen(); err != nil {
			return err
		}
	}

	if len(s.servers) != 0 {
		return errors.New("already started")
	}

	// set default handler, if none is set
	if s.handler == nil {
		if s.api == nil {
			return errors.New("can't create the default handler, as no api is set")
		}

		s.ConfigureAPI()
		s.SetHandler(s.api.Serve(nil))
	}

	if s.hasScheme(schemeUnix) {
		domainSocket := new(http.Server)
		domainSocket.MaxHeaderBytes = s.MaxHeaderSize
		domainSocket.Handler = s.handler
		if int64(s.CleanupTimeout) > 0 {
			domainSocket.IdleTimeout = s.CleanupTimeout
		}

		configureServer(domainSocket, "unix", s.SocketPath)

		if os.Getuid() == 0 {
			err := api.SetDefaultPermissions(s.Debugf, s.SocketPath)
			if err != nil {
				return err
			}
		}
		s.servers = append(s.servers, domainSocket)
		s.wg.Add(1)
		s.Logf("Serving cilium API at unix://%s", s.SocketPath)
		go func(l net.Listener) {
			defer s.wg.Done()
			if err := domainSocket.Serve(l); err != nil && err != http.ErrServerClosed {
				s.Fatalf("%v", err)
			}
			s.Logf("Stopped serving cilium API at unix://%s", s.SocketPath)
		}(s.domainSocketL)
	}

	if s.hasScheme(schemeHTTP) {
		httpServer := new(http.Server)
		httpServer.MaxHeaderBytes = s.MaxHeaderSize
		httpServer.ReadTimeout = s.ReadTimeout
		httpServer.WriteTimeout = s.WriteTimeout
		httpServer.SetKeepAlivesEnabled(int64(s.KeepAlive) > 0)
		if s.ListenLimit > 0 {
			s.httpServerL = netutil.LimitListener(s.httpServerL, s.ListenLimit)
		}

		if int64(s.CleanupTimeout) > 0 {
			httpServer.IdleTimeout = s.CleanupTimeout
		}

		httpServer.Handler = s.handler

		configureServer(httpServer, "http", s.httpServerL.Addr().String())

		s.servers = append(s.servers, httpServer)
		s.wg.Add(1)
		s.Logf("Serving cilium API at http://%s", s.httpServerL.Addr())
		go func(l net.Listener) {
			defer s.wg.Done()
			if err := httpServer.Serve(l); err != nil && err != http.ErrServerClosed {
				s.Fatalf("%v", err)
			}
			s.Logf("Stopped serving cilium API at http://%s", l.Addr())
		}(s.httpServerL)
	}

	if s.hasScheme(schemeHTTPS) {
		httpsServer := new(http.Server)
		httpsServer.MaxHeaderBytes = s.MaxHeaderSize
		httpsServer.ReadTimeout = s.TLSReadTimeout
		httpsServer.WriteTimeout = s.TLSWriteTimeout
		httpsServer.SetKeepAlivesEnabled(int64(s.TLSKeepAlive) > 0)
		if s.TLSListenLimit > 0 {
			s.httpsServerL = netutil.LimitListener(s.httpsServerL, s.TLSListenLimit)
		}
		if int64(s.CleanupTimeout) > 0 {
			httpsServer.IdleTimeout = s.CleanupTimeout
		}
		httpsServer.Handler = s.handler

		// Inspired by https://blog.bracebin.com/achieving-perfect-ssl-labs-score-with-go
		httpsServer.TLSConfig = &tls.Config{
			// Causes servers to use Go's default ciphersuite preferences,
			// which are tuned to avoid attacks. Does nothing on clients.
			PreferServerCipherSuites: true,
			// Only use curves which have assembly implementations
			// https://github.com/golang/go/tree/master/src/crypto/elliptic
			CurvePreferences: []tls.CurveID{tls.CurveP256},
			// Use modern tls mode https://wiki.mozilla.org/Security/Server_Side_TLS#Modern_compatibility
			NextProtos: []string{"h2", "http/1.1"},
			// https://www.owasp.org/index.php/Transport_Layer_Protection_Cheat_Sheet#Rule_-_Only_Support_Strong_Protocols
			MinVersion: tls.VersionTLS12,
			// These ciphersuites support Forward Secrecy: https://en.wikipedia.org/wiki/Forward_secrecy
			CipherSuites: []uint16{
				tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
				tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
				tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
				tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
				tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
				tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
			},
		}

		// build standard config from server options
		if s.TLSCertificate != "" && s.TLSCertificateKey != "" {
			httpsServer.TLSConfig.Certificates = make([]tls.Certificate, 1)
			httpsServer.TLSConfig.Certificates[0], err = tls.LoadX509KeyPair(s.TLSCertificate, s.TLSCertificateKey)
			if err != nil {
				return err
			}
		}

		if s.TLSCACertificate != "" {
			// include specified CA certificate
			caCert, caCertErr := os.ReadFile(s.TLSCACertificate)
			if caCertErr != nil {
				return caCertErr
			}
			caCertPool := x509.NewCertPool()
			ok := caCertPool.AppendCertsFromPEM(caCert)
			if !ok {
				return fmt.Errorf("cannot parse CA certificate")
			}
			httpsServer.TLSConfig.ClientCAs = caCertPool
			httpsServer.TLSConfig.ClientAuth = tls.RequireAndVerifyClientCert
		}

		// call custom TLS configurator
		configureTLS(httpsServer.TLSConfig)

		if len(httpsServer.TLSConfig.Certificates) == 0 && httpsServer.TLSConfig.GetCertificate == nil {
			// after standard and custom config are passed, this ends up with no certificate
			if s.TLSCertificate == "" {
				if s.TLSCertificateKey == "" {
					s.Fatalf("the required flags `--tls-certificate` and `--tls-key` were not specified")
				}
				s.Fatalf("the required flag `--tls-certificate` was not specified")
			}
			if s.TLSCertificateKey == "" {
				s.Fatalf("the required flag `--tls-key` was not specified")
			}
			// this happens with a wrong custom TLS configurator
			s.Fatalf("no certificate was configured for TLS")
		}

		configureServer(httpsServer, "https", s.httpsServerL.Addr().String())

		s.servers = append(s.servers, httpsServer)
		s.wg.Add(1)
		s.Logf("Serving cilium API at https://%s", s.httpsServerL.Addr())
		go func(l net.Listener) {
			defer s.wg.Done()
			if err := httpsServer.Serve(l); err != nil && err != http.ErrServerClosed {
				s.Fatalf("%v", err)
			}
			s.Logf("Stopped serving cilium API at https://%s", l.Addr())
		}(tls.NewListener(s.httpsServerL, httpsServer.TLSConfig))
	}

	return nil
}

// Listen creates the listeners for the server
func (s *Server) Listen() error {
	if s.hasListeners { // already done this
		return nil
	}

	if s.hasScheme(schemeHTTPS) {
		// Use http host if https host wasn't defined
		if s.TLSHost == "" {
			s.TLSHost = s.Host
		}
		// Use http listen limit if https listen limit wasn't defined
		if s.TLSListenLimit == 0 {
			s.TLSListenLimit = s.ListenLimit
		}
		// Use http tcp keep alive if https tcp keep alive wasn't defined
		if int64(s.TLSKeepAlive) == 0 {
			s.TLSKeepAlive = s.KeepAlive
		}
		// Use http read timeout if https read timeout wasn't defined
		if int64(s.TLSReadTimeout) == 0 {
			s.TLSReadTimeout = s.ReadTimeout
		}
		// Use http write timeout if https write timeout wasn't defined
		if int64(s.TLSWriteTimeout) == 0 {
			s.TLSWriteTimeout = s.WriteTimeout
		}
	}

	if s.hasScheme(schemeUnix) {
		addr, err := net.ResolveUnixAddr("unix", s.SocketPath)
		if err != nil {
			return err
		}
		domSockListener, err := net.ListenUnix("unix", addr)
		if err != nil {
			return err
		}
		s.domainSocketL = domSockListener
	}

	if s.hasScheme(schemeHTTP) {
		listener, err := net.Listen("tcp", net.JoinHostPort(s.Host, strconv.Itoa(s.Port)))
		if err != nil {
			return err
		}

		h, p, err := swag.SplitHostPort(listener.Addr().String())
		if err != nil {
			return err
		}
		s.Host = h
		s.Port = p
		s.httpServerL = listener
	}

	if s.hasScheme(schemeHTTPS) {
		tlsListener, err := net.Listen("tcp", net.JoinHostPort(s.TLSHost, strconv.Itoa(s.TLSPort)))
		if err != nil {
			return err
		}

		sh, sp, err := swag.SplitHostPort(tlsListener.Addr().String())
		if err != nil {
			return err
		}
		s.TLSHost = sh
		s.TLSPort = sp
		s.httpsServerL = tlsListener
	}

	s.hasListeners = true
	return nil
}

// Shutdown server and clean up resources
func (s *Server) Shutdown() error {
	ctx, cancel := context.WithTimeout(context.TODO(), s.GracefulTimeout)
	defer cancel()
	return s.Stop(ctx)
}

func (s *Server) Stop(ctx cell.HookContext) error {
	// first execute the pre-shutdown hook
	s.api.PreServerShutdown()

	shutdownChan := make(chan bool)
	for i := range s.servers {
		server := s.servers[i]
		go func() {
			var success bool
			defer func() {
				shutdownChan <- success
			}()
			if err := server.Shutdown(ctx); err != nil {
				s.Logf("HTTP server Shutdown: %v", err)

				// Forcefully close open connections.
				server.Close()
			} else {
				success = true
			}
		}()
	}

	// Wait until all listeners have successfully shut down before calling ServerShutdown
	success := true
	for range s.servers {
		success = success && <-shutdownChan
	}
	if success {
		s.api.ServerShutdown()
	}

	s.wg.Wait()
	s.servers = nil

	return nil
}

// GetHandler returns a handler useful for testing
func (s *Server) GetHandler() http.Handler {
	return s.handler
}

// SetHandler allows for setting a http handler on this server
func (s *Server) SetHandler(handler http.Handler) {
	s.handler = handler
}

// UnixListener returns the domain socket listener
func (s *Server) UnixListener() (*net.UnixListener, error) {
	if !s.hasListeners {
		if err := s.Listen(); err != nil {
			return nil, err
		}
	}
	return s.domainSocketL, nil
}

// HTTPListener returns the http listener
func (s *Server) HTTPListener() (net.Listener, error) {
	if !s.hasListeners {
		if err := s.Listen(); err != nil {
			return nil, err
		}
	}
	return s.httpServerL, nil
}

// TLSListener returns the https listener
func (s *Server) TLSListener() (net.Listener, error) {
	if !s.hasListeners {
		if err := s.Listen(); err != nil {
			return nil, err
		}
	}
	return s.httpsServerL, nil
}
