admin: Read challenge.json from imported directory
This commit is contained in:
parent
560110ba5e
commit
8ed9415c68
@ -57,7 +57,14 @@ func getROSettings(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getChallengeInfo(c *gin.Context) {
|
func getChallengeInfo(c *gin.Context) {
|
||||||
s, err := settings.ReadChallengeInfo(path.Join(settings.SettingsDir, settings.ChallengeFile))
|
challengeinfo, err := sync.GetFileContent(sync.GlobalImporter, "challenge.json")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Unable to retrieve challenge.json:", err.Error())
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to retrive challenge.json: %s", err.Error())})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s, err := settings.ReadChallengeInfo(challengeinfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Unable to ReadChallengeInfo:", err.Error())
|
log.Println("Unable to ReadChallengeInfo:", err.Error())
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to read challenge info: %s", err.Error())})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to read challenge info: %s", err.Error())})
|
||||||
|
@ -23,7 +23,12 @@ func declareThemesRoutes(router *gin.RouterGroup) {
|
|||||||
log.Printf("Unable to ReadSettings: %s", err.Error())
|
log.Printf("Unable to ReadSettings: %s", err.Error())
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during settings reading."})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during settings reading."})
|
||||||
return
|
return
|
||||||
} else if ch, err := settings.ReadChallengeInfo(path.Join(settings.SettingsDir, settings.ChallengeFile)); err != nil {
|
|
||||||
|
} else if challengeinfo, err := sync.GetFileContent(sync.GlobalImporter, "challenge.json"); err != nil {
|
||||||
|
log.Println("Unable to retrieve challenge.json:", err.Error())
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to retrive challenge.json: %s", err.Error())})
|
||||||
|
return
|
||||||
|
} else if ch, err := settings.ReadChallengeInfo(challengeinfo); err != nil {
|
||||||
log.Printf("Unable to ReadChallengeInfo: %s", err.Error())
|
log.Printf("Unable to ReadChallengeInfo: %s", err.Error())
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during challenge info reading."})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during challenge info reading."})
|
||||||
return
|
return
|
||||||
|
@ -3,6 +3,7 @@ package settings
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ChallengeFile is the expected name of the file containing the challenge infos.
|
// ChallengeFile is the expected name of the file containing the challenge infos.
|
||||||
@ -28,13 +29,9 @@ type ChallengeInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadChallenge parses the file at the given location.
|
// ReadChallenge parses the file at the given location.
|
||||||
func ReadChallengeInfo(path string) (*ChallengeInfo, error) {
|
func ReadChallengeInfo(content string) (*ChallengeInfo, error) {
|
||||||
var s ChallengeInfo
|
var s ChallengeInfo
|
||||||
if fd, err := os.Open(path); err != nil {
|
jdec := json.NewDecoder(strings.NewReader(content))
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
defer fd.Close()
|
|
||||||
jdec := json.NewDecoder(fd)
|
|
||||||
|
|
||||||
if err := jdec.Decode(&s); err != nil {
|
if err := jdec.Decode(&s); err != nil {
|
||||||
return &s, err
|
return &s, err
|
||||||
@ -42,7 +39,6 @@ func ReadChallengeInfo(path string) (*ChallengeInfo, error) {
|
|||||||
|
|
||||||
return &s, nil
|
return &s, nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// SaveChallenge saves challenge at the given location.
|
// SaveChallenge saves challenge at the given location.
|
||||||
func SaveChallengeInfo(path string, s *ChallengeInfo) error {
|
func SaveChallengeInfo(path string, s *ChallengeInfo) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user