diff --git a/caddy.go b/caddy.go index ff578d12..b0ec1af6 100644 --- a/caddy.go +++ b/caddy.go @@ -76,7 +76,7 @@ type Instance struct { context Context // servers is the list of servers with their listeners. - servers []serverListener + servers []ServerListener // these callbacks execute when certain events occur onFirstStartup []func() error // starting, not as part of a restart @@ -86,6 +86,9 @@ type Instance struct { onFinalShutdown []func() error // stopping, not as part of a restart } +// Servers returns the ServerListeners in i. +func (i *Instance) Servers() []ServerListener { return i.servers } + // Stop stops all servers contained in i. It does NOT // execute shutdown callbacks. func (i *Instance) Stop() error { @@ -202,7 +205,7 @@ func (i *Instance) Restart(newCaddyfile Input) (*Instance, error) { // internally-kept list of servers that is running. For // saved servers, graceful restarts will be provided. func (i *Instance) SaveServer(s Server, ln net.Listener) { - i.servers = append(i.servers, serverListener{server: s, listener: ln}) + i.servers = append(i.servers, ServerListener{server: s, listener: ln}) } // HasListenerWithAddress returns whether this package is @@ -644,7 +647,7 @@ func startServers(serverList []Server, inst *Instance, restartFds map[string]res errChan <- s.ServePacket(pc) }(s, ln, pc, inst) - inst.servers = append(inst.servers, serverListener{server: s, listener: ln, packet: pc}) + inst.servers = append(inst.servers, ServerListener{server: s, listener: ln, packet: pc}) } // Log errors that may be returned from Serve() calls, @@ -836,7 +839,7 @@ var ( instancesMu sync.Mutex ) -const ( +var ( // DefaultConfigFile is the name of the configuration file that is loaded // by default if no other file is specified. DefaultConfigFile = "Caddyfile" diff --git a/plugins.go b/plugins.go index 5134aa91..bf137170 100644 --- a/plugins.go +++ b/plugins.go @@ -82,13 +82,31 @@ func ValidDirectives(serverType string) []string { return stype.Directives } -// serverListener pairs a server to its listener and/or packetconn. -type serverListener struct { +// ServerListener pairs a server to its listener and/or packetconn. +type ServerListener struct { server Server listener net.Listener packet net.PacketConn } +// LocalAddr returns the local network address of the packetconn. It returns +// nil when it is not set. +func (s ServerListener) LocalAddr() net.Addr { + if s.packet == nil { + return nil + } + return s.packet.LocalAddr() +} + +// Addr returns the listener's network address. It returns nil when it is +// not set. +func (s ServerListener) Addr() net.Addr { + if s.listener == nil { + return nil + } + return s.listener.Addr() +} + // Context is a type which carries a server type through // the load and setup phase; it maintains the state // between loading the Caddyfile, then executing its