74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
"time"
|
|
|
|
"srs.epita.fr/fic-server/admin/pki"
|
|
|
|
"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))
|
|
}
|
|
|
|
type healthFileReport struct {
|
|
IdTeam string `json:"id_team,omitempty"`
|
|
Path string `json:"path"`
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
func getHealth(pathname string) (ret []healthFileReport) {
|
|
if ds, err := ioutil.ReadDir(pathname); err != nil {
|
|
ret = append(ret, healthFileReport{
|
|
Path: strings.TrimPrefix(pathname, TimestampCheck),
|
|
Error: fmt.Sprintf("unable to ReadDir: %s", 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 && time.Since(d.ModTime()) > 2 * time.Second {
|
|
teamDir := strings.TrimPrefix(pathname, TimestampCheck)
|
|
idteam, _ := pki.GetAssociation(path.Join(TeamsDir, teamDir))
|
|
ret = append(ret, healthFileReport{
|
|
IdTeam: idteam,
|
|
Path: path.Join(teamDir, d.Name()),
|
|
Error: "existing untreated file",
|
|
})
|
|
}
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
func GetHealth(httprouter.Params, []byte) (interface{}, error) {
|
|
if _, err := os.Stat(TimestampCheck); err != nil {
|
|
return nil, err
|
|
} else {
|
|
return getHealth(TimestampCheck), nil
|
|
}
|
|
}
|