forked from gdy666/lucky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.go
106 lines (87 loc) · 2.66 KB
/
configure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package web
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"sync"
"time"
"github.com/gdy666/lucky/config"
"github.com/gdy666/lucky/thirdlib/gdylib/fileutils"
"github.com/gdy666/lucky/thirdlib/gdylib/stringsp"
"github.com/gin-gonic/gin"
)
func configure(c *gin.Context) {
//c config.GetConfig()
configureBytes := config.GetConfigureBytes()
//c.Header("Content-Type", "application/json")
//c.Data(http.StatusOK, "application/json", configureBytes)
c.JSON(http.StatusOK,
gin.H{
"ret": 0,
"time": time.Now().Format("060102150405"),
"configure": string(configureBytes)},
)
}
var restoreConfigureVar *config.ProgramConfigure
var restoreConfigureKey string
var restoreConfigureMutex sync.Mutex
func restoreConfigureConfirm(c *gin.Context) {
restoreConfigureMutex.Lock()
defer restoreConfigureMutex.Unlock()
key := c.Query("key")
if key != restoreConfigureKey {
c.JSON(http.StatusOK, gin.H{"ret": 1, "msg": "key不一致"})
return
}
err := config.SetConfig(restoreConfigureVar)
if err != nil {
c.JSON(http.StatusOK, gin.H{"ret": 1, "msg": "还原配置出错"})
return
}
rebootOnce.Do(func() {
go func() {
fileutils.OpenProgramOrFile(os.Args)
os.Exit(0)
}()
})
c.JSON(http.StatusOK, gin.H{"ret": 0, "msg": "还原配置成功", "port": restoreConfigureVar.BaseConfigure.AdminWebListenPort})
}
func restoreConfigure(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusOK, gin.H{"ret": 1, "msg": fmt.Sprintf("c.FormFile err:%s", err.Error())})
return
}
src, err := file.Open()
if err != nil {
c.JSON(http.StatusOK, gin.H{"ret": 1, "msg": fmt.Sprintf("file.Open err:%s", err.Error())})
return
}
defer src.Close()
fileBytes, err := io.ReadAll(src)
if err != nil {
c.JSON(http.StatusOK, gin.H{"ret": 1, "msg": fmt.Sprintf("ioutil.ReadAll err:%s", err.Error())})
return
}
//log.Printf("file:%s\n", string(fileBytes))
var conf config.ProgramConfigure
err = json.Unmarshal(fileBytes, &conf)
if err != nil {
c.JSON(http.StatusOK, gin.H{"ret": 1, "msg": fmt.Sprintf("配置文件[%s]有误", file.Filename)})
return
}
if conf.BaseConfigure.AdminAccount == "" ||
conf.BaseConfigure.AdminPassword == "" ||
conf.BaseConfigure.AdminWebListenPort <= 0 ||
conf.BaseConfigure.AdminWebListenPort >= 65536 {
c.JSON(http.StatusOK, gin.H{"ret": 1, "msg": fmt.Sprintf("配置文件[%s]参数有误", file.Filename)})
return
}
restoreConfigureMutex.Lock()
defer restoreConfigureMutex.Unlock()
restoreConfigureVar = &conf
restoreConfigureKey = stringsp.GetRandomStringNum(16)
c.JSON(http.StatusOK, gin.H{"ret": 0, "file": file.Filename, "restoreConfigureKey": restoreConfigureKey})
}