Selaa lähdekoodia

feat: add a save limits (#120)

jeessy2 1 vuosi sitten
vanhempi
sitoutus
2060ef5987
1 muutettua tiedostoa jossa 14 lisäystä ja 0 poistoa
  1. 14 0
      web/save.go

+ 14 - 0
web/save.go

@@ -4,6 +4,7 @@ import (
 	"backup-x/client"
 	"backup-x/entity"
 	"backup-x/util"
+	"fmt"
 	"log"
 	"net/http"
 	"strconv"
@@ -11,11 +12,24 @@ import (
 	"time"
 )
 
+// 服务启动时间
+var startTime = time.Now()
+
+// 保存限制时间
+var saveLimit = time.Duration(30 * time.Minute)
+
 // Save 保存
 func Save(writer http.ResponseWriter, request *http.Request) {
 	oldConf, _ := entity.GetConfigCache()
 	conf := &entity.Config{}
 
+	if oldConf.Password == "" {
+		if time.Since(startTime) > saveLimit {
+			writer.Write([]byte(fmt.Sprintf("需在 %s 之前完成用户名密码设置,请重启backup-x", startTime.Add(saveLimit).Format("2006-01-02 15:04:05"))))
+			return
+		}
+	}
+
 	conf.EncryptKey = oldConf.EncryptKey
 	if conf.EncryptKey == "" {
 		encryptKey, err := util.GenerateEncryptKey()