From a475617657245dae8abde55cb9ed0d4c28705cdf Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Fri, 6 Dec 2019 18:37:35 +0100 Subject: [PATCH] admin: heath api now checks untreated files --- admin/api/health.go | 56 ++++++++++++++++++++++++++++++++++++++++++ admin/api/timestamp.go | 26 -------------------- 2 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 admin/api/health.go delete mode 100644 admin/api/timestamp.go diff --git a/admin/api/health.go b/admin/api/health.go new file mode 100644 index 00000000..43f3da77 --- /dev/null +++ b/admin/api/health.go @@ -0,0 +1,56 @@ +package api + +import ( + "fmt" + "io/ioutil" + "os" + "path" + "strings" + "time" + + "github.com/julienschmidt/httprouter" +) + +var TimestampCheck = "submissions" + +func init() { + router.GET("/api/timestamps.json", apiHandler( + func(httprouter.Params, []byte) (interface{}, error) { + if stat, err := os.Stat(TimestampCheck); err != nil { + return nil, err + } else { + now := time.Now().UTC() + return map[string]interface{}{ + "frontend": stat.ModTime().UTC(), + "backend": now, + "diffFB": now.Sub(stat.ModTime()), + }, nil + } + })) + router.GET("/api/health.json", apiHandler(GetHealth)) +} + +func getHealth(pathname string) (ret []string) { + if ds, err := ioutil.ReadDir(pathname); err != nil { + ret = append(ret, fmt.Sprintf("%s: unable to ReadDir: %s", strings.TrimPrefix(pathname, TimestampCheck), err)) + return + } else { + for _, d := range ds { + p := path.Join(pathname, d.Name()) + if d.IsDir() && d.Name() != ".tmp" && d.Mode()&os.ModeSymlink == 0 { + ret = append(ret, getHealth(p)...) + } else if !d.IsDir() && d.Mode()&os.ModeSymlink == 0 { + ret = append(ret, fmt.Sprintf("%s/%s: existing untreated file.", strings.TrimPrefix(pathname, TimestampCheck), d.Name())) + } + } + return + } +} + +func GetHealth(httprouter.Params, []byte) (interface{}, error) { + if _, err := os.Stat(TimestampCheck); err != nil { + return nil, err + } else { + return getHealth(TimestampCheck), nil + } +} diff --git a/admin/api/timestamp.go b/admin/api/timestamp.go deleted file mode 100644 index 9da1f77c..00000000 --- a/admin/api/timestamp.go +++ /dev/null @@ -1,26 +0,0 @@ -package api - -import ( - "os" - "time" - - "github.com/julienschmidt/httprouter" -) - -var TimestampCheck = "submissions" - -func init() { - router.GET("/api/timestamps.json", apiHandler( - func(httprouter.Params, []byte) (interface{}, error) { - if stat, err := os.Stat(TimestampCheck); err != nil { - return nil, err - } else { - now := time.Now().UTC() - return map[string]interface{}{ - "frontend": stat.ModTime().UTC(), - "backend": now, - "diffFB": now.Sub(stat.ModTime()), - }, nil - } - })) -}