-
Notifications
You must be signed in to change notification settings - Fork 51
/
service.go
52 lines (45 loc) · 1.7 KB
/
service.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package mainboilerplate
import (
"fmt"
"math/rand"
"net"
"os"
"time"
petname "github.com/dustinkirkland/golang-petname"
"go.gazette.dev/core/broker/protocol"
"go.gazette.dev/core/server"
)
// ZoneConfig configures the zone of the application.
type ZoneConfig struct {
Zone string `long:"zone" env:"ZONE" default:"local" description:"Availability zone within which this process is running"`
}
// ServiceConfig represents identification and addressing configuration of the process.
type ServiceConfig struct {
ZoneConfig
ID string `long:"id" env:"ID" description:"Unique ID of this process. Auto-generated if not set"`
Host string `long:"host" env:"HOST" description:"Addressable, advertised hostname or IP of this process. Hostname is used if not set"`
Port string `long:"port" env:"PORT" description:"Service port for HTTP and gRPC requests. A random port is used if not set. Port may also take the form 'unix:///path/to/socket' to use a Unix Domain Socket"`
}
// ProcessSpec of the ServiceConfig.
func (cfg ServiceConfig) BuildProcessSpec(srv *server.Server) protocol.ProcessSpec {
var err error
if cfg.ID == "" {
rand.Seed(time.Now().UnixNano()) // Seed generator for Generate's use.
cfg.ID = petname.Generate(2, "-")
}
if cfg.Host == "" {
cfg.Host, err = os.Hostname()
Must(err, "failed to determine hostname")
}
var endpoint string
switch addr := srv.RawListener.Addr().(type) {
case *net.TCPAddr:
endpoint = fmt.Sprintf("http://%s:%d", cfg.Host, addr.Port)
case *net.UnixAddr:
endpoint = fmt.Sprintf("%s://%s%s", addr.Net, cfg.Host, addr.Name)
}
return protocol.ProcessSpec{
Id: protocol.ProcessSpec_ID{Zone: cfg.Zone, Suffix: cfg.ID},
Endpoint: protocol.Endpoint(endpoint),
}
}