-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go_
148 lines (121 loc) · 3.04 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2018 Axel Etcheverry. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"os/signal"
"time"
"github.com/hashicorp/memberlist"
"github.com/rs/zerolog/log"
)
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
hostname, _ := os.Hostname()
ip, err := externalIP()
if err != nil {
log.Panic().Err(err).Msg("externalIP")
}
fmt.Printf("IP: %s\n", ip)
addrs, err := net.LookupHost("hyperfs-index")
if err != nil {
log.Panic().Err(err).Msg("LookupHost")
}
fmt.Printf("LookupHost: %+v\n", addrs)
ips, err := net.LookupIP("hyperfs-index")
if err != nil {
log.Panic().Err(err).Msg("LookupIP")
}
fmt.Printf("LookupIP: %+v\n", ips)
resp, err := http.Get("http://hyperfs-index:8000/ip")
if err != nil {
log.Error().Err(err).Msg("http.Get ip")
}
iplookuped, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error().Err(err).Msg("ioutil.ReadAll")
}
fmt.Printf("peer: %+v\n", string(iplookuped))
cfg := memberlist.DefaultLANConfig()
cfg.Name = hostname
list, err := memberlist.Create(cfg)
if err != nil {
log.Error().Err(err).Msg("memberlist.Create")
}
if len(iplookuped) > 0 {
// Join an existing cluster by specifying at least one known member.
if _, err := list.Join([]string{string(iplookuped)}); err != nil {
log.Error().Err(err).Msg("Failed to join cluster")
}
}
// Ask for members of the cluster
for _, member := range list.Members() {
fmt.Printf("Member: %s %s\n", member.Name, member.Addr)
}
healthHandler := func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
io.WriteString(w, "Hello, world!\n")
}
http.HandleFunc("/health", healthHandler)
http.HandleFunc("/members", func(w http.ResponseWriter, req *http.Request) {
json.NewEncoder(w).Encode(list.Members())
})
http.HandleFunc("/ip", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
io.WriteString(w, ip)
})
go func() {
if err := http.ListenAndServe(":8000", nil); err != nil {
log.Panic().Err(err).Msg("ListenAndServe")
}
}()
node := list.LocalNode()
fmt.Printf("Local member %s:%d\n", node.Addr, node.Port)
<-c
list.Leave(2 * time.Second)
list.Shutdown()
}
func externalIP() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("are you connected to the network?")
}