admin: heath api now checks untreated files

This commit is contained in:
nemunaire 2019-12-06 18:37:35 +01:00
parent b4fa57f9c9
commit a475617657
2 changed files with 56 additions and 26 deletions

56
admin/api/health.go Normal file
View File

@ -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
}
}

View File

@ -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
}
}))
}