Skip to content

Commit e2d4733

Browse files
authored
Merge pull request #25 from cfergeau/manyhosts
daemon: Re-read hosts file for each request
2 parents 0231dc9 + 589c0d7 commit e2d4733

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

cmd/admin-helper/daemon.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66

77
"github.com/code-ready/admin-helper/pkg/api"
8-
"github.com/code-ready/admin-helper/pkg/hosts"
98
"github.com/kardianos/service"
109
"github.com/spf13/cobra"
1110
)
@@ -80,12 +79,7 @@ func (p *program) Start(s service.Service) error {
8079
_ = logger.Error(err)
8180
return
8281
}
83-
hosts, err := hosts.New()
84-
if err != nil {
85-
_ = logger.Error(err)
86-
return
87-
}
88-
if err := http.Serve(ln, api.Mux(hosts)); err != nil {
82+
if err := http.Serve(ln, api.Mux()); err != nil {
8983
_ = logger.Error(err)
9084
return
9185
}

pkg/api/mux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/code-ready/admin-helper/pkg/types"
1111
)
1212

13-
func Mux(hosts *hosts.Hosts) http.Handler {
13+
func Mux() http.Handler {
1414
mux := http.NewServeMux()
1515
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
1616
_, _ = fmt.Fprint(w, constants.Version)

pkg/api/mux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func TestMux(t *testing.T) {
14-
ts := httptest.NewServer(Mux(nil))
14+
ts := httptest.NewServer(Mux())
1515
defer ts.Close()
1616

1717
client := client.New(http.DefaultClient, ts.URL)

pkg/hosts/hosts.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ func defaultFilter(s string) bool {
4343
return clusterRegexp.MatchString(s) || appRegexp.MatchString(s)
4444
}
4545

46+
func Add(ip string, hosts []string) error {
47+
h, err := New()
48+
if err != nil {
49+
return err
50+
}
51+
return h.Add(ip, hosts)
52+
}
53+
4654
func (h *Hosts) Add(ip string, hosts []string) error {
4755
if err := h.verifyHosts(hosts); err != nil {
4856
return err
@@ -75,6 +83,14 @@ func (h *Hosts) Add(ip string, hosts []string) error {
7583
return h.File.Flush()
7684
}
7785

86+
func Remove(hosts []string) error {
87+
h, err := New()
88+
if err != nil {
89+
return err
90+
}
91+
return h.Remove(hosts)
92+
}
93+
7894
func (h *Hosts) Remove(hosts []string) error {
7995
if err := h.verifyHosts(hosts); err != nil {
8096
return err
@@ -102,6 +118,14 @@ func (h *Hosts) Remove(hosts []string) error {
102118
return h.File.Flush()
103119
}
104120

121+
func Clean(rawSuffixes []string) error {
122+
h, err := New()
123+
if err != nil {
124+
return err
125+
}
126+
return h.Clean(rawSuffixes)
127+
}
128+
105129
func (h *Hosts) Clean(rawSuffixes []string) error {
106130
if err := h.checkIsWritable(); err != nil {
107131
return err

0 commit comments

Comments
 (0)