AFAIRE
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kong/server/server.go

74 lines
1.5 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### kong ####### Copyright (c) 2021-2022 losyme ################################################ MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package server
import (
"context"
"net/http"
)
type Server struct {
options *Options
server *http.Server
}
func New(handler http.Handler, opts ...Option) (*Server, error) {
po := &Options{}
for _, option := range opts {
option(po)
}
tlsConfig, err := po.tlsConfig()
if err != nil {
return nil, err
}
httpServer := &http.Server{
Addr: po.Addr,
Handler: handler,
ErrorLog: po.logger,
IdleTimeout: po.IdleTimeout,
ReadTimeout: po.ReadTimeout,
WriteTimeout: po.WriteTimeout,
TLSConfig: tlsConfig,
}
server := &Server{
options: po,
server: httpServer,
}
return server, nil
}
func (s *Server) Start() error {
po := s.options
var err error
if po.tls {
err = s.server.ListenAndServeTLS(po.CertFile, po.KeyFile)
} else {
err = s.server.ListenAndServe()
}
if err == http.ErrServerClosed {
return nil
}
return err
}
func (s *Server) Stop(ctx context.Context) error {
s.server.SetKeepAlivesEnabled(false)
return s.server.Shutdown(ctx)
}
/*
######################################################################################################## @(°_°)@ #######
*/