admin: Use gin-gonic as router
This commit is contained in:
parent
83468ad723
commit
8b3fbdb64a
32 changed files with 2748 additions and 1598 deletions
|
|
@ -3,6 +3,7 @@ package api
|
|||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
|
@ -10,26 +11,26 @@ import (
|
|||
|
||||
"srs.epita.fr/fic-server/admin/pki"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
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 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)
|
||||
}
|
||||
|
||||
type healthFileReport struct {
|
||||
|
|
@ -41,22 +42,22 @@ type healthFileReport struct {
|
|||
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),
|
||||
})
|
||||
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 {
|
||||
} 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",
|
||||
Path: path.Join(teamDir, d.Name()),
|
||||
Error: "existing untreated file",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -64,10 +65,11 @@ func getHealth(pathname string) (ret []healthFileReport) {
|
|||
}
|
||||
}
|
||||
|
||||
func GetHealth(httprouter.Params, []byte) (interface{}, error) {
|
||||
func GetHealth(c *gin.Context) {
|
||||
if _, err := os.Stat(TimestampCheck); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return getHealth(TimestampCheck), nil
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": fmt.Sprintf("health.json: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, getHealth(TimestampCheck))
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue