diff --git a/common/notify.go b/common/notify.go deleted file mode 100644 index 5c738e4..0000000 --- a/common/notify.go +++ /dev/null @@ -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 ¬ifyService{ - address: address, - }, nil -} diff --git a/common/notify_test.go b/common/notify_test.go deleted file mode 100644 index 1e9c543..0000000 --- a/common/notify_test.go +++ /dev/null @@ -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) - } -} diff --git a/common/share.go b/common/share.go deleted file mode 100644 index a0da4b2..0000000 --- a/common/share.go +++ /dev/null @@ -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 -} diff --git a/common/share_test.go b/common/share_test.go deleted file mode 100644 index ce79e03..0000000 --- a/common/share_test.go +++ /dev/null @@ -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) - } -}