Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Latest commit

 

History

History
57 lines (45 loc) · 1.54 KB

README.md

File metadata and controls

57 lines (45 loc) · 1.54 KB

AnyPort GoDoc

anyport is a Go package to provide with a tool to bind any available or random port to listen for incoming TCP connections. The use case does make sense when a started (micro)service should not be pinned to a fixed TCP port but could be found through Service Discovery (like Consul) endpoints.

Usage

Random TCP Port (any range)

Plain TCP:

  anyPort, err := anyport.ListenInsecure("localhost")
  if nil != err {
      panic(err)
  }
  defer anyPort.Listener.Close()
  log.Printf("Incoming port: %d", anyPort.PortNumber)

Over TLS:

  tlsConfig := tls.Config{...}
  anyPort, err := anyport.ListenSecure("localhost", &tlsConfig)
  if nil != err {
      panic(err)
  }
  defer anyPort.Listener.Close()
  log.Printf("Incoming TLS port: %d", anyPort.PortNumber)

Random TCP port (in a fixed range)

Plain TCP:

  anyPort, err := anyport.ListenInsecure("localhost:3000-5000")
  if nil != err {
      panic(err)
  }
  defer anyPort.Listener.Close()
  log.Printf("Incoming port: %d", anyPort.PortNumber)

Over TLS:

  tlsConfig := tls.Config{...}
  anyPort, err := anyport.ListenSecure("localhost:3000-5000", &tlsConfig)
  if nil != err {
      panic(err)
  }
  defer anyPort.Listener.Close()
  log.Printf("Incoming TLS port: %d", anyPort.PortNumber)

License

The library is released under the MIT license. See LICENSE file.