Explorar o código

move common proxy to CasaOS-Common (#643)

...and use gateway proxy from CasaOS-Common
Tiger Wang %!s(int64=2) %!d(string=hai) anos
pai
achega
0fb5cab480
Modificáronse 8 ficheiros con 11 adicións e 218 borrados
  1. 0 86
      common/notify.go
  2. 0 29
      common/notify_test.go
  3. 0 78
      common/share.go
  4. 0 14
      common/share_test.go
  5. 1 1
      go.mod
  6. 2 2
      go.sum
  7. 3 3
      main.go
  8. 5 5
      service/service.go

+ 0 - 86
common/notify.go

@@ -1,86 +0,0 @@
-package common
-
-import (
-	"bytes"
-	"encoding/json"
-	"errors"
-	"fmt"
-	"net/http"
-	"os"
-	"path/filepath"
-	"strings"
-)
-
-const (
-	CasaOSURLFilename = "casaos.url"
-	APICasaOSNotify   = "/v1/notify"
-)
-
-type NotifyService interface {
-	SendNotify(path string, message map[string]interface{}) error
-	SendSystemStatusNotify(message map[string]interface{}) error
-}
-type notifyService struct {
-	address string
-}
-
-func (n *notifyService) SendNotify(path string, message map[string]interface{}) error {
-	url := strings.TrimSuffix(n.address, "/") + APICasaOSNotify + "/" + path
-	body, err := json.Marshal(message)
-	if err != nil {
-		return err
-	}
-	response, err := http.Post(url, "application/json", bytes.NewBuffer(body))
-	if err != nil {
-		return err
-	}
-
-	if response.StatusCode != http.StatusOK {
-		return errors.New("failed to send notify (status code: " + fmt.Sprint(response.StatusCode) + ")")
-	}
-	return nil
-}
-
-// disk: "sys_disk":{"size":56866869248,"avail":5855485952,"health":true,"used":48099700736}
-// usb:   "sys_usb":[{"name": "sdc","size": 7747397632,"model": "DataTraveler_2.0","avail": 7714418688,"children": null}]
-func (n *notifyService) SendSystemStatusNotify(message map[string]interface{}) error {
-	url := strings.TrimSuffix(n.address, "/") + APICasaOSNotify + "/system_status"
-
-	body, err := json.Marshal(message)
-	if err != nil {
-		return err
-	}
-	response, err := http.Post(url, "application/json", bytes.NewBuffer(body))
-	if err != nil {
-		return err
-	}
-
-	if response.StatusCode != http.StatusOK {
-		return errors.New("failed to send notify (status code: " + fmt.Sprint(response.StatusCode) + ")")
-	}
-	return nil
-}
-
-func NewNotifyService(runtimePath string) (NotifyService, error) {
-	casaosAddressFile := filepath.Join(runtimePath, CasaOSURLFilename)
-
-	buf, err := os.ReadFile(casaosAddressFile)
-	if err != nil {
-		return nil, err
-	}
-
-	address := string(buf)
-
-	response, err := http.Get(address + "/ping")
-	if err != nil {
-		return nil, err
-	}
-
-	if response.StatusCode != http.StatusOK {
-		return nil, errors.New("failed to ping casaos service")
-	}
-
-	return &notifyService{
-		address: address,
-	}, nil
-}

+ 0 - 29
common/notify_test.go

@@ -1,29 +0,0 @@
-package common
-
-import "testing"
-
-func TestSendNotify(t *testing.T) {
-	notify, err := NewNotifyService("/var/run/casaos")
-	if err != nil {
-		t.Fatal(err)
-	}
-	err = notify.SendNotify("test", map[string]interface{}{
-		"test": "test",
-	})
-	if err != nil {
-		t.Fatal(err)
-	}
-}
-
-func TestSendSystemStatusNotify(t *testing.T) {
-	notify, err := NewNotifyService("/var/run/casaos")
-	if err != nil {
-		t.Fatal(err)
-	}
-	err = notify.SendSystemStatusNotify(map[string]interface{}{
-		"sys_usb": `[{"name": "sdc","size": 7747397632,"model": "DataTraveler_2.0","avail": 7714418688,"children": null}]`,
-	})
-	if err != nil {
-		t.Fatal(err)
-	}
-}

+ 0 - 78
common/share.go

@@ -1,78 +0,0 @@
-package common
-
-import (
-	"bytes"
-	"encoding/json"
-	"errors"
-	"fmt"
-	"net/http"
-	"os"
-	"path/filepath"
-	"strings"
-)
-
-const (
-	APICasaOSShare = "/v1/samba/shares"
-)
-
-type ShareService interface {
-	DeleteShare(id string) error
-}
-type shareService struct {
-	address string
-}
-
-func (n *shareService) DeleteShare(id string) error {
-	url := strings.TrimSuffix(n.address, "/") + APICasaOSShare + "/" + id
-	fmt.Println(url)
-	message := "{}"
-	body, err := json.Marshal(message)
-	if err != nil {
-		return err
-	}
-
-	client := &http.Client{}
-
-	// Create request
-	req, err := http.NewRequest("DELETE", url, bytes.NewBuffer(body))
-	if err != nil {
-		return err
-	}
-
-	// Fetch Request
-	response, err := client.Do(req)
-	if err != nil {
-		return err
-	}
-	defer response.Body.Close()
-
-	if response.StatusCode != http.StatusOK {
-		return errors.New("failed to send share (status code: " + fmt.Sprint(response.StatusCode) + ")")
-	}
-	return nil
-
-}
-
-func NewShareService(runtimePath string) (ShareService, error) {
-	casaosAddressFile := filepath.Join(runtimePath, CasaOSURLFilename)
-
-	buf, err := os.ReadFile(casaosAddressFile)
-	if err != nil {
-		return nil, err
-	}
-
-	address := string(buf)
-
-	response, err := http.Get(address + "/ping")
-	if err != nil {
-		return nil, err
-	}
-
-	if response.StatusCode != 200 {
-		return nil, errors.New("failed to ping casaos service")
-	}
-
-	return &shareService{
-		address: address,
-	}, nil
-}

+ 0 - 14
common/share_test.go

@@ -1,14 +0,0 @@
-package common
-
-import "testing"
-
-func TestDeleteShare(t *testing.T) {
-	share, err := NewShareService("/var/run/casaos")
-	if err != nil {
-		t.Fatal(err)
-	}
-	err = share.DeleteShare("1")
-	if err != nil {
-		t.Fatal(err)
-	}
-}

+ 1 - 1
go.mod

@@ -4,7 +4,7 @@ go 1.16
 
 
 require (
 require (
 	github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d
 	github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d
-	github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220929035515-b1287110d6d8
+	github.com/IceWhaleTech/CasaOS-Common v0.3.7-1
 	github.com/IceWhaleTech/CasaOS-Gateway v0.3.6
 	github.com/IceWhaleTech/CasaOS-Gateway v0.3.6
 	github.com/Microsoft/go-winio v0.5.0 // indirect
 	github.com/Microsoft/go-winio v0.5.0 // indirect
 	github.com/ambelovsky/go-structs v1.1.0 // indirect
 	github.com/ambelovsky/go-structs v1.1.0 // indirect

+ 2 - 2
go.sum

@@ -84,8 +84,8 @@ github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d
 github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d/go.mod h1:lW9x+yEjqKdPbE3+cf2fGPJXCw/hChX3Omi9QHTLFsQ=
 github.com/Curtis-Milo/nat-type-identifier-go v0.0.0-20220215191915-18d42168c63d/go.mod h1:lW9x+yEjqKdPbE3+cf2fGPJXCw/hChX3Omi9QHTLFsQ=
 github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
 github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
 github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220901034123-ca130f6b5ce9/go.mod h1:2MiivEMzvh41codhEKUcn46WK3Ffesop/04qa9jsvQk=
 github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220901034123-ca130f6b5ce9/go.mod h1:2MiivEMzvh41codhEKUcn46WK3Ffesop/04qa9jsvQk=
-github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220929035515-b1287110d6d8 h1:r8nhgQ6tnrn6ikXN9aLH/K4H4H64Nc0hZ6jyW2B22x0=
-github.com/IceWhaleTech/CasaOS-Common v0.0.0-20220929035515-b1287110d6d8/go.mod h1:2MiivEMzvh41codhEKUcn46WK3Ffesop/04qa9jsvQk=
+github.com/IceWhaleTech/CasaOS-Common v0.3.7-1 h1:paay9dFGAWjNcjtCrN6ymvuU21KtxPoIpNG3wo10ufk=
+github.com/IceWhaleTech/CasaOS-Common v0.3.7-1/go.mod h1:2MiivEMzvh41codhEKUcn46WK3Ffesop/04qa9jsvQk=
 github.com/IceWhaleTech/CasaOS-Gateway v0.3.6 h1:2tQQo85+jzbbjqIsKKn77QlAA73bc7vZsVCFvWnK4mg=
 github.com/IceWhaleTech/CasaOS-Gateway v0.3.6 h1:2tQQo85+jzbbjqIsKKn77QlAA73bc7vZsVCFvWnK4mg=
 github.com/IceWhaleTech/CasaOS-Gateway v0.3.6/go.mod h1:hnZwGUzcOyiufMpVO7l3gu2gAm6Ws4TY4Nlj3kMshXA=
 github.com/IceWhaleTech/CasaOS-Gateway v0.3.6/go.mod h1:hnZwGUzcOyiufMpVO7l3gu2gAm6Ws4TY4Nlj3kMshXA=
 github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
 github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=

+ 3 - 3
main.go

@@ -8,7 +8,7 @@ import (
 	"path/filepath"
 	"path/filepath"
 	"time"
 	"time"
 
 
-	"github.com/IceWhaleTech/CasaOS-Gateway/common"
+	"github.com/IceWhaleTech/CasaOS-Common/model"
 	"github.com/IceWhaleTech/CasaOS/model/notify"
 	"github.com/IceWhaleTech/CasaOS/model/notify"
 	"github.com/IceWhaleTech/CasaOS/pkg/cache"
 	"github.com/IceWhaleTech/CasaOS/pkg/cache"
 	"github.com/IceWhaleTech/CasaOS/pkg/config"
 	"github.com/IceWhaleTech/CasaOS/pkg/config"
@@ -112,7 +112,7 @@ func main() {
 	}
 	}
 	routers := []string{"sys", "apps", "container", "app-categories", "port", "file", "folder", "batch", "image", "samba", "notify"}
 	routers := []string{"sys", "apps", "container", "app-categories", "port", "file", "folder", "batch", "image", "samba", "notify"}
 	for _, v := range routers {
 	for _, v := range routers {
-		err = service.MyService.Gateway().CreateRoute(&common.Route{
+		err = service.MyService.Gateway().CreateRoute(&model.Route{
 			Path:   "/v1/" + v,
 			Path:   "/v1/" + v,
 			Target: "http://" + listener.Addr().String(),
 			Target: "http://" + listener.Addr().String(),
 		})
 		})
@@ -126,7 +126,7 @@ func main() {
 		time.Sleep(time.Second * 2)
 		time.Sleep(time.Second * 2)
 		// v0.3.6
 		// v0.3.6
 		if config.ServerInfo.HttpPort != "" {
 		if config.ServerInfo.HttpPort != "" {
-			changePort := common.ChangePortRequest{}
+			changePort := model.ChangePortRequest{}
 			changePort.Port = config.ServerInfo.HttpPort
 			changePort.Port = config.ServerInfo.HttpPort
 			err := service.MyService.Gateway().ChangePort(&changePort)
 			err := service.MyService.Gateway().ChangePort(&changePort)
 			if err == nil {
 			if err == nil {

+ 5 - 5
service/service.go

@@ -11,7 +11,7 @@
 package service
 package service
 
 
 import (
 import (
-	gateway "github.com/IceWhaleTech/CasaOS-Gateway/common"
+	"github.com/IceWhaleTech/CasaOS-Common/external"
 	"github.com/gorilla/websocket"
 	"github.com/gorilla/websocket"
 	"github.com/patrickmn/go-cache"
 	"github.com/patrickmn/go-cache"
 	"gorm.io/gorm"
 	"gorm.io/gorm"
@@ -35,12 +35,12 @@ type Repository interface {
 	System() SystemService
 	System() SystemService
 	Shares() SharesService
 	Shares() SharesService
 	Connections() ConnectionsService
 	Connections() ConnectionsService
-	Gateway() gateway.ManagementService
+	Gateway() external.ManagementService
 }
 }
 
 
 func NewService(db *gorm.DB, RuntimePath string) Repository {
 func NewService(db *gorm.DB, RuntimePath string) Repository {
 
 
-	gatewayManagement, err := gateway.NewManagementService(RuntimePath)
+	gatewayManagement, err := external.NewManagementService(RuntimePath)
 	if err != nil && len(RuntimePath) > 0 {
 	if err != nil && len(RuntimePath) > 0 {
 		panic(err)
 		panic(err)
 	}
 	}
@@ -68,10 +68,10 @@ type store struct {
 	system      SystemService
 	system      SystemService
 	shares      SharesService
 	shares      SharesService
 	connections ConnectionsService
 	connections ConnectionsService
-	gateway     gateway.ManagementService
+	gateway     external.ManagementService
 }
 }
 
 
-func (c *store) Gateway() gateway.ManagementService {
+func (c *store) Gateway() external.ManagementService {
 	return c.gateway
 	return c.gateway
 }
 }
 func (s *store) Connections() ConnectionsService {
 func (s *store) Connections() ConnectionsService {