aboutsummaryrefslogtreecommitdiff
path: root/httpd.go
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2020-07-26 22:11:24 +0700
committerShulhan <m.shulhan@gmail.com>2020-07-26 22:38:03 +0700
commit73666b02edc55997ab187d0eefea48b662c9de6d (patch)
tree7a5c4146cd73ae81f515165116b12378f588c9cc /httpd.go
parent6cbf38421d453c070cb79ddffde388ad395f0505 (diff)
downloadrescached-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.go18
1 files changed, 15 insertions, 3 deletions
diff --git a/httpd.go b/httpd.go
index 7dd04a1..5f422c4 100644
--- a/httpd.go
+++ b/httpd.go
@@ -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