// NewServer creates a new api mcs server but does not configure it
funcNewServer(api*operations.McsAPI)*Server{
s:=new(Server)
s.shutdown=make(chanstruct{})
s.api=api
s.interrupt=make(chanos.Signal,1)
returns
}
// ConfigureAPI configures the API and handlers.
func(s*Server)ConfigureAPI(){
ifs.api!=nil{
s.handler=configureAPI(s.api)
}
}
// ConfigureFlags configures the additional flags defined by the handlers. Needs to be called before the parser.Parse
func(s*Server)ConfigureFlags(){
ifs.api!=nil{
configureFlags(s.api)
}
}
// Server for the mcs API
typeServerstruct{
EnabledListeners[]string`long:"scheme" description:"the listeners to enable, this can be repeated and defaults to the schemes in the swagger spec"`
CleanupTimeouttime.Duration`long:"cleanup-timeout" description:"grace period for which to wait before killing idle connections" default:"10s"`
GracefulTimeouttime.Duration`long:"graceful-timeout" description:"grace period for which to wait before shutting down the server" default:"15s"`
MaxHeaderSizeflagext.ByteSize`long:"max-header-size" description:"controls the maximum number of bytes the server will read parsing the request header's keys and values, including the request line. It does not limit the size of the request body." default:"1MiB"`
SocketPathflags.Filename`long:"socket-path" description:"the unix socket to listen on" default:"/var/run/mcs.sock"`
domainSocketLnet.Listener
Hoststring`long:"host" description:"the IP to listen on" default:"localhost" env:"HOST"`
Portint`long:"port" description:"the port to listen on for insecure connections, defaults to a random value" env:"PORT"`
ListenLimitint`long:"listen-limit" description:"limit the number of outstanding requests"`
KeepAlivetime.Duration`long:"keep-alive" description:"sets the TCP keep-alive timeouts on accepted connections. It prunes dead TCP connections ( e.g. closing laptop mid-download)" default:"3m"`
ReadTimeouttime.Duration`long:"read-timeout" description:"maximum duration before timing out read of the request" default:"30s"`
WriteTimeouttime.Duration`long:"write-timeout" description:"maximum duration before timing out write of the response" default:"60s"`
httpServerLnet.Listener
TLSHoststring`long:"tls-host" description:"the IP to listen on for tls, when not specified it's the same as --host" env:"TLS_HOST"`
TLSPortint`long:"tls-port" description:"the port to listen on for secure connections, defaults to a random value" env:"TLS_PORT"`
TLSCertificateflags.Filename`long:"tls-certificate" description:"the certificate to use for secure connections" env:"TLS_CERTIFICATE"`
TLSCertificateKeyflags.Filename`long:"tls-key" description:"the private key to use for secure connections" env:"TLS_PRIVATE_KEY"`
TLSCACertificateflags.Filename`long:"tls-ca" description:"the certificate authority file to be used with mutual tls auth" env:"TLS_CA_CERTIFICATE"`
TLSListenLimitint`long:"tls-listen-limit" description:"limit the number of outstanding requests"`
TLSKeepAlivetime.Duration`long:"tls-keep-alive" description:"sets the TCP keep-alive timeouts on accepted connections. It prunes dead TCP connections ( e.g. closing laptop mid-download)"`
TLSReadTimeouttime.Duration`long:"tls-read-timeout" description:"maximum duration before timing out read of the request"`
TLSWriteTimeouttime.Duration`long:"tls-write-timeout" description:"maximum duration before timing out write of the response"`
httpsServerLnet.Listener
api*operations.McsAPI
handlerhttp.Handler
hasListenersbool
shutdownchanstruct{}
shuttingDownint32
interruptedbool
interruptchanos.Signal
}
// Logf logs message either via defined user logger or via system one if no user logger is defined.
func(s*Server)Logf(fstring,args...interface{}){
ifs.api!=nil&&s.api.Logger!=nil{
s.api.Logger(f,args...)
}else{
log.Printf(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(fstring,args...interface{}){
ifs.api!=nil&&s.api.Logger!=nil{
s.api.Logger(f,args...)
os.Exit(1)
}else{
log.Fatalf(f,args...)
}
}
// SetAPI configures the server with the specified API. Needs to be called before Serve
func(s*Server)SetAPI(api*operations.McsAPI){
ifapi==nil{
s.api=nil
s.handler=nil
return
}
s.api=api
s.handler=configureAPI(api)
}
func(s*Server)hasScheme(schemestring)bool{
schemes:=s.EnabledListeners
iflen(schemes)==0{
schemes=defaultSchemes
}
for_,v:=rangeschemes{
ifv==scheme{
returntrue
}
}
returnfalse
}
// Serve the api
func(s*Server)Serve()(errerror){
if!s.hasListeners{
iferr=s.Listen();err!=nil{
returnerr
}
}
// set default handler, if none is set
ifs.handler==nil{
ifs.api==nil{
returnerrors.New("can't create the default handler, as no api is set")