Browse Source

Remount if the configuration file already exists (#877)

link 2 years ago
parent
commit
fbfcc2c43a
9 changed files with 25 additions and 26 deletions
  1. 1 0
      build/sysroot/usr/lib/systemd/system/casaos.service
  2. 1 0
      go.mod
  3. 1 0
      go.sum
  4. 0 1
      main.go
  5. 4 3
      route/periodical.go
  6. 4 12
      route/v1/recover.go
  7. 5 3
      route/v1/system.go
  8. 2 2
      route/v2.go
  9. 7 5
      service/notify.go

+ 1 - 0
build/sysroot/usr/lib/systemd/system/casaos.service

@@ -1,5 +1,6 @@
 [Unit]
 After=casaos-message-bus.service
+After=rclone.service
 ConditionFileNotEmpty=/etc/casaos/casaos.conf
 Description=CasaOS Main Service
 

+ 1 - 0
go.mod

@@ -42,6 +42,7 @@ require (
 	go.uber.org/zap v1.24.0
 	golang.org/x/crypto v0.5.0
 	golang.org/x/oauth2 v0.3.0
+	golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
 	gorm.io/gorm v1.24.3
 	gotest.tools v2.2.0+incompatible
 )

+ 1 - 0
go.sum

@@ -323,6 +323,7 @@ golang.org/x/oauth2 v0.3.0 h1:6l90koy8/LaBLmLu8jpHeHexzMwEita0zFfYlggy2F8=
 golang.org/x/oauth2 v0.3.0/go.mod h1:rQrIauxkUhJ6CuwEXwymO2/eh4xz2ZWF1nBkcxS+tGk=
 golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

+ 0 - 1
main.go

@@ -147,7 +147,6 @@ func main() {
 		route.V2APIPath,
 		route.V2DocPath,
 		route.V3FilePath,
-		route.V4DirPath,
 	}
 	for _, apiPath := range routers {
 		err = service.MyService.Gateway().CreateRoute(&model.Route{

+ 4 - 3
route/periodical.go

@@ -66,9 +66,10 @@ func SendAllHardwareStatusBySocket() {
 
 	body["sys_net"] = newNet
 	systemTempMap := service.MyService.Notify().GetSystemTempMap()
-	for k, v := range systemTempMap {
-		body[k] = v
-	}
+	systemTempMap.Range(func(key, value interface{}) bool {
+		body[key.(string)] = value
+		return true
+	})
 	service.MyService.Notify().SendNotify("casaos:system:utilization", body)
 }
 

+ 4 - 12
route/v1/recover.go

@@ -58,12 +58,8 @@ func GetRecoverStorage(c *gin.Context) {
 		username = fileutil.NameAccumulation(username, "/mnt")
 		dataMap, _ := service.MyService.Storage().GetConfigByName(username)
 		if len(dataMap) > 0 {
-			c.String(200, `<p>The same configuration has been added</p><script>window.close()</script>`)
-			service.MyService.Storage().CheckAndMountByName(username)
-			notify["status"] = "warn"
-			notify["message"] = "The same configuration has been added"
-			service.MyService.Notify().SendNotify("casaos:file:recover", notify)
-			return
+			service.MyService.Storage().UnmountStorage("/mnt/" + username)
+			service.MyService.Storage().DeleteConfigByName(username)
 		}
 		dmap := make(map[string]string)
 		dmap["client_id"] = add.ClientID
@@ -115,12 +111,8 @@ func GetRecoverStorage(c *gin.Context) {
 		username = fileutil.NameAccumulation(username, "/mnt")
 		dataMap, _ := service.MyService.Storage().GetConfigByName(username)
 		if len(dataMap) > 0 {
-			c.String(200, `<p>The same configuration has been added</p><script>window.close()</script>`)
-			service.MyService.Storage().CheckAndMountByName(username)
-			notify["status"] = "warn"
-			notify["message"] = "The same configuration has been added"
-			service.MyService.Notify().SendNotify("casaos:file:recover", notify)
-			return
+			service.MyService.Storage().UnmountStorage("/mnt/" + username)
+			service.MyService.Storage().DeleteConfigByName(username)
 		}
 		dmap := make(map[string]string)
 		dmap["client_id"] = add.AppKey

+ 5 - 3
route/v1/system.go

@@ -232,9 +232,11 @@ func GetSystemUtilization(c *gin.Context) {
 	}
 
 	data["net"] = newNet
-	for k, v := range service.MyService.Notify().GetSystemTempMap() {
-		data[k] = v
-	}
+	systemMap := service.MyService.Notify().GetSystemTempMap()
+	systemMap.Range(func(key, value interface{}) bool {
+		data[key.(string)] = value
+		return true
+	})
 	c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
 }
 

+ 2 - 2
route/v2.go

@@ -69,8 +69,8 @@ func InitV2Router() http.Handler {
 
 	e.Use(echo_middleware.JWTWithConfig(echo_middleware.JWTConfig{
 		Skipper: func(c echo.Context) bool {
-			//	return c.RealIP() == "::1" || c.RealIP() == "127.0.0.1"
-			return true
+			return c.RealIP() == "::1" || c.RealIP() == "127.0.0.1"
+			//return true
 		},
 		ParseTokenFunc: func(token string, c echo.Context) (interface{}, error) {
 			claims, code := jwt.Validate(token)

+ 7 - 5
service/notify.go

@@ -15,6 +15,7 @@ import (
 	"github.com/IceWhaleTech/CasaOS/service/model"
 	"github.com/IceWhaleTech/CasaOS/types"
 	"go.uber.org/zap"
+	"golang.org/x/sync/syncmap"
 
 	socketio "github.com/googollee/go-socket.io"
 	"github.com/gorilla/websocket"
@@ -36,17 +37,18 @@ type NotifyServer interface {
 	//SendInstallAppBySocket(app notifyCommon.Application)
 	SendNotify(name string, message map[string]interface{})
 	SettingSystemTempData(message map[string]interface{})
-	GetSystemTempMap() map[string]interface{}
+	GetSystemTempMap() syncmap.Map
 }
 
 type notifyServer struct {
 	db            *gorm.DB
-	SystemTempMap map[string]interface{}
+	SystemTempMap syncmap.Map //[string]interface{}
 }
 
 func (i *notifyServer) SettingSystemTempData(message map[string]interface{}) {
 	for k, v := range message {
-		i.SystemTempMap[k] = v
+		i.SystemTempMap.Store(k, v)
+		//i.SystemTempMap[k] = v
 	}
 }
 
@@ -340,10 +342,10 @@ func SendMeg() {
 // 	}
 
 // }
-func (i *notifyServer) GetSystemTempMap() map[string]interface{} {
+func (i *notifyServer) GetSystemTempMap() syncmap.Map {
 	return i.SystemTempMap
 }
 
 func NewNotifyService(db *gorm.DB) NotifyServer {
-	return &notifyServer{db: db, SystemTempMap: make(map[string]interface{})}
+	return &notifyServer{db: db, SystemTempMap: syncmap.Map{}}
 }