2019-12-06 17:37:35 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2022-05-16 09:38:46 +00:00
|
|
|
"net/http"
|
2019-12-06 17:37:35 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-01-29 10:35:01 +00:00
|
|
|
"srs.epita.fr/fic-server/admin/pki"
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-12-06 17:37:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var TimestampCheck = "submissions"
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func declareHealthRoutes(router *gin.RouterGroup) {
|
|
|
|
router.GET("/timestamps.json", func(c *gin.Context) {
|
|
|
|
stat, err := os.Stat(TimestampCheck)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": fmt.Sprintf("timestamp.json: %s", err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
now := time.Now().UTC()
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"frontend": stat.ModTime().UTC(),
|
|
|
|
"backend": now,
|
|
|
|
"diffFB": now.Sub(stat.ModTime()),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
router.GET("/health.json", GetHealth)
|
2019-12-06 17:37:35 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 10:35:01 +00:00
|
|
|
type healthFileReport struct {
|
|
|
|
IdTeam string `json:"id_team,omitempty"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHealth(pathname string) (ret []healthFileReport) {
|
2019-12-06 17:37:35 +00:00
|
|
|
if ds, err := ioutil.ReadDir(pathname); err != nil {
|
2020-01-29 10:35:01 +00:00
|
|
|
ret = append(ret, healthFileReport{
|
2022-05-16 09:38:46 +00:00
|
|
|
Path: strings.TrimPrefix(pathname, TimestampCheck),
|
|
|
|
Error: fmt.Sprintf("unable to ReadDir: %s", err),
|
|
|
|
})
|
2019-12-06 17:37:35 +00:00
|
|
|
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)...)
|
2022-05-16 09:38:46 +00:00
|
|
|
} else if !d.IsDir() && d.Mode()&os.ModeSymlink == 0 && time.Since(d.ModTime()) > 2*time.Second {
|
2020-01-29 10:35:01 +00:00
|
|
|
teamDir := strings.TrimPrefix(pathname, TimestampCheck)
|
|
|
|
idteam, _ := pki.GetAssociation(path.Join(TeamsDir, teamDir))
|
|
|
|
ret = append(ret, healthFileReport{
|
|
|
|
IdTeam: idteam,
|
2022-05-16 09:38:46 +00:00
|
|
|
Path: path.Join(teamDir, d.Name()),
|
|
|
|
Error: "existing untreated file",
|
2020-01-29 10:35:01 +00:00
|
|
|
})
|
2019-12-06 17:37:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func GetHealth(c *gin.Context) {
|
2019-12-06 17:37:35 +00:00
|
|
|
if _, err := os.Stat(TimestampCheck); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": fmt.Sprintf("health.json: %s", err.Error())})
|
|
|
|
return
|
2019-12-06 17:37:35 +00:00
|
|
|
}
|
2022-05-16 09:38:46 +00:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, getHealth(TimestampCheck))
|
2019-12-06 17:37:35 +00:00
|
|
|
}
|