admin: Use gin-gonic as router
This commit is contained in:
parent
83468ad723
commit
8b3fbdb64a
32 changed files with 2785 additions and 1635 deletions
|
@ -3,18 +3,20 @@ package api
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var DashboardDir string
|
||||
|
||||
func init() {
|
||||
router.GET("/api/public/:sid", apiHandler(getPublic))
|
||||
router.DELETE("/api/public/:sid", apiHandler(deletePublic))
|
||||
router.PUT("/api/public/:sid", apiHandler(savePublic))
|
||||
func declarePublicRoutes(router *gin.RouterGroup) {
|
||||
router.GET("/public/:sid", getPublic)
|
||||
router.DELETE("/public/:sid", deletePublic)
|
||||
router.PUT("/public/:sid", savePublic)
|
||||
}
|
||||
|
||||
type FICPublicScene struct {
|
||||
|
@ -62,31 +64,44 @@ func savePublicTo(path string, s FICPublicDisplay) error {
|
|||
}
|
||||
}
|
||||
|
||||
func getPublic(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
if _, err := os.Stat(path.Join(DashboardDir, fmt.Sprintf("public%s.json", ps.ByName("sid")))); !os.IsNotExist(err) {
|
||||
return readPublic(path.Join(DashboardDir, fmt.Sprintf("public%s.json", ps.ByName("sid"))))
|
||||
} else {
|
||||
return FICPublicDisplay{Scenes: []FICPublicScene{}, Side: []FICPublicScene{}}, nil
|
||||
func getPublic(c *gin.Context) {
|
||||
if _, err := os.Stat(path.Join(DashboardDir, fmt.Sprintf("public%s.json", c.Params.ByName("sid")))); !os.IsNotExist(err) {
|
||||
p, err := readPublic(path.Join(DashboardDir, fmt.Sprintf("public%s.json", c.Params.ByName("sid"))))
|
||||
if err != nil {
|
||||
log.Println("Unable to readPublic in getPublic:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during scene retrieval."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, p)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, FICPublicDisplay{Scenes: []FICPublicScene{}, Side: []FICPublicScene{}})
|
||||
}
|
||||
|
||||
func deletePublic(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
if err := savePublicTo(path.Join(DashboardDir, fmt.Sprintf("public%s.json", ps.ByName("sid"))), FICPublicDisplay{}); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return FICPublicDisplay{Scenes: []FICPublicScene{}, Side: []FICPublicScene{}}, nil
|
||||
func deletePublic(c *gin.Context) {
|
||||
if err := savePublicTo(path.Join(DashboardDir, fmt.Sprintf("public%s.json", c.Params.ByName("sid"))), FICPublicDisplay{}); err != nil {
|
||||
log.Println("Unable to deletePublic:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during scene deletion."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, FICPublicDisplay{Scenes: []FICPublicScene{}, Side: []FICPublicScene{}})
|
||||
}
|
||||
|
||||
func savePublic(ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
func savePublic(c *gin.Context) {
|
||||
var scenes FICPublicDisplay
|
||||
if err := json.Unmarshal(body, &scenes); err != nil {
|
||||
return nil, err
|
||||
err := c.ShouldBindJSON(&scenes)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := savePublicTo(path.Join(DashboardDir, fmt.Sprintf("public%s.json", ps.ByName("sid"))), scenes); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return scenes, err
|
||||
if err := savePublicTo(path.Join(DashboardDir, fmt.Sprintf("public%s.json", c.Params.ByName("sid"))), scenes); err != nil {
|
||||
log.Println("Unable to savePublicTo:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during scene saving."})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, scenes)
|
||||
}
|
||||
|
|
Reference in a new issue