Skip to content

Commit

Permalink
Merge pull request #25 from cfergeau/manyhosts
Browse files Browse the repository at this point in the history
daemon: Re-read hosts file for each request
  • Loading branch information
praveenkumar authored Nov 25, 2021
2 parents 0231dc9 + 589c0d7 commit e2d4733
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
8 changes: 1 addition & 7 deletions cmd/admin-helper/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"

"github.com/code-ready/admin-helper/pkg/api"
"github.com/code-ready/admin-helper/pkg/hosts"
"github.com/kardianos/service"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -80,12 +79,7 @@ func (p *program) Start(s service.Service) error {
_ = logger.Error(err)
return
}
hosts, err := hosts.New()
if err != nil {
_ = logger.Error(err)
return
}
if err := http.Serve(ln, api.Mux(hosts)); err != nil {
if err := http.Serve(ln, api.Mux()); err != nil {
_ = logger.Error(err)
return
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/code-ready/admin-helper/pkg/types"
)

func Mux(hosts *hosts.Hosts) http.Handler {
func Mux() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, constants.Version)
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestMux(t *testing.T) {
ts := httptest.NewServer(Mux(nil))
ts := httptest.NewServer(Mux())
defer ts.Close()

client := client.New(http.DefaultClient, ts.URL)
Expand Down
24 changes: 24 additions & 0 deletions pkg/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ func defaultFilter(s string) bool {
return clusterRegexp.MatchString(s) || appRegexp.MatchString(s)
}

func Add(ip string, hosts []string) error {
h, err := New()
if err != nil {
return err
}
return h.Add(ip, hosts)
}

func (h *Hosts) Add(ip string, hosts []string) error {
if err := h.verifyHosts(hosts); err != nil {
return err
Expand Down Expand Up @@ -75,6 +83,14 @@ func (h *Hosts) Add(ip string, hosts []string) error {
return h.File.Flush()
}

func Remove(hosts []string) error {
h, err := New()
if err != nil {
return err
}
return h.Remove(hosts)
}

func (h *Hosts) Remove(hosts []string) error {
if err := h.verifyHosts(hosts); err != nil {
return err
Expand Down Expand Up @@ -102,6 +118,14 @@ func (h *Hosts) Remove(hosts []string) error {
return h.File.Flush()
}

func Clean(rawSuffixes []string) error {
h, err := New()
if err != nil {
return err
}
return h.Clean(rawSuffixes)
}

func (h *Hosts) Clean(rawSuffixes []string) error {
if err := h.checkIsWritable(); err != nil {
return err
Expand Down

0 comments on commit e2d4733

Please sign in to comment.