admin: Use gin-gonic as router

This commit is contained in:
nemunaire 2022-05-16 11:38:46 +02:00
commit 8b3fbdb64a
32 changed files with 2748 additions and 1598 deletions

View file

@ -4,11 +4,12 @@ import (
"bufio"
"encoding/hex"
"fmt"
"net/http"
"path"
"strings"
"unicode"
"github.com/julienschmidt/httprouter"
"github.com/gin-gonic/gin"
"srs.epita.fr/fic-server/libfic"
)
@ -146,10 +147,10 @@ func SyncExerciceFiles(i Importer, exercice *fic.Exercice) (errs []string) {
}
// ApiGetRemoteExerciceFiles is an accessor to remote exercice files list.
func ApiGetRemoteExerciceFiles(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
func ApiGetRemoteExerciceFiles(c *gin.Context) {
theme, errs := BuildTheme(GlobalImporter, c.Params.ByName("thid"))
if theme != nil {
exercice, _, _, _, errs := BuildExercice(GlobalImporter, theme, path.Join(theme.Path, ps.ByName("exid")), nil)
exercice, _, _, _, errs := BuildExercice(GlobalImporter, theme, path.Join(theme.Path, c.Params.ByName("exid")), nil)
if exercice != nil {
files, digests, errs := BuildFilesListInto(GlobalImporter, exercice, "files")
if files != nil {
@ -164,14 +165,17 @@ func ApiGetRemoteExerciceFiles(ps httprouter.Params, _ []byte) (interface{}, err
Size: fSize,
})
}
return ret, nil
c.JSON(http.StatusOK, ret)
} else {
return nil, fmt.Errorf("%q", errs)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Errorf("%q", errs)})
return
}
} else {
return nil, fmt.Errorf("%q", errs)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Errorf("%q", errs)})
return
}
} else {
return nil, fmt.Errorf("%q", errs)
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Errorf("%q", errs)})
return
}
}

View file

