admin: Copy challenge.json from sync to distsettings
This commit is contained in:
parent
4b2625c47d
commit
eb07eadae0
@ -3,8 +3,10 @@ package api
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"time"
|
||||
@ -58,7 +60,21 @@ func getROSettings(c *gin.Context) {
|
||||
}
|
||||
|
||||
func getChallengeInfo(c *gin.Context) {
|
||||
challengeinfo, err := sync.GetFileContent(sync.GlobalImporter, "challenge.json")
|
||||
var challengeinfo string
|
||||
var err error
|
||||
if sync.GlobalImporter == nil {
|
||||
if fd, err := os.Open(path.Join(settings.SettingsDir, settings.ChallengeFile)); err == nil {
|
||||
defer fd.Close()
|
||||
var buf []byte
|
||||
buf, err = io.ReadAll(fd)
|
||||
if err == nil {
|
||||
challengeinfo = string(buf)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
challengeinfo, err = sync.GetFileContent(sync.GlobalImporter, settings.ChallengeFile)
|
||||
}
|
||||
|
||||
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())})
|
||||
@ -96,6 +112,12 @@ func saveChallengeInfo(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := settings.SaveChallengeInfo(path.Join(settings.SettingsDir, settings.ChallengeFile), info); err != nil {
|
||||
log.Println("Unable to SaveChallengeInfo:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to save distributed challenge info: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, info)
|
||||
}
|
||||
|
||||
|
@ -142,6 +142,20 @@ func main() {
|
||||
log.Fatal("Unable to initialize the importer:", err)
|
||||
}
|
||||
log.Println("Using", sync.GlobalImporter.Kind())
|
||||
|
||||
// Update distributed challenge.json
|
||||
challengeinfo, err := sync.GetFileContent(sync.GlobalImporter, settings.ChallengeFile)
|
||||
if err == nil {
|
||||
if fd, err := os.Create(path.Join(settings.SettingsDir, settings.ChallengeFile)); err != nil {
|
||||
log.Fatal("Unable to open SETTINGS/challenge.json:", err)
|
||||
} else {
|
||||
fd.Write([]byte(challengeinfo))
|
||||
err = fd.Close()
|
||||
if err != nil {
|
||||
log.Fatal("Something went wrong during SETTINGS/challenge.json writing:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sanitize options
|
||||
|
Loading…
x
Reference in New Issue
Block a user