Skip to content

Commit 25b2abc

Browse files
committed
Closes #32
Signed-off-by: Vishal Rana <[email protected]>
1 parent b8456a4 commit 25b2abc

File tree

2,087 files changed

+461919
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,087 files changed

+461919
-13
lines changed

.dockerignore

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
Dockerfile
2-
*_test.go
3-
**/*_test.go
4-
*.md
5-
**/*.md
1+
vendor
2+
website

Gopkg.lock

+134-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

+8
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,11 @@
6060
[[constraint]]
6161
branch = "master"
6262
name = "golang.org/x/crypto"
63+
64+
[[constraint]]
65+
name = "github.com/coreos/etcd"
66+
version = "3.2.14"
67+
68+
[[constraint]]
69+
name = "github.com/hashicorp/serf"
70+
version = "0.7.0"

admin/api/handler.go renamed to admin/handler.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package api
1+
package admin
22

33
import (
4+
"net/http"
5+
46
"github.com/labstack/armor"
57
"github.com/labstack/armor/plugin"
68
"github.com/labstack/echo"
@@ -10,12 +12,29 @@ type (
1012
handler struct {
1113
armor *armor.Armor
1214
}
15+
16+
Node struct {
17+
Name string `json:"name"`
18+
}
1319
)
1420

1521
func (h *handler) getPath(c echo.Context) string {
1622
return "/" + c.Param("path")
1723
}
1824

25+
func (h *handler) nodes(c echo.Context) error {
26+
serf := h.armor.Cluster.Serf
27+
nodes := []*Node{}
28+
29+
for _, m := range serf.Members() {
30+
nodes = append(nodes, &Node{
31+
Name: m.Name,
32+
})
33+
}
34+
35+
return c.JSON(http.StatusOK, nodes)
36+
}
37+
1938
func (h *handler) lookupPlugin(host *armor.Host, path *armor.Path, plugin string) (p armor.Plugin) {
2039
plugins := []armor.Plugin{}
2140

admin/api/server.go renamed to admin/server.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package api
1+
package admin
22

33
import (
44
"github.com/labstack/armor"
@@ -14,6 +14,10 @@ func Start(a *armor.Armor) {
1414
}))
1515
h := &handler{armor: a}
1616

17+
// Nodes
18+
nodes := e.Group("/nodes")
19+
nodes.GET("", h.nodes)
20+
1721
// Global
1822

1923
// Hosts
@@ -40,5 +44,5 @@ func Start(a *armor.Armor) {
4044
pathPlugins.POST("/targets", h.addProxyTarget)
4145
pathPlugins.DELETE("/targets/:target", h.removeProxyTarget)
4246

43-
e.Start(":8081")
47+
e.Start(a.Admin.Address)
4448
}

armor.go

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"sync"
55
"time"
66

7+
"github.com/hashicorp/serf/serf"
8+
79
"github.com/labstack/echo"
810
"github.com/labstack/gommon/color"
911
"github.com/labstack/gommon/log"
@@ -12,8 +14,12 @@ import (
1214
type (
1315
Armor struct {
1416
mutex sync.RWMutex
17+
Name string `yaml:"name"`
1518
Address string `yaml:"address"`
1619
TLS *TLS `yaml:"tls"`
20+
Admin *Admin `yaml:"admin"`
21+
Cockroach *Cockroach `yaml:"cockroach"`
22+
Cluster *Cluster `yaml:"cluster"`
1723
ReadTimeout time.Duration `yaml:"read_timeout"`
1824
WriteTimeout time.Duration `yaml:"write_timeout"`
1925
RawPlugins []RawPlugin `yaml:"plugins"`
@@ -33,6 +39,20 @@ type (
3339
Email string `yaml:"email"`
3440
}
3541

42+
Admin struct {
43+
Address string `yaml:"address"`
44+
}
45+
46+
Cockroach struct {
47+
URI string `yaml:"uri"`
48+
}
49+
50+
Cluster struct {
51+
Address string `yaml:"address"`
52+
Peers []string `yaml:"peers"`
53+
Serf *serf.Serf
54+
}
55+
3656
Host struct {
3757
mutex sync.RWMutex
3858
Name string `yaml:"-"`

cluster/cluster.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cluster
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net"
7+
"os"
8+
"strconv"
9+
10+
"github.com/hashicorp/logutils"
11+
"github.com/hashicorp/serf/serf"
12+
"github.com/labstack/armor"
13+
)
14+
15+
func Start(a *armor.Armor) {
16+
conf := serf.DefaultConfig()
17+
filter := &logutils.LevelFilter{
18+
Levels: []logutils.LogLevel{"DEBUG", "INFO", "WARN", "ERROR"},
19+
MinLevel: logutils.LogLevel("WARN"),
20+
Writer: os.Stderr,
21+
}
22+
conf.MemberlistConfig.Logger = log.New(filter, "", log.LstdFlags)
23+
conf.LogOutput = filter
24+
conf.NodeName = a.Name
25+
host, port, err := net.SplitHostPort(a.Cluster.Address)
26+
if err != nil {
27+
a.Logger.Fatal(err)
28+
}
29+
if host == "" {
30+
host = "127.0.0.1"
31+
}
32+
p, err := strconv.Atoi(port)
33+
if err != nil {
34+
a.Logger.Fatal(err)
35+
}
36+
conf.MemberlistConfig.BindAddr = host
37+
conf.MemberlistConfig.BindPort = p
38+
a.Cluster.Serf, err = serf.Create(conf)
39+
if err != nil {
40+
a.Logger.Fatal(err)
41+
}
42+
a.Cluster.Serf.Join(a.Cluster.Peers, true)
43+
ch := make(chan serf.Event, 64)
44+
conf.EventCh = ch
45+
for {
46+
select {
47+
case e := <-ch:
48+
switch t := e.(type) {
49+
case serf.UserEvent:
50+
fmt.Printf("%s\n", t.Payload)
51+
}
52+
}
53+
}
54+
}

cmd/armor/main.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/go-yaml/yaml"
1111

1212
"github.com/labstack/armor"
13+
"github.com/labstack/armor/admin"
14+
"github.com/labstack/armor/cluster"
1315
"github.com/labstack/armor/http"
1416
"github.com/labstack/gommon/color"
1517
"github.com/labstack/gommon/log"
@@ -100,7 +102,14 @@ func main() {
100102
h.LoadPlugins()
101103

102104
// Start admin
103-
// go api.Start(a)
105+
if a.Admin != nil {
106+
go admin.Start(a)
107+
}
108+
109+
// Start cluster
110+
if a.Cluster != nil {
111+
go cluster.Start(a)
112+
}
104113

105114
// Start server - start
106115
colorer.Printf(banner, colorer.Red("v"+armor.Version), colorer.Blue(armor.Website))

0 commit comments

Comments
 (0)