all repos — homestead @ a9f47f4e4ed3223e86dc1d5c3edd005b8d9ce589

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
37
package server

import (
	"net"
	"strconv"

	"gitlab.com/tozd/go/errors"
	"go.alanpearce.eu/x/listenfd"
)

func (s *Server) serveTCP() errors.E {
	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 errors.WithStack(err)
	}

	return errors.WithStack(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)
}