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) {
|
||||
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 {
|
||||
log.Println("Unable to ReadChallengeInfo:", 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())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during settings reading."})
|
||||
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())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during challenge info reading."})
|
||||
return
|
||||
|
@ -3,6 +3,7 @@ package settings
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ChallengeFile is the expected name of the file containing the challenge infos.
|
||||
@ -28,20 +29,15 @@ type ChallengeInfo struct {
|
||||
}
|
||||
|
||||
// ReadChallenge parses the file at the given location.
|
||||
func ReadChallengeInfo(path string) (*ChallengeInfo, error) {
|
||||
func ReadChallengeInfo(content string) (*ChallengeInfo, error) {
|
||||
var s ChallengeInfo
|
||||
if fd, err := os.Open(path); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer fd.Close()
|
||||
jdec := json.NewDecoder(fd)
|
||||
jdec := json.NewDecoder(strings.NewReader(content))
|
||||
|
||||
if err := jdec.Decode(&s); err != nil {
|
||||
return &s, err
|
||||
}
|
||||
|
||||
return &s, nil
|
||||
if err := jdec.Decode(&s); err != nil {
|
||||
return &s, err
|
||||
}
|
||||
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
// SaveChallenge saves challenge at the given location.
|
||||
|
Loading…
x
Reference in New Issue
Block a user