diff options
| author | Shulhan <m.shulhan@gmail.com> | 2020-07-26 22:11:24 +0700 |
|---|---|---|
| committer | Shulhan <m.shulhan@gmail.com> | 2020-07-26 22:38:03 +0700 |
| commit | 73666b02edc55997ab187d0eefea48b662c9de6d (patch) | |
| tree | 7a5c4146cd73ae81f515165116b12378f588c9cc /httpd.go | |
| parent | 6cbf38421d453c070cb79ddffde388ad395f0505 (diff) | |
| download | rescached-73666b02edc55997ab187d0eefea48b662c9de6d.tar.xz | |
httpd: fetch the hosts block file if its not exist when enabled
While at it, return an error when doing update on hosts block.
Diffstat (limited to 'httpd.go')
| -rw-r--r-- | httpd.go | 18 |
1 files changed, 15 insertions, 3 deletions
@@ -6,6 +6,7 @@ package rescached import ( "encoding/json" + "errors" "fmt" "log" stdhttp "net/http" @@ -266,9 +267,18 @@ func (srv *Server) apiHostsBlockUpdate( } func (srv *Server) hostsBlockEnable(hb *hostsBlock) (err error) { + hb.IsEnabled = true + err = hb.unhide() if err != nil { - return err + if !errors.Is(err, os.ErrNotExist) { + return err + } + // File not exist, fetch new from serfer. + err = hb.update() + if err != nil { + return err + } } hfile, err := dns.ParseHostsFile(filepath.Join(dirHosts, hb.Name)) @@ -278,8 +288,10 @@ func (srv *Server) hostsBlockEnable(hb *hostsBlock) (err error) { srv.dns.PopulateCaches(hfile.Messages) - hb.IsEnabled = true - hb.update() + err = hb.update() + if err != nil { + return err + } srv.env.HostsFiles = append(srv.env.HostsFiles, convertHostsFile(hfile)) return nil |