@ -6,11 +6,12 @@ import (
"encoding/hex"
"fmt"
"io"
"net/http"
"os"
"path"
"strings"
"github.com/julienschmidt/httprouter"
"github.com/gin-gonic/gin"
_ "golang.org/x/crypto/blake2b"
"srs.epita.fr/fic-server/libfic"
@ -140,21 +141,24 @@ func SyncExerciceHints(i Importer, exercice *fic.Exercice, flagsBindings map[int
}
// ApiListRemoteExerciceHints is an accessor letting foreign packages to access remote exercice hints.
func ApiGetRemoteExerciceHints(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
func ApiGetRemoteExerciceHints(c *gin.Context) {
theme, errs := BuildTheme(GlobalImporter, c.Params.ByName("thid"))
if theme != nil {
exercice, _, _, _, errs := BuildExercice(GlobalImporter, theme, path.Join(theme.Path, ps.ByName("exid")), nil)
exercice, _, _, _, errs := BuildExercice(GlobalImporter, theme, path.Join(theme.Path, c.Params.ByName("exid")), nil)
if exercice != nil {
hints, errs := CheckExerciceHints(GlobalImporter, exercice)
if hints != nil {
return hints, nil
} else {
return hints, fmt.Errorf("%q", errs)
c.JSON(http.StatusOK, hints)
return
}
} else {
return exercice, fmt.Errorf("%q", errs)
c.AbortWithStatusJSON(http.StatusInternalServerError, fmt.Errorf("%q", errs))
return
}
} else {
return nil, fmt.Errorf("%q", errs)
c.AbortWithStatusJSON(http.StatusInternalServerError, fmt.Errorf("%q", errs))
return
}
c.AbortWithStatusJSON(http.StatusInternalServerError, fmt.Errorf("%q", errs))
}

View file

@ -3,13 +3,14 @@ package sync
import (
"fmt"
"math/rand"
"net/http"
"path"
"sort"
"strconv"
"strings"
"unicode"
"github.com/julienschmidt/httprouter"
"github.com/gin-gonic/gin"
"srs.epita.fr/fic-server/libfic"
)
@ -554,21 +555,25 @@ func SyncExerciceFlags(i Importer, exercice *fic.Exercice) (kmap map[int64]fic.F
}
// ApiListRemoteExerciceFlags is an accessor letting foreign packages to access remote exercice flags.
func ApiGetRemoteExerciceFlags(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
func ApiGetRemoteExerciceFlags(c *gin.Context) {
theme, errs := BuildTheme(GlobalImporter, c.Params.ByName("thid"))
if theme != nil {
exercice, _, _, _, errs := BuildExercice(GlobalImporter, theme, path.Join(theme.Path, ps.ByName("exid")), nil)
exercice, _, _, _, errs := BuildExercice(GlobalImporter, theme, path.Join(theme.Path, c.Params.ByName("exid")), nil)
if exercice != nil {
flags, errs := CheckExerciceFlags(GlobalImporter, exercice, []string{})
if flags != nil {
return flags, nil
} else {
return flags, fmt.Errorf("%q", errs)
c.JSON(http.StatusOK, flags)
return
}
} else {
return exercice, fmt.Errorf("%q", errs)
c.AbortWithStatusJSON(http.StatusInternalServerError, fmt.Errorf("%q", errs))
return
}
} else {
return nil, fmt.Errorf("%q", errs)
c.AbortWithStatusJSON(http.StatusInternalServerError, fmt.Errorf("%q", errs))
return
}
c.AbortWithStatusJSON(http.StatusInternalServerError, fmt.Errorf("%q", errs))
return
}

View file

@ -3,12 +3,13 @@ package sync
import (
"fmt"
"log"
"net/http"
"path"
"strconv"
"strings"
"github.com/BurntSushi/toml"
"github.com/julienschmidt/httprouter"
"github.com/gin-gonic/gin"
"github.com/russross/blackfriday/v2"
"srs.epita.fr/fic-server/libfic"
@ -308,26 +309,36 @@ func SyncExercices(i Importer, theme *fic.Theme) (errs []string) {
}
// ApiListRemoteExercices is an accessor letting foreign packages to access remote exercices list.
func ApiListRemoteExercices(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
func ApiListRemoteExercices(c *gin.Context) {
theme, errs := BuildTheme(GlobalImporter, c.Params.ByName("thid"))
if theme != nil {
return GetExercices(GlobalImporter, theme)
exercices, err := GetExercices(GlobalImporter, theme)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, exercices)
} else {
return nil, fmt.Errorf("%q", errs)
c.AbortWithStatusJSON(http.StatusInternalServerError, fmt.Errorf("%q", errs))
return
}
}
// ApiListRemoteExercice is an accessor letting foreign packages to access remote exercice attributes.
func ApiGetRemoteExercice(ps httprouter.Params, _ []byte) (interface{}, error) {
theme, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
func ApiGetRemoteExercice(c *gin.Context) {
theme, errs := BuildTheme(GlobalImporter, c.Params.ByName("thid"))
if theme != nil {
exercice, _, _, _, errs := BuildExercice(GlobalImporter, theme, path.Join(theme.Path, ps.ByName("exid")), nil)
exercice, _, _, _, errs := BuildExercice(GlobalImporter, theme, path.Join(theme.Path, c.Params.ByName("exid")), nil)
if exercice != nil {
return exercice, nil
c.JSON(http.StatusOK, exercice)
return
} else {
return exercice, fmt.Errorf("%q", errs)
c.JSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Errorf("%q", errs)})
return
}
} else {
return nil, fmt.Errorf("%q", errs)
c.JSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Errorf("%q", errs)})
return
}
}

View file

@ -5,13 +5,14 @@ import (
"image"
"image/jpeg"
"math/rand"
"net/http"
"os"
"path"
"regexp"
"strings"
"unicode"
"github.com/julienschmidt/httprouter"
"github.com/gin-gonic/gin"
"github.com/russross/blackfriday/v2"
"golang.org/x/image/draw"
@ -235,16 +236,23 @@ func SyncThemes(i Importer) (errs []string) {
}
// ApiListRemoteThemes is an accessor letting foreign packages to access remote themes list.
func ApiListRemoteThemes(_ httprouter.Params, _ []byte) (interface{}, error) {
return GetThemes(GlobalImporter)
func ApiListRemoteThemes(c *gin.Context) {
themes, err := GetThemes(GlobalImporter)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
c.JSON(http.StatusOK, themes)
}
// ApiListRemoteTheme is an accessor letting foreign packages to access remote main theme attributes.
func ApiGetRemoteTheme(ps httprouter.Params, _ []byte) (interface{}, error) {
r, errs := BuildTheme(GlobalImporter, ps.ByName("thid"))
func ApiGetRemoteTheme(c *gin.Context) {
r, errs := BuildTheme(GlobalImporter, c.Params.ByName("thid"))
if r == nil {
return r, fmt.Errorf("%q", errs)
} else {
return r, nil
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Errorf("%q", errs)})
return
}
c.JSON(http.StatusOK, r)
}