all repos — homestead @ f17c0bd9ce8ad16b39c9c5ba80c917049e135bed

Code for my website

internal/server/tcp.go (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package server

import (
	"net"
	"strconv"

	"go.alanpearce.eu/x/listenfd"
)

func (s *Server) serveTCP() error {
	addr := joinHostPort(s.options.ListenAddress, s.options.Port)
	s.log.Debug(
		"fallback listener",
		"addr",
		addr,
	)
	l, err := listenfd.GetListener(0, addr, s.log.Named("tcp.listenfd"))
	if err != nil {
		return err
	}

	return s.server.Serve(l)
}

func joinHostPort(host string, port int) string {
	if len(host) < 1 {
		return ""
	}

	portString := strconv.Itoa(port)
	if host[0] == '[' && host[len(host)-1] == ']' {
		return host + ":" + portString
	}

	return net.JoinHostPort(host, portString)
}